(编辑:jimmy 日期: 2024/12/28 浏览:2)
首先创建一个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 <user> <password>' alias h102='ssh_expect 192.168.0.102 <user> <password>'
这些别名可以写到~/.bashrc文件中
然后执行 h101 就可以自动登录192.168.0.101机器了。