Pexpect 是 Don Libes 的 Expect 語言的一個 Python 實現,是一個用來啓動子程序,並使用正則表達式對程序輸出作出特定響應,以此實現與其自動交互的 Python 模塊。 Pexpect 的使用範圍很廣,能夠用來實現與 ssh、ftp 、telnet 等程序的自動交互;能夠用來自動複製軟件安裝包並在不一樣機器自動安裝;還能夠用來實現軟件測試中與命令行交互的自動化。python
import pexpect import sys child = pexpect.spawn('ssh std20@123.57.211.212') child.logfile = sys.stdout #fout = file('mylog.txt', 'w') #child.logfile = fout child.expect('password:') child.sendline('std20') child.expect('std20.*') child.sendline('ls /') child.expect('std20.*') child.sendline('exit') #coding=utf_8 from pexpect import pxssh import sys s = pxssh.pxssh() s.logfile = sys.stdout hostname = '123.57.211.212' username = 'std20' password = 'std20' s.login(hostname, username, password) s.sendline('ls /') s.prompt() #匹配系統提示符 s.sendline('whoami') s.prompt() s.logout()
項目發佈和運維的工做至關機械,頻率還蠻高,致使時間浪費在敲大量重複的命令上。git
修復bug什麼的,測試,提交版本庫(2分鐘),ssh到測試環境pull部署(2分鐘),rsync到線上機器A,B,C,D,E(1分鐘),分別ssh到ABCDE五臺機器,逐一重啓(8-10分鐘) = 13-15分鐘正則表達式
其中鬱悶的是,每次操做都是相同的,命令同樣,要命的是在多個機器上,很難在本機一個腳本搞定,主要時間都浪費在ssh,敲命令上了,寫成腳本,徹底能夠一鍵執行,花兩分鐘看下執行結果。ubuntu
pip install fabric
#fabfile.py from fabric.api import run def host_type(): run('uname -s')
啓動api
itcast@ubuntu:~/tmp/fab$ fab -H 127.0.0.1 host_type [127.0.0.1] Executing task 'host_type' [127.0.0.1] run: uname -s [127.0.0.1] Login password for 'itcast': [127.0.0.1] out: Linux [127.0.0.1] out: Done. Disconnecting from 127.0.0.1... done. itcast@ubuntu:~/tmp/fab$ fab -H 127.0.0.1 host_type [127.0.0.1] Executing task 'host_type' [127.0.0.1] run: uname -s [127.0.0.1] Login password for 'itcast': [127.0.0.1] out: Linux [127.0.0.1] out:
from fabric.api import * env.hosts=['192.168.17.192', '192.168.17.193'] #env.password='python' env.passwords = { 'itcast@192.168.17.192:22':'python', 'itcast@192.168.17.193:22':'python', } @runs_once def input_raw(): return prompt("please input directory name:", default="/home") def workask(dirname): run('ls -l ' + dirname) @task def go(): print('start ...') getdirname = input_raw() workask(getdirname) print('end ...')
from fabric.api import * env.user = 'itcast' env.hosts = ['192.168.17.192', '192.168.17.193'] env.password = 'python' @task @runs_once def tar_task(): with lcd('/home/itcast/testdemo'): local('tar zcvf demo.tar.gz demo.py') @task def put_task(): run('mkdir -p /home/itcast/testdemo') with cd('/home/itcast/testdemo'): put('/home/itcast/testdemo/demo.tar.gz', '/home/itcast/testdemo/demo.tar.gz') @task def check_task(): lmd5 = local('md5sum /home/itcast/testdemo/demo.tar.gz', capture=True).split(' ')[0] rmd5 = run('md5sum /home/itcast/testdemo/demo.tar.gz').split(' ')[0] if lmd5 == rmd5: print('OK ...') else: print('ERROR ...') @task def run_task(): with cd('/home/itcast/testdemo'): run('tar zxvf demo.tar.gz') run('python demo.py') @task def go(): tar_task() put_task() check_task() run_task()
from fabric.api import * env.user = 'itcast' env.hosts = ['192.168.17.192', '192.168.17.193'] env.password = 'python' @runs_once @task def local_update(): with lcd("/home/itcast/tmp/itcasthello"): local("git add -A") local("git commit -m 'update'") local("git pull origin master") local("git push origin master") @task def remote_update(): with cd("/home/itcast/tmp/itcasthello"): run("git checkout master") run("git pull origin master") @task def deploy(): local_update() remote_update()