这里必须先安装:
yum install expect -y
expect是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录。
下面给出scp和ssh的使用示例:
1、scp
[root@BS-WORKERMAN-NFS package]# cat scp.sh
#!/bin/bash
src=/package
dsc=/root/
for i in {4,5,6}
do
expect -c "
spawn scp -r $src/scp.sh root@10.20.18.2$i:$dsc
expect {
"*assword" {set timeout 300; send "game2017./
";}
"yes/no" {send "yes
"; exp_continue;}
}
expect eof"
done
2、ssh
#!/bin/bash
expect -c "
spawn ssh root@192.168.1.204 "ls;"
expect {
"*assword" {set timeout 300; send "password
";}
"yes/no" {send "yes
"; exp_continue;}
}
expect eof"