安装
except是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录
因为要用到except,所以没有安装过的一定要先安装
scp
#!/usr/bin/expect
expect -c "
spawn scp -r ~/Documents/1.txt root@10.20.18.22:/tmp
expect {
\"*assword\" {set timeout 300; send \"密码\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
done
注意:
- spawn后面跟scp语句
- 密码后一定要跟\r
ssh
#!/usr/bin/expect
expect -c "
spawn ssh root@192.168.1.204 \"ls;\"
expect {
\"*assword\" {set timeout 300; send \"密码\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"