Expect自动化交互式程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34646546/article/details/88766509

Expect自动化交互式程序

Expect介绍

Expect是一个用来实现自动交互功能的软件套件, 是基于TCL的脚本编程工具语言

Expect的使用

首先 要安装Expect  直接使用yum安装即可

yum install expect -y

举个例子

每次我们进行ssh连接时都需要输入密码  而且首次需要输入yes来确认

[root@alice ~]# ssh -p 22 [email protected] "hostname -I"
The authenticity of host '[149.28.24.71]:1205 ([149.28.24.71]:1205)' can't be established.
RSA key fingerprint is SHA256:kKgl7C0Z+14ZEKTYDW1QhnY9KTpwKh62+u1tx+hoLLc.
RSA key fingerprint is MD5:30:2b:0f:51:a7:ac:b8:83:b3:d0:38:65:4b:90:9f:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[149.28.24.71]:1205' (RSA) to the list of known hosts.
[email protected]'s password: 
149.28.24.71 2001:19f0:7001:7ee:5400:1ff:fef1:9fe5 

这种情况 我们就可以用Expect来实现自动交互

代码如下:

[root@alice expect]# cat ssh.ex                    扩展名为ex 代表是Expect脚本
#!/usr/bin/expect

spawn  ssh -p 22 [email protected] "hostname -I"  执行ssh命令 (开头需要有spawn 否则无法正常执行)

expect {
	"yes/no" { send "yes\r"; exp_continue  }    利用Expect获取执行上述执行命令输出的字符串是否为期待的字符串"yes/no" 如果是 则发送yes ,这里的/n为换行
	"password:" { send  "123456\r" };           利用Expect获取执行上述执行命令输出的字符串是否为期待的字符串"password" 如果是 则发送123456 ,
}
interact                                           处理完毕后结束标识


 

猜你喜欢

转载自blog.csdn.net/qq_34646546/article/details/88766509
今日推荐