Leif160519的blog

expect是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录。
下面给出ssh和scp的使用示例:
#!/bin/bash
expect -c "
spawn ssh root@192.168.81.11 \"ls;\"
expect {
\"*assword\" {set timeout 30; send \"password\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
其中,
password代表机器的密码,并且最后必须以\r结束
expect -c "
spawn scp root@192.168.81.11:/root/1.log /root
expect {
\"*assword\" {set timeout 30; send \"password\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
同理,
password代表机器的密码,并且最后必须以\r结束
spawn:spawn是expect的语句,执行命令前都要加这句expect “assword:“:这句意思是交互获取是否返回 password:关键字,因为在执行命令时会返回输入 password 的提示 xxxxxxxx password:set:设定变量为某个值send:将密码发送出去exp_continue:重新执行 expect 命令分支set timeout -1 :设置超时方式为永远等待set timeout 30 "设置超时时间为 30 秒interact:代表执行完留在远程控制台,不加这句执行完后返回本地控制台expect eof:等待 spawn 进程结束后退出信号 eof“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:expect实现scp ssh自动输入密码登录