Last active
August 29, 2015 14:06
-
-
Save caiyili/a94073ed842b1c96d58e to your computer and use it in GitHub Desktop.
ssh connect between two machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
CONF=$HOME/.local/etc/goto.conf | |
EXPECT=$HOME/.local/bin/doexpect.sh | |
function goto { | |
if [[ "$1" == "-l" || "$1" == "" ]] ;then | |
__list_info | |
else | |
echo "connect to $1 ..." ; | |
_connect $1 | |
fi | |
} | |
function __list_info { | |
cat $CONF | awk -F"\t" 'BEGIN{OFS="\t"} {print $1,$2"@"$3}' | |
} | |
function __list { | |
cat $CONF | awk -F"\t" 'BEGIN{OFS="\t"}{print $1}' | |
} | |
function __go_complete { | |
local curw | |
COMPREPLY=() | |
curw=${COMP_WORDS[COMP_CWORD]} | |
COMPREPLY=($(compgen -W '`__list`' -- $curw)) | |
return 0 | |
} | |
function _connect { | |
local name=$1 | |
local line=$(cat $CONF | awk -F"\t" -v name=$name '$1 == name{print $2,$3,$4}' | head -n1) | |
echo "connect $line" | |
if [[ "$line" == "" ]]; then | |
echo "$name not in conf file!" | |
else | |
$EXPECT $line | |
fi | |
} | |
complete -F __go_complete goto |
使用方法:
1)在~/.bashrc或者~/.bash_profile中·source /path/to/goto.sh·
2)直接输入goto 列出可选择的登录机器
3)goto $aliasname 连接到指定的机器上
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一个机器之间跳转管理的脚本,把机器名/密码等写在一个配置文件里,连接的时候就直接goto跳转到相应机器上。使用的是expect自动输入密码。
上述脚本依赖于两个文件 goto.conf | doexpect.sh
1)doexpect.sh
2)goto.conf格式如下
···
aliasname \t username \t host \t password
···