個人終端

關於windows的ssh client終端, 着實糾結好久, 從putty到SecureCRT到Xshell再到mRemoteNG再到Tera Terminal. 再到後來使用的Cygwin+ConEmu+Screen的管理方式. 老是以爲不那麼趁心如意, 每種ssh client都存在着各類各樣不一樣的問題. 其實就歸結於一點, windows的終端實在是不夠給力. node

最終個人解決方案是cygwin + tmux + tmux-logging+ oh-my-zsh + autojump + shell腳本的管理方式. 終端就是用cygwin自帶的minttypython

首先安裝cygwin, 能夠直接去cygwin官網下載https://www.cygwin.com/, 而後安裝便可, 建議使用mirrors.163.com的源進行下載linux

安裝oh-my-zsh和autojumpgit

wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

cygwin默認啓動使用bash, 修改.bashrc文件添加github

exec zsh

個人zsh配置文件,供參考web

export ZSH=$HOME/.oh-my-zsh
ZSH_THEME="ys"
DISABLE_AUTO_UPDATE="true"
plugins=(git textmate ruby autojump osx mvn gradle)
export PATH=$HOME/bin:/usr/local/bin:$PATH
source $ZSH/oh-my-zsh.sh
if [[ -n $SSH_CONNECTION ]]; then
   export EDITOR='vim'
 else
   export EDITOR='vim'
 fi
alias vi='vim'
alias drccd="ssh sun59@drccd.dr.avaya.com"
alias toolsa="ssh sun59@st3tds04.us1.avaya.com"
alias vsp_test='ssh admin@192.168.65.31'
alias sb='screen -S hugh -c /home/Administrator/screen/screenrc'
alias sx='screen -r hugh'
alias vm='screen -S vm -c /home/Administrator/screen/vm'
alias ls='ls --color'
alias grep='grep --color'
alias open='cygstart'
export LANG=en_US.UTF-8
echo -e "\033[33mWelcome to Linux World\033[0m"
alias rm='rm -i'
alias ca='screen -S ca -c /home/Administrator/screen/test'
alias c2="noglob sh c2"
alias mm='open /home/Administrator/Desktop/Tools/sublime/Docs/Markdown/web_service.md'
alias c='pygmentize -g'
ANSIBLE=/opt/ansible
export PATH=$PATH:$ANSIBLE/bin
export PYTHONPATH=$ANSIBLE/lib
export ANSIBLE_LIBRARY=$ANSIBLE/library
export CHEATCOLORS=true
function _cheat_autocomplete {
    sheets=$(cheat -l | cut -d' ' -f1)
    COMPREPLY=()
    if [ $COMP_CWORD = 1 ]; then
        COMPREPLY=(`compgen -W "$sheets" -- $2`)
    fi
}
export PATH=/cygdrive/c/Users/Administrator/Desktop/Tools/sublime/Docs/python/bin:$PATH

安裝autojumpshell

wget https://github.com/downloads/joelthelion/autojump/autojump_v21.1.2.tar.gz
tar -xf autojump_v21.1.2.tar.gz
cd autojump_v21.1.2
./install.sh

而後安裝tmux, 直接按照tmux的官方文檔安裝vim

 git clone https://github.com/tmux/tmux.git
 cd tmux
 sh autogen.sh
 ./configure && make

而後配置tmux, 下面是我tmux的配置文件, 個人bind key是^o, 不一樣於默認的^a, 由於在linux系統中常常要使用^a回到行首.windows

主要實現了幾個功能.api

1. 快速打開man page 使用bindkey + m

2. 快速的記錄筆記 bindkey + n

3. 快速的打開一個開發環境鏈接 bindkey + a

4. 快速的打開小抄功能(cheat)

5. 綁定使用快捷鍵, 好比alt + 1輸入一個經常使用的默認密碼itsgrand3

# released under the WTFPL v2 license, by Gregory Pakosz (@gpakosz)
# modified by Ying. last version 2016/8/15
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set-option -g @plugin knakayama/tmux-newline-detector
run '~/.tmux/plugins/tpm/tpm'
set -g prefix ^o
unbind ^b
set -g -q mouse off
set -g default-terminal "screen-256color" # colors!
setw -g xterm-keys on
set -s escape-time 0 # fastest command sequences
set -sg repeat-time 600 # increase repeat timeout
set -s quiet on # disable various messages
set -g history-limit 65535 # boost history
bind r source-file ~/.tmux.conf \; display '~/.tmux.conf sourced'
if 'which -s reattach-to-user-namespace' 'set -g default-command "exec initializing... 2> /dev/null & reattach-to-user-namespace $SHELL -l"'
set -g base-index 1 # start windows numbering at 1
setw -g pane-base-index 1 # make pane numbering consistent with windows
setw -g automatic-rename on # rename window to reflect current program
set -g renumber-windows on
set -g set-titles on # set terminal title
set -g set-titles-string '#h ❐ #S ● #I #W'
set -g display-panes-time 800 # slightly longer pane indicators display time
set -g display-time 1000 # slightly longer status messages display time
set -g status-interval 10 # redraw status line every 10 seconds
setw -g clock-mode-style 24
bind -n C-l send-keys C-l \; run 'tmux clear-history'
set -g monitor-activity on
set -g visual-activity off
bind C-f command-prompt -p find-session 'switch-client -t %%'
bind -r h select-pane -L # move left
bind -r j select-pane -D # move down
bind -r k select-pane -U # move up
bind -r l select-pane -R # move right
bind > swap-pane -D # swap current pane with the next one
bind < swap-pane -U # swap current pane with the previous one
bind + run 'cut -c3- ~/.tmux.conf | sh -s maximize_pane'
bind -r H resize-pane -L 2
bind -r J resize-pane -D 2
bind -r K resize-pane -U 2
bind -r L resize-pane -R 2

