expect自动化交互脚本(一)
生活随笔
收集整理的这篇文章主要介绍了
expect自动化交互脚本(一)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
最近在弄ansible的时候,每次使用的时候都要输入密码感觉非常的麻烦,起初是一台一台做无密码验证,但是效率太低。了解到linux是可以批量做部署的,但是需要使用expect脚本来完成。
expect脚本需要linux安装expect程序才能被支持
[root@DECMDB01 ~]# yum -y install expect在执行的时候需要通过expect命令执行:
先看一组例子:
正常远程登陆Linux服务器:
[root@130 ~]# ssh root@192.168.222.131 The authenticity of host '192.168.222.131 (192.168.222.131)' can't be established. RSA key fingerprint is e4:69:83:2f:74:75:56:70:87:a6:4f:b1:8d:2e:01:8f. Are you sure you want to continue connecting (yes/no)? ^C ##在不使用expect脚本我们需要手动输入yes/no [root@130 ~]#通过expect登陆Linux服务器:
[root@130 ~]# expect auto_yes.exp ##通过expect自动化脚本执行登陆 spawn ssh root@192.168.222.131 The authenticity of host '192.168.222.131 (192.168.222.131)' can't be established. RSA key fingerprint is e4:69:83:2f:74:75:56:70:87:a6:4f:b1:8d:2e:01:8f. Are you sure you want to continue connecting (yes/no)? yes ##这里的yes是脚本自动补全 Warning: Permanently added '192.168.222.131' (RSA) to the list of known hosts. root@192.168.222.131's password: ##密码自动填充 Last login: Fri Nov 3 03:19:31 2017 from 192.168.222.1 [root@131 ~]# ##这里已经成功ssh到192.168.222.131比较差异:
expect脚本避免了手动输入yes或no,如果在未来工作中,有这种交互动作非常频繁,而且响应结果一致的话,那么显然expect是个不错的选择。
=============================================================
我们画个分割线看看脚本是怎么写的
#!/bin/expect ##指定脚本解释器 spawn ssh root@192.168.222.131 ##执行命令 expect {"yes/no" {send "yes\r";exp_continue} ##匹配到yes/no就自动输入yes,继续往下匹配"*password" {send "root\r"} ##匹配到password,则自动输入root } expect eof ##结束转载于:https://blog.51cto.com/swiki/1978487
总结
以上是生活随笔为你收集整理的expect自动化交互脚本(一)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Modbus RTU 通信工具设计
- 下一篇: Moodle安全漏洞如何启动远程代码执行