本篇文章主要介紹了python的paramiko模組實現遠端控制和傳輸範例,非常具有實用價值,需要的朋友可以參考下
本文介紹了python的paramiko模組實現遠端控制和傳輸範例,分享給大家,如下:
1 安裝
sudo pip install paramiko
2 ssh實作遠端控制
#LINUX下执行shell ssh username@ip #输入密码后就可以对远程机器进行操作 ssh username@ip command #输入密码后远程机器就执行command
ssh執行後,想退出,可以kill掉ssh行程。
3 paramiko實作ssh
import paramiko hostname = '10.1.111.111' username = 'root' password = '111111' port = 22 #整数不是字符串 paramiko.util.log_to_file('paramiko.log') ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #允许连接不在know_hosts文件中的主机 ssh.connect(hostname=hostname,port=port,username=username,password=password) stdin, stdout, stderr = ssh.exec_command("ls") #远程执行shell命令 print(stdout.readlines()) #输出回显结果 ssh.close()
exec_command指令,以分號;分隔表示先後執行兩個指令;可以傳入多個參數; exec_command為單一會話,執行完成之後會回到登入時的缺省目錄,如下:
a='~/Videos' b='aaa' ssh.exec_command('cd %s;mkdir %s'%(a,b)) #aaa在a目录下 ssh.exec_command('mkdir aaa') #aaa在缺省目录下
遠端執行指令時如果有交互,可以這樣用stdin.write(“”)來完成。
注意:
(1)sudo後面要加-S,表示從stdin接收密碼;
(2)stdin.write('password\n')最後要加\n作為指令的結束,否則伺服器一直等待;
(3) flush()寫入的緩衝( flush() any buffer you're writing to )
stdin, stdout, stderr = ssh.exec_command('sudo -S ls') stdin.write('password\n') stdin.flush()
可以利用多進程或執行緒可以批次執行命令:
import paramiko import threading def ssh_cmd(ip,port,username,passwd,cmd): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(ip,port,username,passwd) for m in cmd: stdin, stdout, stderr = ssh.exec_command(m) print(stdout.readlines()) ssh.close() if __name__=='__main__': cmd = ['ls','ifconfig'] a=threading.Thread(target=ssh_cmd,args=(ip,port,username,passwd,cmd)) a.start() a.join()
4 遠端傳輸檔案
scp從本機伺服器複製到遠端伺服器
scp local_file remote_username@remote_ip:remote_file
指定了用戶名,命令執行後需要輸入用戶密碼;如果不指定用戶名,命令執行後需要輸入使用者名稱和密碼; 從遠處複製檔案到本地
scp remote_username@remote_ip:remote_file local_file
5 paramiko實現遠端傳輸檔案
新建一個SFTPClient對象,該物件重複使用之前的SSH連接,因此,我們使用sftp傳輸檔案時,不需要再次進行使用者認證。實作檔案
上傳:
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport()) #sftp = ssh.open_sftp() #两者选其一即可 sftp.put('bbb.c','aaa.c') #文件上传并重命名 sftp.close()
或以下實作檔案上傳與下載:
import paramiko t=paramiko.Transport((hostname,port)) t.connect(username=username,password=password) sftp = paramiko.SFTPClient.from_transport(t) sftp.put('bbb.c','aaa.c') #文件上传并重命名 sftp.get('m.py', 'mm.py')#文件下载并重命名 t.close()
官方文件
以上是python中paramiko模組實現遠端控制以及傳輸的範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

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

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具