自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ShutIt:一個(gè)基于Python的shell自動(dòng)化框架

開發(fā) 后端 自動(dòng)化
本文通過實(shí)例簡(jiǎn)單介紹了ShutIt這個(gè)基于Python的自動(dòng)化框架的使用方法。

[[194837]]

譯者注:本文通過實(shí)例簡(jiǎn)單介紹了ShutIt這個(gè)基于Python的自動(dòng)化框架的使用方法。除了pexpect,我們又多了這個(gè)選擇。以下是譯文。

ShutIt是一個(gè)易于使用的基于shell的自動(dòng)化框架。它對(duì)基于python的expect庫(kù)(pexpect)進(jìn)行了包裝。你可以把它看作是“沒有痛點(diǎn)的expect”。它可以通過pip進(jìn)行安裝。

Hello World

讓我們從最簡(jiǎn)單的例子開始吧。創(chuàng)建一個(gè)名為example.py的文件:

  1. import shutit   
  2.  
  3. session = shutit.create_session('bash')   
  4.  
  5. session.send('echo Hello World', echo=True 

運(yùn)行這個(gè)文件:

  1. python example.py 

輸出:

  1. python example.py 
  2.  
  3. echo "Hello World" 
  4.  
  5. echo "Hello World" 
  6.  
  7. Hello World 
  8.  
  9. Ians-MacBook-Air.local:ORIGIN_ENV:RhuebR2T#  

“send”函數(shù)的***個(gè)參數(shù)是要運(yùn)行的命令。“echo”的參數(shù)將會(huì)輸出到終端上。默認(rèn)情況下,ShutIt是靜默的。

登錄服務(wù)器

如果你要登陸一臺(tái)服務(wù)器并執(zhí)行服務(wù)器上的命令。可以將example.py改為:

  1. import shutit 
  2.  
  3. session = shutit.create_session('bash'
  4.  
  5. session.login('ssh you@example.com'user='you'password='mypassword'
  6.  
  7. session.send('hostname', echo=True
  8.  
  9. session.logout()  

程序?qū)⒌卿浀竭@臺(tái)服務(wù)器上,并輸出主機(jī)名。

  1. hostname   
  2.  
  3. hostname   
  4.  
  5. example.com   
  6.  
  7. example.com:cgoIsdVv:heDa77HB#  

顯然,這很不安全!你可以這樣運(yùn)行:

  1. import shutit 
  2.  
  3. session = shutit.create_session('bash'
  4.  
  5. password = session.get_input('', ispass=True
  6.  
  7. session.login('ssh you@example.com'user='you'password=password
  8.  
  9. session.send('hostname', echo=True
  10.  
  11. session.logout()  

它會(huì)讓你輸入密碼:

  1. Input Secret: 
  2.  
  3. hostname 
  4.  
  5. hostname 
  6.  
  7. example.com 
  8.  
  9. example.com:cgoIsdVv:heDa77HB#  

同樣的,“login”方法在登錄后改變了提示符。你給了ShutIt一個(gè)登錄命令,并附帶用戶名和密碼(如果需要的話),然后,ShutIt會(huì)完成剩余的事情。

“logout”負(fù)責(zé)終止“login”,并向屏幕輸出發(fā)生的任何變化。

登錄到多臺(tái)服務(wù)器

假設(shè)你有一個(gè)集群包含兩臺(tái)服務(wù)器,并希望同時(shí)登錄到這兩個(gè)服務(wù)器上去。則只需要?jiǎng)?chuàng)建兩個(gè)會(huì)話,并運(yùn)行類似的login和send命令:

  1. import shutit 
  2.  
  3. session1 = shutit.create_session('bash'
  4.  
  5. session2 = shutit.create_session('bash'
  6.  
  7. password1 = session1.get_input('Password for server1', ispass=True
  8.  
  9. password2 = session2.get_input('Password for server2', ispass=True
  10.  
  11. session1.login('ssh you@one.example.com'user='you'password=password1) 
  12.  
  13. session2.login('ssh you@two.example.com'user='you'password=password2) 
  14.  
  15. session1.send('hostname', echo=True
  16.  
  17. session2.send('hostname', echo=True
  18.  
  19. session1.logout() 
  20.  
  21. session2.logout()  

將輸出這樣的結(jié)果:

  1. $ python example.py 
  2.  
  3. Password for server1 
  4.  
  5. Input Secret: 
  6.  
  7. Password for server2 
  8.  
  9. Input Secret: 
  10.  
  11. hostname 
  12.  
  13. hostname 
  14.  
  15. one.example.com 
  16.  
  17. one.example.com:Fnh2pyFj:qkrsmUNs# hostname 
  18.  
  19. hostname 
  20.  
  21. two.example.com 
  22.  
  23. two.example.com:Gl2lldEo:D3FavQjA#  

實(shí)例:監(jiān)控多臺(tái)服務(wù)器

我們可以通過添加一些代碼邏輯來(lái)檢查命令的輸出,從而將上述代碼變成一個(gè)簡(jiǎn)單的監(jiān)控工具:

  1. import shutit 
  2.  
  3. capacity_command="""df / | awk '{print $5}' | tail -1 | sed s/[^0-9]//""" 
  4.  
  5. session1 = shutit.create_session('bash'
  6.  
  7. session2 = shutit.create_session('bash'
  8.  
  9. password1 = session.get_input('Password for server1', ispass=True
  10.  
  11. password2 = session.get_input('Password for server2', ispass=True
  12.  
  13. session1.login('ssh you@one.example.com'user='you'password=password1) 
  14.  
  15. session2.login('ssh you@two.example.com'user='you'password=password2) 
  16.  
  17. capacity = session1.send_and_get_output(capacity_command) 
  18.  
  19. if int(capacity) < 10: 
  20.  
  21. print('RUNNING OUT OF SPACE ON server1!'
  22.  
  23. capacity = session2.send_and_get_output(capacity_command) 
  24.  
  25. if int(capacity) < 10: 
  26.  
  27. print('RUNNING OUT OF SPACE ON server2!'
  28.  
  29. session1.logout() 
  30.  
  31. session2.logout()  

在這里,我們用了“sendandget_output”方法來(lái)獲取capacity_command命令的輸出。

還有很多更加優(yōu)雅的方法可以完成上面的操作,但這取決于你想要Python有多聰明。

更復(fù)雜的IO – Expecting

假設(shè)你需要跟一個(gè)命令行程序進(jìn)行交互,并且要實(shí)現(xiàn)自動(dòng)化操作。在這里,我們使用telnet來(lái)舉一個(gè)簡(jiǎn)單的例子:

  1. import shutit 
  2.  
  3. session = shutit.create_session('bash'
  4.  
  5. session.send('telnet', expect='elnet>', echo=True
  6.  
  7. session.send('open google.com 80', expect='scape character', echo=True
  8.  
  9. session.send('GET /', echo=True, check_exit=False
  10.  
  11. session.logout()  

注意“expect”的參數(shù)。你只需要給出telnet提示符的一個(gè)子集來(lái)進(jìn)行匹配。

注意“check_exit”的參數(shù),后面我們會(huì)講到這個(gè)參數(shù)的。上面這段代碼將輸出:

  1. $ python example.py 
  2.  
  3. telnet 
  4.  
  5. telnet> open google.com 80 
  6.  
  7. Trying 216.58.214.14... 
  8.  
  9. Connected to google.com. 
  10.  
  11. Escape character is '^]'
  12.  
  13. GET / 
  14.  
  15. HTTP/1.0 302 Found 
  16.  
  17. Cache-Control: private 
  18.  
  19. Content-Type: text/html; charset=UTF-8 
  20.  
  21. Referrer-Policy: no-referrer 
  22.  
  23. Location: http://www.google.co.uk/?gfe_rd=cr&ei=huczWcj3GfTW8gfq0paQDA 
  24.  
  25. Content-Length: 261 
  26.  
  27. Date: Sun, 04 Jun 2017 10:57:10 GMT 
  28.  
  29. <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"
  30.  
  31. <TITLE>302 Moved</TITLE></HEAD><BODY> 
  32.  
  33. <H1>302 Moved</H1> 
  34.  
  35. The document has moved 
  36.  
  37. <A HREF="http://www.google.co.uk/?gfe_rd=cr&ei=huczWcj3GfTW8gfq0paQDA"
  38.  
  39. here 
  40.  
  41. </A>. 
  42.  
  43. </BODY></HTML> 
  44.  
  45. Connection closed by foreign host.  

現(xiàn)在回到“checkexit = false”上來(lái)。由于telnet命令會(huì)返回一個(gè)錯(cuò)誤的退出碼(1),我們不想讓腳本執(zhí)行失敗,這里的“checkexit = false”能讓ShutIt知道你并不關(guān)注這個(gè)退出碼。

如果你沒有傳入這個(gè)參數(shù),ShutIt會(huì)給你一個(gè)交互式的提示,如果你有終端接入的話。這被稱為“暫停點(diǎn)”。

暫停點(diǎn)

你可以隨便在什么時(shí)候通過調(diào)用以下方法來(lái)設(shè)置一個(gè)“暫停點(diǎn)”。

  1. [...] 
  2.  
  3. session.pause_point('This is a pause point'
  4.  
  5. [...]  

當(dāng)腳本運(yùn)行到暫停點(diǎn)時(shí),同時(shí)按下“Ctrl”和“]”,則可以讓腳本繼續(xù)執(zhí)行。這對(duì)于調(diào)試非常有用:添加一個(gè)暫停點(diǎn),看看周圍,然后繼續(xù)。試試這個(gè):

  1. import shutit 
  2.  
  3. session = shutit.create_session('bash'
  4.  
  5. session.pause_point('Have a look around!'
  6.  
  7. session.send('echo "Did you enjoy your pause point?"', echo=True 

程序輸出:

  1. $ python example.py 
  2.  
  3. Have a look around! 
  4.  
  5. Ians-Air.home:ORIGIN_ENV:I00LA1Mq# bash 
  6.  
  7. imiell@Ians-Air:/space/git/shutit ⑂ master +  
  8.  
  9. CTRL-] caught, continuing with run... 
  10.  
  11. 2017-06-05 15:12:33,577 INFO: Sending: exit 
  12.  
  13. 2017-06-05 15:12:33,633 INFO: Output (squashed): exitexitIans-Air.home:ORIGIN_ENV:I00LA1Mq# [...] 
  14.  
  15. echo "Did you enjoy your pause point?" 
  16.  
  17. echo "Did you enjoy your pause point?" 
  18.  
  19. Did you enjoy your pause point? 
  20.  
  21. Ians-Air.home:ORIGIN_ENV:I00LA1Mq#  

更復(fù)雜的IO – Backgrounding

回到我們上面的“監(jiān)控多臺(tái)服務(wù)器”的例子上來(lái)。想象一下,我們要在每臺(tái)服務(wù)器上運(yùn)行一個(gè)長(zhǎng)時(shí)間運(yùn)行的任務(wù)。默認(rèn)情況下,ShutIt會(huì)持續(xù)運(yùn)行很長(zhǎng)時(shí)間。但是我們可以在后臺(tái)運(yùn)行任務(wù)來(lái)加快ShutIt的運(yùn)行速度。

在這里,你可以使用簡(jiǎn)單的命令“sleep 60”來(lái)嘗試一個(gè)例子。

  1. import shutit 
  2.  
  3. import time 
  4.  
  5. long_command="""sleep 60""" 
  6.  
  7. session1 = shutit.create_session('bash'
  8.  
  9. session2 = shutit.create_session('bash'
  10.  
  11. password1 = session1.get_input('Password for server1', ispass=True
  12.  
  13. password2 = session2.get_input('Password for server2', ispass=True
  14.  
  15. session1.login('ssh you@one.example.com'user='you'password=password1) 
  16.  
  17. session2.login('ssh you@two.example.com'user='you'password=password2) 
  18.  
  19. start = time.time() 
  20.  
  21. session1.send(long_command, background=True
  22.  
  23. session2.send(long_command, background=True
  24.  
  25. print('That took: ' + str(time.time() - start) + ' seconds to fire'
  26.  
  27. session1.wait() 
  28.  
  29. session2.wait() 
  30.  
  31. print('That took: ' + str(time.time() - start) + ' seconds to complete') 

我的筆記本電腦說,運(yùn)行這兩個(gè)命令只需花費(fèi)0.5秒,而腳本在一分鐘以后才運(yùn)行結(jié)束(使用了’wait’方法)。

雖然這個(gè)例子看起來(lái)是微不足道的,但是想像一下,如果你有數(shù)百臺(tái)這樣的服務(wù)器需要管理,那么你可以看到這幾行代碼和一個(gè)python import所帶來(lái)的強(qiáng)大的力量。

更多信息

ShutIt可以做很多事。更多信息,請(qǐng)參閱:

  • ShutIt (https://ianmiell.github.io/shutit/)
  • GitHub (https://github.com/ianmiell/shutit/blob/master/README.md) 
責(zé)任編輯:龐桂玉 來(lái)源: Python開發(fā)者
相關(guān)推薦

2019-08-14 10:49:20

Python編程語(yǔ)言代碼

2019-04-22 09:00:00

Python框架自動(dòng)化測(cè)試

2023-07-24 10:58:51

CIO自動(dòng)化

2021-04-01 10:51:44

機(jī)器人人工智能編程

2021-06-02 09:32:46

Angular自動(dòng)化功能請(qǐng)求

2020-11-06 08:43:21

AIOps運(yùn)維DevOps

2023-12-21 11:12:31

Node.js.NET開源庫(kù)

2022-04-11 15:34:29

機(jī)器學(xué)習(xí)AutoML開源

2025-01-08 11:00:00

AIshell開源

2024-05-13 16:29:56

Python自動(dòng)化

2018-12-10 08:10:39

2024-11-13 13:14:38

2020-04-28 09:00:00

測(cè)試測(cè)試自動(dòng)化

2022-02-17 13:03:28

Python腳本代碼

2024-06-21 10:46:44

2024-08-16 21:51:42

2017-11-27 15:17:24

PythonMonkey腳本

2021-02-03 09:04:11

解密Spring配置

2022-02-17 10:37:16

自動(dòng)化開發(fā)團(tuán)隊(duì)預(yù)測(cè)

2023-02-11 21:14:58

自動(dòng)化測(cè)試框架
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)