python使用paramiko模块编写脚本进行远程服务器操作

简介:
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.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()

脚本二:在远程服务器上执行相应命令

#!/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文件内容:

dx 172.16.22.22 22 root larryroot
wt 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-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

Posted in 未分类