亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python使用Paramiko模塊編寫腳本進行遠程服務器操作

 更新時間:2016年05月05日 18:03:08   作者:larry  
這篇文章主要介紹了Python使用Paramiko模塊編寫腳本進行遠程服務器操作的實例,通過Paramiko能夠方便地使用SSH服務,需要的朋友可以參考下

簡介:
paramiko是python(2.2或更高)的模塊,遵循SSH2協(xié)議實現(xiàn)了安全(加密和認證)連接遠程機器。
安裝所需軟件包:
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.gz
cd pycrypto-2.5
python setup.py build
python setup.py install
tar zxvf paramiko-1.7.7.1.tar.gz
cd paramiko-1.7.7.1
python setup.py build
python 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()

       
腳本二:在遠程服務器上執(zhí)行相應命令

#!/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% /boot
tmpfs    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 -m
total    used    free   shared  buffers   cached
Mem:      114    112     2     0     26     35
-/+ buffers/cache:     50     64
Swap:     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% /boot
tmpfs    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% /boot
tmpfs    tmpfs   60M   0  60M  0% /dev/shm
/dev/sda4   ext3   33G 377M  31G  2% /data

ip.list文件內(nèi)容:

dx   172.16.22.22  22  root  larryroot
wt   172.16.22.23  22  root  larryroot

腳本四:類似于腳本二,在所有遠程服務器上執(zhí)行相應命令

#!/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模塊可以很方便的管理服務器,文件的上傳下載后續(xù)會整理出來。

SSH
下面是通過ssh的dsa或rsa公鑰驗證批量登錄服務器執(zhí)行命令:

#!/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:]) ####獲取命令行參數(shù) 
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()

 
執(zhí)行python腳本:

python sshkey.py df -h
################172.16.22.22########################
Filesystem      Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
14G 3.5G 9.7G 27% /
/dev/mapper/VolGroup00-data
116G  47G  64G 43% /data
/dev/cciss/c0d0p1   99M  13M  82M 14% /boot
tmpfs         5.9G   0 5.9G  0% /dev/shm

相關文章

  • 解決在Python編輯器pycharm中程序run正常debug錯誤的問題

    解決在Python編輯器pycharm中程序run正常debug錯誤的問題

    今天小編就為大家分享一篇解決在Python編輯器pycharm中程序run正常debug錯誤的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 利用Python實時獲取steam特惠游戲數(shù)據(jù)

    利用Python實時獲取steam特惠游戲數(shù)據(jù)

    Steam是由美國電子游戲商Valve于2003年9月12日推出的數(shù)字發(fā)行平臺,被認為是計算機游戲界最大的數(shù)碼發(fā)行平臺之一。本文將利用Python實時獲取steam特惠游戲數(shù)據(jù),感興趣的可以嘗試一下
    2022-06-06
  • python3.5使用tkinter制作記事本

    python3.5使用tkinter制作記事本

    TkInter是標準的Python GUI庫。的Python與Tkinter的結(jié)合提供了一個快速和容易的方法來創(chuàng)建GUI應用程序。 Tkinter的提供了一個強大的面向?qū)ο蟮慕涌赥k的GUI工具包.
    2016-06-06
  • Python?Flask-Login構(gòu)建強大的用戶認證系統(tǒng)實例探究

    Python?Flask-Login構(gòu)建強大的用戶認證系統(tǒng)實例探究

    這篇文章主要為大家介紹了Python?Flask-Login構(gòu)建強大的用戶認證系統(tǒng)示例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 在Python中使用mechanize模塊模擬瀏覽器功能

    在Python中使用mechanize模塊模擬瀏覽器功能

    這篇文章主要介紹了在Python中使用mechanize模塊模擬瀏覽器功能,包括使用cookie和設置代理等功能的實現(xiàn),需要的朋友可以參考下
    2015-05-05
  • python刪除列表元素的三種方法(remove,pop,del)

    python刪除列表元素的三種方法(remove,pop,del)

    這篇文章主要介紹了python刪除列表元素的三種方法(remove,pop,del),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • 基于Python的身份證號碼自動生成程序

    基于Python的身份證號碼自動生成程序

    今天收到一個小需求:需要一個自動生成身份證號碼的小程序。近期用python較多,因此打算用python實現(xiàn)
    2014-08-08
  • python中的斷言(assert語句)

    python中的斷言(assert語句)

    這篇文章主要介紹了python中的斷言(assert語句),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python 利用4行代碼實現(xiàn)圖片灰度化的項目實踐

    Python 利用4行代碼實現(xiàn)圖片灰度化的項目實踐

    灰度處理是將彩色圖像轉(zhuǎn)換為灰度圖像的過程,即每個像素的顏色由紅、綠、藍三個通道的值組成,轉(zhuǎn)換為一個單一的灰度值,本文主要介紹了Python 利用4行代碼實現(xiàn)圖片灰度化的項目實踐,感興趣的可以了解一下
    2024-04-04
  • Python列出一個文件夾及其子目錄的所有文件

    Python列出一個文件夾及其子目錄的所有文件

    這篇文章主要介紹了Python列出一個文件夾及其子目錄的所有文件的方法,和python列出文件夾下的所有文件的四種方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06

最新評論