简介:
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

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

WebStorm Mac版
好用的JavaScript開發工具