python + expect + list = goto myip
生活随笔
收集整理的這篇文章主要介紹了
python + expect + list = goto myip
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡介
對于工程師來講,服務器是必不可少的工具,我們上班大部分時間是在使用ssh這樣的軟件去登錄,但是林子大了,啥鳥都有,群體需要保護,在你和服務器之間,有了堡壘機,每次登錄都需要先登錄堡壘機,然后登錄服務器,So,
難道我們想浪費我們價值1000$/hour的time在登錄服務器的過程中嗎? 當然Say: NO!思路很簡單,
1. 我們需要借助expect來模擬我們登錄服務器的過程。文件名denglu
2. 我們需要借助一個賬號密碼本,來記錄我們的ip地址和對應的password。文件名list
3. 我們需要一個腳本來連接denglu和list。文件名goto
總結:
goto myip
讀取list找到對應的ip地址和對應的密碼
調用denglu來模擬登錄
/bin/goto
#!/usr/bin/env python #coding: utf8 # fileName: gotoimport os,sys,rehost = sys.argv[1]matchedline = [] for line in open("/bin/list"):result = re.findall(host,line)if len(result) == 1:matchedline.append(line)if len(matchedline) == 1:secretline=matchedline[0]print "Found the machted line."value = re.findall("\S+",secretline)if len(value) == 2:print "Get the password from file."print valueelse:print "Please check your config file list."exit(1)print valuehost = value[0]passwd = value[1]os.system("/bin/denglu %s \"%s\""%(host, passwd)) else:print "No Not Find Matched. Exit............."/bin/denglu
#!/usr/bin/expect -f # fileName: denglu#log_file /tmp/1.txtset username "yourName" set password "yourPasswd"# This is very important. set HOST [lindex $argv 0] set serverpassword [lindex $argv 1] set timeout 30#發給自己看的 #send_user "$serverpassword" #exit# connect via ssh spawn ssh -o StrictHostKeyChecking=no xxx@xxx.xxx.xxx -p xxxx####################### expect {-re "yes/no.*" {exp_send "yes\r"exp_continue}-re "password:.*" {exp_send "$password\r"} }expect {-re ">.*" {exp_send "ssh root@$HOST\r"} }expect {-re "yes/no.*" {exp_send "yes\r"exp_continue}-re "assword:.*" {exp_send "$serverpassword\r"#puts $serverpasswordputs "**********"}-re "yes/no.*" {exp_send "yes\r"exp_continue} }expect {-re "yes/no.*" {exp_send "yes\r"exp_continue}-re "#.*" {interact}}/bin/list
ip1 password1 ip2 password2goto denglu list三個文件我都放在了/bin下面,當然chmod a+x一下,然后使用命令goto ip1來登錄。
Enjoy~
總結
以上是生活随笔為你收集整理的python + expect + list = goto myip的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于paypal账户限制的话题
- 下一篇: Python网络爬虫反爬破解策略实战