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安全漏洞如何启动远程代码执行