expect是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录。
下面给出scp和ssh的使用示例:
1、scp
expect -c "
spawn scp root@1.2.3.4:/root/1.log /root
expect {
\"*assword\" {set timeout 300; send \"password\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
2、ssh
#!/bin/bash
expect -c "
spawn ssh root@192.168.1.204 \"ls;\"
expect {
\"*assword\" {set timeout 300; send \"password\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
转载请标明文章来源:《https://www.centos.bz/2012/11/scp-ssh-auto-login-with-expect/》