expect用来实现简单的输入密码验证,在检测到password字符的时候进行输入.
通过这个原理可以实现更复杂的自动化,此处只是进行最简单的实验
写脚本运行,第一行,没有就安装
#!/usr/bin/expect
设置变量
set port xxxxx
set user xxxxx
set passed xxxxx
set host xxx.xxx.xxx.xxx
set remote_file "path/to/file"
set local_file "path/to/file"
spawn,fork新进程执行后续命令.此处有个坑,如果把端口号用户名和主机号写成一个字符串,会报错
spawn scp -P $port $user@$host:$remote_file $local_file
检测到”password”时继续执行,若检测不到则等待到超时,(默认超时时间没查,不清楚).此处可用正则表达式
expect "*password:*"
相当于在控制台输入变量passwd,根据情况决定是否输入回车(\r)
send "$passwd\r"
返回控制台
interact