首先創建一個expect腳本ssh_expect,文件內容如下:
#!/usr/bin/expect -f
set hostname [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set timeout 30
set force_conservative 1
if {$force_conservative} {
set send_slow {128 .1}
}
spawn ssh $user@$hostname;
expect {
"*continue connecting (yes/no)?" { send -s "yes\r"; exp_continue }
"*assword:" {
send -s "$passwd\r";
}
}
interact
然後定義一些命令別名,比如:
alias h101='ssh_expect 192.168.0.101 '
alias h102='ssh_expect 192.168.0.102 '
這些別名可以寫到~/.bashrc文件中
然後執行 h101 就可以自動登錄192.168.0.101機器了。