Skip to content

Instantly share code, notes, and snippets.

@caiyili
Last active August 29, 2015 14:06
Show Gist options
  • Save caiyili/a94073ed842b1c96d58e to your computer and use it in GitHub Desktop.
Save caiyili/a94073ed842b1c96d58e to your computer and use it in GitHub Desktop.
ssh connect between two machine
#!/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
@caiyili
Copy link
Author

caiyili commented Sep 19, 2014

一个机器之间跳转管理的脚本,把机器名/密码等写在一个配置文件里,连接的时候就直接goto跳转到相应机器上。使用的是expect自动输入密码。
上述脚本依赖于两个文件 goto.conf | doexpect.sh
1)doexpect.sh

#!/home/tools/bin/64/expect -f

set user [lindex $argv 0]
set host [lindex $argv 1]
set pwd  [lindex $argv 2]

spawn ssh -l $user $host
expect {
    "no)?" {send "yes\r" ;}
    "password:" { send "$pwd\r";}
}
interact

2)goto.conf格式如下
···
aliasname \t username \t host \t password
···

@caiyili
Copy link
Author

caiyili commented Sep 19, 2014

使用方法:
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