unbind n
unbind p
bind -r C-h previous-window # select previous window
bind -r C-l next-window # select next window
bind Tab last-window # move to last active window
bind m command-prompt "splitw 'exec man %%'" # man (prefix m)
bind n command-prompt "splitw 'exec vi ~/notes/`date +%F`_%%'"
bind a run "tmux splitw /usr/bin/ssh sun59@st3tds04.us1.avaya.com"
bind y run "tmux splitw /usr/bin/ssh 127.0.0.1"
bind e command-prompt "splitw 'exec cheat -e %%'"
bind U run "cut -c3- ~/.tmux.conf | sh -s urlview #{pane_id}"
bind F run "cut -c3- ~/.tmux.conf | sh -s fpp #{pane_id}"
bind -t vi-choice h tree-collapse
bind -t vi-choice l tree-expand
run -b 'tmux bind -t vi-choice K start-of-list 2> /dev/null'
run -b 'tmux bind -t vi-choice J end-of-list 2> /dev/null'
bind -t vi-choice H tree-collapse-all
bind -t vi-choice L tree-expand-all
bind -t vi-choice Escape cancel
bind -ct vi-edit H start-of-line
bind -ct vi-edit L end-of-line
bind -ct vi-edit q cancel
bind -ct vi-edit Escape cancel
bind Escape copy-mode # enter copy mode
bind b list-buffers # list paster buffers
bind p paste-buffer # paste from the top pate buffer
bind P choose-buffer # choose which buffer to paste from

bind -t vi-copy v begin-selection
bind -t vi-copy C-v rectangle-toggle
bind -t vi-copy y copy-selection
bind -t vi-copy Escape cancel
bind -t vi-copy H start-of-line
bind -t vi-copy L end-of-line
if -b 'which -s reattach-to-user-namespace' 'bind y run "tmux save-buffer - | reattach-to-user-namespace pbcopy"'
if '[ -f ~/.tmux.conf.local ]' 'source ~/.tmux.conf.local'
unbind '"'
bind - splitw -v # vertical split (prefix -)
unbind %
bind | splitw -h # horizontal split (prefix |)
set -g @plugin 'tmux-plugins/tmux-logging'
run-shell ~/clone/path/logging.tmux
bind-key -n M-1 send-keys itsgrand3
bind-key -n M-2 send-keys admin01
bind-key -n M-3 send-keys root01
bind-key -n M-4 send-keys sroot01
bind-key -n M-5 send-keys Administrator
bind-key -n M-6 send-keys redhat
bind-key -n F1 send-keys Escape x
bind-key -n F2 send-keys Escape r
bind-key -n F3 send-keys Escape e
bind-key -n F5 send-keys Escape h
bind-key -n F7 send-keys Escape n
bind-key -n F8 send-keys Escape p

日誌功能須要安裝tmux的插件

git clone https://github.com/tmux-plugins/tmux-logging ~/clone/path

 而後在 ~/.tmux/plugins/tmux-logging/script/variables.sh 中修改快捷鍵. 而後就能夠在想記錄日誌的時候使用tmux的快捷鍵安裝日誌便可

最後就是腳本的使用來作主機管理, 我我的使用的是shell腳本, 方式也比較簡單易實現. 將來預期可使用python的paramiko, 而後將登陸主機信息入庫來控制

寫了一個c2腳本, 他會去讀取我本地目錄的xshell_session文件

#!/bin/zsh

xshell_session=/home/Administrator/xshell_session

