expect 自动输入密码

测试环境centos 6.5 7.4

1、远程登陆执行命令  scp

#!/bin/bash

expect -c '
set timeout 10000
spawn ssh root@192.168.67.22

#spawn scp ./3.sh 192.168.67.133:/root/zzx/

expect {
"yes/no" {send "yes\r"; exp_continue}
"*assword" {send "1\r"}
}
expect "#"
#send "ip a\r" #send "sleep 1\r"
send "hostname\r"
expect "#"
send "echo 123\r"
expect "#"
send "exit\r"
#interact #注释上面两行(exit去掉) 这一行注释去掉可以停在远程机器    interact 
'
#以下和expect无关
#hostname
echo "end expect"

 2、scp

#!/bin/bash
src=/root/zzx
dsc=/root/zzx
expect -c "
spawn scp -r $src/scp.sh root@192.168.67.133:$dsc
expect {
\"*assword\" {set timeout 300; send \"1\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof" 

 

posted on 2017-11-14 11:43  寒星12345678999  阅读(348)  评论(0编辑  收藏  举报