python的paramiko模塊實現(xiàn)遠程控制和傳輸示例
本文介紹了python的paramiko模塊實現(xiàn)遠程控制和傳輸示例,分享給大家,具體如下:
1 安裝
sudo pip install paramiko
2 ssh實現(xiàn)遠程控制
#LINUX下執(zhí)行shell ssh username@ip #輸入密碼后就可以對遠程機器進行操作 ssh username@ip command #輸入密碼后遠程機器就執(zhí)行command
ssh運行后,想退出,可以kill掉ssh進程。
3 paramiko實現(xiàn)ssh
import paramiko
hostname = '10.1.111.111'
username = 'root'
password = '111111'
port = 22 #整數(shù)不是字符串
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") #遠程執(zhí)行shell命令
print(stdout.readlines()) #輸出回顯結(jié)果
ssh.close()
exec_command命令,以分號;分隔表示先后執(zhí)行兩個命令;可以傳入多個參數(shù); exec_command為單個會話,執(zhí)行完成之后會回到登錄時的缺省目錄,如下:
a='~/Videos'
b='aaa'
ssh.exec_command('cd %s;mkdir %s'%(a,b)) #aaa在a目錄下
ssh.exec_command('mkdir aaa') #aaa在缺省目錄下
遠端執(zhí)行命令時如果有交互,可以這樣用 stdin.write(“”)來完成。
注意:
(1)sudo后要加-S,表示從stdin接收密碼;
(2)stdin.write(‘password\n')最后要加\n作為命令的結(jié)束,否則服務(wù)器一直等待;
(3) flush()寫入的緩沖( flush() any buffer you're writing to )
stdin, stdout, stderr = ssh.exec_command('sudo -S ls')
stdin.write('password\n')
stdin.flush()
可以利用多進程或線程可以批量執(zhí)行命令:
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從本地服務(wù)器復(fù)制到遠程服務(wù)器
scp local_file remote_username@remote_ip:remote_file
指定了用戶名,命令執(zhí)行后需要輸入用戶密碼;如果不指定用戶名,命令執(zhí)行后需要輸入用戶名和密碼; 從遠處復(fù)制文件到本地
scp remote_username@remote_ip:remote_file local_file
5 paramiko實現(xiàn)遠程傳輸文件
新建一個SFTPClient對象,該對象復(fù)用之前的SSH連接,因此,我們使用sftp傳輸文件時,不需要再次進行用戶認證。實現(xiàn)文件
上傳:
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
#sftp = ssh.open_sftp() #兩者選其一即可
sftp.put('bbb.c','aaa.c') #文件上傳并重命名
sftp.close()
或者以下實現(xiàn)文件上傳和下載:
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()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
一文帶你了解Python中不同數(shù)據(jù)對象的空值校驗方法
空值校驗在數(shù)據(jù)處理和應(yīng)用程序開發(fā)中是一個非常重要的任務(wù),Python提供了多種方式來檢查不同數(shù)據(jù)對象(如字符串、列表、字典、集合等)是否為空或包含空值,下面就跟隨小編一起來學習一下吧2024-01-01
零基礎(chǔ)寫python爬蟲之urllib2中的兩個重要概念:Openers和Handlers
文章首先介紹了urllib2的2個方法,然后詳細介紹了urllib2中的2個重要概念Openers和Handlers的相關(guān)知識,希望能對大家有所幫助2014-11-11
python循環(huán)控制之break和continue流程控制語句
這篇文章主要介紹了python循環(huán)控制之break流程控制語句,Python中提供了兩個關(guān)鍵字用來控制循環(huán)語句,分別是break和continue,本文都有介紹,需要的朋友可以參考一下2022-03-03
Python基于多線程實現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫的方法
這篇文章主要介紹了Python基于多線程實現(xiàn)抓取數(shù)據(jù)存入數(shù)據(jù)庫的方法,結(jié)合實例形式分析了Python使用數(shù)據(jù)庫類與多線程類進行數(shù)據(jù)抓取與寫入數(shù)據(jù)庫操作的具體使用技巧,需要的朋友可以參考下2018-06-06
python微信公眾號之關(guān)注公眾號自動回復(fù)
這篇文章主要為大家詳細介紹了python微信公眾號之關(guān)注公眾號自動回復(fù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
python分析實現(xiàn)微信釘釘?shù)溶浖嚅_分身
我發(fā)現(xiàn)壇友分享的很多都是通過cmd?去start?多個微信,雖然能實現(xiàn)多開,但不夠靈活,比如我上午登錄了一個微信,下午在登錄就不太好用了,當然也可能是我start的姿勢不對。于是我就搜了下單實例原理,自己動手實現(xiàn)了個隨用隨開的2022-02-02
Python常見庫matplotlib學習筆記之畫圖中各個模塊的含義及修改方法
matplotlib是python最著名的繪圖庫,它提供了一整套和matlab相似的命令A(yù)PI,十分適合交互式地進行制圖,下面這篇文章主要給大家介紹了關(guān)于Python常見庫matplotlib學習筆記之畫圖中各個模塊的含義及修改方法的相關(guān)資料,需要的朋友可以參考下2023-05-05