if [ $# -ge 1 ];then
    cat $xshell_session|egrep $1 &> /dev/null
    if [ $? -eq 0 ];then
        B_screen=$(cat $xshell_session|egrep $1|awk -F "---->" '{print $2}')
    else
        echo "the host cannot be found!"
        exit 1
    fi
else
    echo "You need input something"
    exit 2

fi

if [ "$TERM" == "screen" -o "$TERM" == "cygwin" -o "$TERM" == "xterm" ];then
if [ ! $2 ]; then
    screen -t $1 $B_screen
    exit 0
else
    screen -t $2 $B_screen
    exit 0
fi
fi

if [ $TERM == 'screen-256color' ];then
if [ ! $2 ];then
    tmux new-window -n $1 $B_screen
else
    tmux new-window -n $2 $B_screen
fi

fi

xshell_sessions格式以下

vm_node1                  ----> ssh root@192.168.48.131
vm_node2                  ----> ssh root@192.168.48.132
vm_rhel6                  ----> ssh root@192.168.48.133
vm_node3                  ----> ssh root@192.168.48.137
vm_node4                  ----> ssh root@192.168.48.138

能夠直接c2 node1來建立遠程鏈接.

此外可使用初始化腳原本建立初始化的tmux session, 示例以下, 能夠直接啓動多個窗口

session=Linux

if [ ! $1 ];then
tmux has -t $session 2>/dev/null
    if [ $? != 0 ];then
        tmux new -d -n Linux -s Linux ssh root@192.168.48.130
        tmux split-window -h
        tmux new-window -n node1 -d ssh root@192.168.48.131
        tmux splitw -h -d -t node1   ssh root@192.168.48.132
        tmux new-window -n node3 -d ssh root@192.168.48.137
        tmux splitw -h -d -t node3 ssh root@192.168.48.138
        tmux new-window -n cm202 -d ssh init@10.133.169.202
        tmux splitw -h -d -t cm202 ssh init@192.168.64.57
        tmux new-window -n cm36 -d ssh init@192.168.64.36
        tmux splitw -h -d -t cm36 ssh init@192.168.70.209
        tmux new-window -n lab_sm -d ssh cust@192.168.65.36
        tmux splitw -h -d -t lab_sm ssh admin@192.168.65.33
        tmux new-window -n dup_cm1 -d ssh craft@10.130.124.86
        tmux splitw -h -d -t dup_cm1 ssh craft@10.130.124.85
        tmux att -t $session
    else
        tmux att -t $session
    fi
else
    if [ $1 == "-q" ];then
        tmux kill-session -t $session > /dev/null 2>&1
        exit 0
    fi
    if [ $1 == "-x" ];then
        tmux new -d -n linux -s Linux
        tmux new-window -n c1 -d ssh root@192.168.48.130
        tmux new-window -n n1 -d ssh root@192.168.48.131
        tmux new-window -n n2 -d ssh root@192.168.48.132
        tmux new-window -n n3 -d ssh root@192.168.48.137
        tmux new-window -n n4 -d ssh root@192.168.48.138
        tmux new-window -n p1 -d ssh root@192.168.48.135
        tmux att -t $session
    else
        echo -e "\033[032mWrong Input\033[0m"
        exit 1
    fi
fi

添加一張效果圖, 可使用^o +num 來進行session的切換

還有不少細節能夠我的定製. 

最後附上一個我我的使用ConEmu + screen的screen配置

screen -t c1 2 /usr/bin/ssh root@192.168.48.130
escape ^Oo
defscrollback 10000
#screen -t r7 2 /usr/bin/ssh root@192.168.48.135
screen -t class 9 /usr/bin/ssh root@192.168.64.100
screen -t kvm 7 /usr/bin/ssh root@192.168.64.93
#screen -t s2 8 /usr/bin/ssh root@192.168.48.136
screen -t x1 8 /usr/bin/ssh root@192.168.64.92
#screen -t client1 0 /usr/bin/ssh root@172.16.100.7
#screen -t cls 9 /usr/bin/ssh root@192.168.64.131
screen -t tsa 0 /usr/bin/ssh sun59@st3tds04.us1.avaya.com
screen -t n1 3 /usr/bin/ssh root@192.168.48.131
screen -t n2    4 /usr/bin/ssh root@192.168.48.132
screen -t n3 5 /usr/bin/ssh root@192.168.48.137
screen -t n4 6 /usr/bin/ssh root@192.168.48.138
screen -t bash 1


termcapinfo xterm* ti@:te@
# Scroll up
bindkey -d "^[[5S" eval copy "stuff 5\025"
bindkey -m "^[[5S" stuff 5\025

# Scroll down
bindkey -d "^[[5T" eval copy "stuff 5\004"
bindkey -m "^[[5T" stuff 5\004


bindkey "^[s" split -v
bindkey "^[q" focus
bindkey "^[f" only


#hardstatus off
#hardstatus alwaysfirstline
caption always "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%H %Y/%m/%d %c"
#backtick 1 5 5 true
#termcapinfo rxvt* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'
#hardstatus string "screen (%n: %t)"
#caption string "%{= kw}%Y-%m-%d;%c %{= kw}%-Lw%{= kG}%{+b}[%n %t]%{-b}%{= kw}%+Lw%1`"
#caption always

#hardstatus off
#hardstatus alwayslastline
#hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W} %c %{g}]'
相關文章
相關標籤/搜索