测试环境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
"; exp_continue}
"*assword" {send "1
"}
}
expect "#"
#send "ip a
" #send "sleep 1
"
send "hostname
"
expect "#"
send "echo 123
"
expect "#"
send "exit
"
#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 ";} "yes/no" {send "yes "; exp_continue;} } expect eof"