Linux中使用expect脚本实现远程机器自动登录
首先创建一个expect脚本ssh_expect,文件内容如下:
#!/usr/bin/expect-f sethostname[lindex$argv0] setuser[lindex$argv1] setpasswd[lindex$argv2] settimeout30 setforce_conservative1 if{$force_conservative}{ setsend_slow{128.1} } spawnssh$user@$hostname; expect{ "*continueconnecting(yes/no)?"{send-s"yes\r";exp_continue} "*assword:"{ send-s"$passwd\r"; } } interact
然后定义一些命令别名,比如:
aliash101='ssh_expect192.168.0.101<user><password>' aliash102='ssh_expect192.168.0.102<user><password>'
这些别名可以写到~/.bashrc文件中
然后执行h101就可以自动登录192.168.0.101机器了。