在home
目錄下建立.expect
目錄,並添加兩個文件:bash
ssh-login.exp
#!/usr/bin/expect -f set timeout 30 set host [lindex $argv 0] set port [lindex $argv 1] set user [lindex $argv 2] set password [lindex $argv 3] spawn ssh -p $port $user@$host expect { "yes/no" { send "yes\r"; exp_continue } "password:" { send "$password\r" } } interact
ssh-login.sh
# alias ssh-name="./expect/ssh-login.exp host port username password" alias ssh-n1.cluster=".expect/ssh-login.exp 192.168.9.181 22 root cluster" alias ssh-n2.cluster=".expect/ssh-login.exp 192.168.9.182 22 root cluster" alias ssh-n3.cluster=".expect/ssh-login.exp 192.168.9.183 22 root cluster" alias ssh-n4.cluster=".expect/ssh-login.exp 192.168.9.184 22 root cluster" alias ssh-n5.cluster=".expect/ssh-login.exp 192.168.9.185 22 root cluster"
在.bashrc
下添加ssh
if [ -f ~/.expect/ssh-login.sh ]; then . ~/.expect/ssh-login.sh fi