Python使用Paramiko模組編寫指令碼進行遠程伺服器操作

來源:互聯網
上載者:User
簡介:
paramiko是python(2.2或更高)的模組,遵循SSH2協議實現了安全(加密和認證)串連遠程機器。
安裝所需軟體包:
http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.5.tar.gz
http://www.lag.net/paramiko/download/paramiko-1.7.7.1.tar.gz

tar zxvf pycrypto-2.5.tar.gzcd pycrypto-2.5python setup.py buildpython setup.py installtar zxvf paramiko-1.7.7.1.tar.gzcd paramiko-1.7.7.1python setup.py buildpython setup.py install

指令碼簡單編寫:
管理單台伺服器:

指令碼一:查詢172.16.22.23磁碟使用方式

#!/usr/bin/python import paramiko hostname="172.16.22.23" port=22 username="root" password="larryroot" if __name__=="__main__":     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.connect(hostname,port,username,password)     stdin,stdout,sterr=s.exec_command("df -Th")     print stdout.read()     s.close()


指令碼二:在遠程伺服器上執行相應命令

#!/usr/bin/python #by larry #2011/01/30 import sys import paramiko  hostname=sys.argv[1] command = " ".join(sys.argv[2:]) port=22 username="root" password="larryroot" if __name__=="__main__":     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.connect(hostname,port,username,password)     stdin,stdout,sterr=s.exec_command(command)     print stdout.read()     s.close()

使用方法:

python single1.py ip地址  命令[root@localhost ~]# python single1.py 172.16.22.23 df -TH

Filesystem  Type   Size  Used Avail Use% Mounted on/dev/sda2   ext3   13G  6.0G  5.7G 52% //dev/sda1   ext3   104M  12M  87M 13% /boottmpfs    tmpfs   61M   0  61M  0% /dev/shm/dev/sda4   ext3   7.6G  465M  6.8G  7% /data/dev/sdb1   ext3   32G  5.9G  25G 20% /autocd[root@localhost ~]# python single1.py 172.16.22.23 free -mtotal    used    free   shared  buffers   cachedMem:      114    112     2     0     26     35-/+ buffers/cache:     50     64Swap:     1027     0    1027

指令碼三:管理多台伺服器:批量查詢ip列表中對應伺服器的磁碟使用方式

#!/usr/bin/python #by larry #2011/01/30 import paramiko port=22 username="root" file=open("ip.list") for line in file:     hostname=str(line.split("\t")[1])     password=str(line.split("\t")[4]).strip()     print "##########################",hostname,"########################"     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.connect(hostname,port,username,password)     stdin,stdout,sterr=s.exec_command("df -Th")     print stdout.read()     s.close() file.close()

用法:

[root@localhost ~]# python ssh.py

############################ 172.16.22.22 ########################Filesystem  Type  Size Used Avail Use% Mounted on/dev/sda2   ext3   12G 5.6G 5.3G 52% //dev/sda1   ext3   99M  12M  83M 13% /boottmpfs    tmpfs   58M   0  58M  0% /dev/shm/dev/sda4   ext3  7.1G 443M 6.3G  7% /data/dev/sdb1   ext3   30G 5.5G  23G 20% /autocd############################ 172.16.22.23 ########################Filesystem  Type  Size Used Avail Use% Mounted on/dev/sda2   ext3   15G 2.6G  11G 19% //dev/sda1   ext3   99M  12M  82M 13% /boottmpfs    tmpfs   60M   0  60M  0% /dev/shm/dev/sda4   ext3   33G 377M  31G  2% /data

ip.list檔案內容:

dx   172.16.22.22  22  root  larryrootwt   172.16.22.23  22  root  larryroot

指令碼四:類似於指令碼二,在所有遠程伺服器上執行相應命令

#!/usr/bin/python #by larry #2011/01/30 import paramiko import sys port=22 username="root" command = " ".join(sys.argv[1:]) file=open("ip.list") for line in file:     hostname=str(line.split("\t")[1])     password=str(line.split("\t")[4]).strip()     print "##################",hostname,"######################"     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.connect(hostname,port,username,password)     stdin,stdout,sterr=s.exec_command(command)     print stdout.read()     s.close() file.close()


用法:

python ssh.py 命令

簡單整理到這裡通過python的paramiko模組可以很方便的管理伺服器,檔案的上傳下載後續會整理出來。

SSH
下面是通過ssh的dsa或rsa公開金鑰驗證批量登入伺服器執行命令:

#!/usr/bin/python #2012/02/02 by larry import paramiko import sys,os port=22 username="larry" key_file="~/.ssh/authorized_keys" know_host="/home/larry/.ssh/known_hosts" command=" ".join(sys.argv[1:]) ####擷取命令列參數 file=open("ip.list") for line in file:     hostname=str(line.split(" ")[1]) ####截取ip欄位     print "#####################################",hostname,"###############################################"     s=paramiko.SSHClient()     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     s.load_system_host_keys(know_host)     s.connect(hostname,port,username,key_file)     stdin,stdout,sterr=s.exec_command(command)     print stdout.read().strip()     s.close() file.close()


執行python指令碼:

python sshkey.py df -h

################172.16.22.22########################Filesystem      Size Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol0014G 3.5G 9.7G 27% //dev/mapper/VolGroup00-data116G  47G  64G 43% /data/dev/cciss/c0d0p1   99M  13M  82M 14% /boottmpfs         5.9G   0 5.9G  0% /dev/shm
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.