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
tarzxvfpycrypto-2.5.tar.gz cdpycrypto-2.5 pythonsetup.pybuild pythonsetup.pyinstall tarzxvfparamiko-1.7.7.1.tar.gz cdparamiko-1.7.7.1 pythonsetup.pybuild pythonsetup.pyinstall
脚本简单编写:
管理单台服务器:
脚本一:查询172.16.22.23磁盘使用情况
#!/usr/bin/python importparamiko 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") printstdout.read() s.close()
脚本二:在远程服务器上执行相应命令
#!/usr/bin/python #bylarry #2011/01/30 importsys importparamiko 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) printstdout.read() s.close()
使用方法:
pythonsingle1.pyip地址命令 [root@localhost~]#pythonsingle1.py172.16.22.23df-TH
FilesystemTypeSizeUsedAvailUse%Mountedon /dev/sda2ext313G6.0G5.7G52%/ /dev/sda1ext3104M12M87M13%/boot tmpfstmpfs61M061M0%/dev/shm /dev/sda4ext37.6G465M6.8G7%/data /dev/sdb1ext332G5.9G25G20%/autocd [root@localhost~]#pythonsingle1.py172.16.22.23free-m totalusedfreesharedbufferscached Mem:114112202635 -/+buffers/cache:5064 Swap:102701027
脚本三:管理多台服务器:批量查询ip列表中对应服务器的磁盘使用情况
#!/usr/bin/python #bylarry #2011/01/30 importparamiko port=22 username="root" file=open("ip.list") forlineinfile: 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") printstdout.read() s.close() file.close()
用法:
[root@localhost~]#pythonssh.py
############################172.16.22.22######################## FilesystemTypeSizeUsedAvailUse%Mountedon /dev/sda2ext312G5.6G5.3G52%/ /dev/sda1ext399M12M83M13%/boot tmpfstmpfs58M058M0%/dev/shm /dev/sda4ext37.1G443M6.3G7%/data /dev/sdb1ext330G5.5G23G20%/autocd ############################172.16.22.23######################## FilesystemTypeSizeUsedAvailUse%Mountedon /dev/sda2ext315G2.6G11G19%/ /dev/sda1ext399M12M82M13%/boot tmpfstmpfs60M060M0%/dev/shm /dev/sda4ext333G377M31G2%/data
ip.list文件内容:
dx172.16.22.2222rootlarryroot wt172.16.22.2322rootlarryroot
脚本四:类似于脚本二,在所有远程服务器上执行相应命令
#!/usr/bin/python #bylarry #2011/01/30 importparamiko importsys port=22 username="root" command="".join(sys.argv[1:]) file=open("ip.list") forlineinfile: 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) printstdout.read() s.close() file.close()
用法:
pythonssh.py命令
简单整理到这里通过python的paramiko模块可以很方便的管理服务器,文件的上传下载后续会整理出来。
SSH
下面是通过ssh的dsa或rsa公钥验证批量登录服务器执行命令:
#!/usr/bin/python #2012/02/02bylarry importparamiko importsys,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") forlineinfile: 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) printstdout.read().strip() s.close() file.close()
执行python脚本:
pythonsshkey.pydf-h
################172.16.22.22######################## FilesystemSizeUsedAvailUse%Mountedon /dev/mapper/VolGroup00-LogVol00 14G3.5G9.7G27%/ /dev/mapper/VolGroup00-data 116G47G64G43%/data /dev/cciss/c0d0p199M13M82M14%/boot tmpfs5.9G05.9G0%/dev/shm