python利用paramiko連接遠(yuǎn)程服務(wù)器執(zhí)行命令的方法
python中的paramiko模塊是用來實(shí)現(xiàn)ssh連接到遠(yuǎn)程服務(wù)器上的庫,在進(jìn)行連接的時(shí)候,可以用來執(zhí)行命令,也可以用來上傳文件。
1、得到一個(gè)連接的對(duì)象
在進(jìn)行連接的時(shí)候,可以使用如下的代碼:
def connect(host):
'this is use the paramiko connect the host,return conn'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# ssh.connect(host,username='root',allow_agent=True,look_for_keys=True)
ssh.connect(host,username='root',password='root',allow_agent=True)
return ssh
except:
return None
在connect函數(shù)中,參數(shù)是一個(gè)主機(jī)的IP地址或者是主機(jī)名稱,在執(zhí)行這個(gè)方法之后,如果成功的連接到服務(wù)器,那么就會(huì)返回一個(gè)sshclient對(duì)象。
第一步是建立一個(gè)SSHClient的對(duì)象,然后設(shè)置ssh客戶端允許連接不在know_host文件中的機(jī)器,然后就嘗試連接服務(wù)器,在連接服務(wù)器的時(shí)候,可以使用兩種方式:一種方式是使用秘鑰的方式,也就是參數(shù)look_for_keys,這里用設(shè)置密碼尋找,也可以直接使用密碼的方式,也就是直接使用參數(shù)password,從而最后返回一個(gè)連接的對(duì)象。
2、 獲取設(shè)置的命令
在進(jìn)行paramiko連接之后,那么必須要得到需要執(zhí)行的命令,如下代碼所示:
def command(args,outpath): 'this is get the command the args to return the command' cmd = '%s %s' % (outpath,args) return cmd
在參數(shù)中,一個(gè)是args,一個(gè)outpath,args表示命令的參數(shù),而outpath表示為可執(zhí)行文件的路徑,例如/usr/bin/ls -l。在其中outpath也就是/usr/bin/ls ,而參數(shù)為-l
這個(gè)方法主要是用來組合命令,將分開的參數(shù)作為命令的一部分進(jìn)行組裝。
3、 執(zhí)行命令
在連接過后,可以進(jìn)行直接執(zhí)行命令,那么就有了如下的函數(shù):
def exec_commands(conn,cmd): 'this is use the conn to excute the cmd and return the results of excute the command' stdin,stdout,stderr = conn.exec_command(cmd) results=stdout.read() return results
在此函數(shù)中,傳入的參數(shù)一個(gè)為連接的對(duì)象conn,一個(gè)為需要執(zhí)行的命令cmd,最后得到執(zhí)行的結(jié)果,也就是stdout.read(),最后返回得到的結(jié)果
4、 上傳文件
在使用連接對(duì)象的時(shí)候,也可以直接進(jìn)行上傳相關(guān)的文件,如下函數(shù):
def copy_moddule(conn,inpath,outpath): 'this is copy the module to the remote server' ftp = conn.open_sftp() ftp.put(inpath,outpath) ftp.close() return outpath
此函數(shù)的主要參數(shù)為,一個(gè)是連接對(duì)象conn,一個(gè)是上傳的文件名稱,一個(gè)上傳之后的文件名稱,在此必須寫入完整的文件名稱包括路徑。
做法主要是打開一個(gè)sftp對(duì)象,然后使用put方法進(jìn)行上傳文件,最后關(guān)閉sftp連接,最后返回一個(gè)上傳的文件名稱的完整路徑
5、 執(zhí)行命令得到結(jié)果
最后就是,執(zhí)行命令,得到返回的結(jié)果,如下代碼:
def excutor(host,outpath,args):
conn = connect(host)
if not conn:
return [host,None]
exec_commands(conn,'chmod +x %s' % outpath)
cmd =command(args,outpath)
result = exec_commands(conn,cmd)
print '%r' % result
result = json.loads(result)
return [host,result]
首先,進(jìn)行連接服務(wù)器,得到一個(gè)連接對(duì)象,如果連接不成功,那么返回主機(jī)名和None,表示沒有連接成功,如果連接成功,那么修改文件的執(zhí)行權(quán)限,從而可以執(zhí)行文件,然后得到執(zhí)行的命令,最后,進(jìn)行執(zhí)行命令,得到結(jié)果,將結(jié)果用json格式表示返回,從而結(jié)果能得到一個(gè)美觀的json格式,最后和主機(jī)名一起返回相關(guān)的信息
6、 測試代碼
測試代碼如下:
if __name__ == '__main__':
print json.dumps(excutor('192.168.1.165','ls',' -l'),indent=4,sort_keys=True)
print copy_module(connect('192.168.1.165'),'kel.txt','/root/kel.1.txt')
exec_commands(connect('192.168.1.165'),'chmod +x %s' % '/root/kel.1.txt')
第一步測試命令執(zhí)行,第二步測試上傳文件,第三部測試修改上傳文件的權(quán)限。
完整代碼如下:
#!/usr/bin/env python
import json
import paramiko
def connect(host):
'this is use the paramiko connect the host,return conn'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# ssh.connect(host,username='root',allow_agent=True,look_for_keys=True)
ssh.connect(host,username='root',password='root',allow_agent=True)
return ssh
except:
return None
def command(args,outpath):
'this is get the command the args to return the command'
cmd = '%s %s' % (outpath,args)
return cmd
def exec_commands(conn,cmd):
'this is use the conn to excute the cmd and return the results of excute the command'
stdin,stdout,stderr = conn.exec_command(cmd)
results=stdout.read()
return results
def excutor(host,outpath,args):
conn = connect(host)
if not conn:
return [host,None]
#exec_commands(conn,'chmod +x %s' % outpath)
cmd =command(args,outpath)
result = exec_commands(conn,cmd)
result = json.dumps(result)
return [host,result]
def copy_module(conn,inpath,outpath):
'this is copy the module to the remote server'
ftp = conn.open_sftp()
ftp.put(inpath,outpath)
ftp.close()
return outpath
if __name__ == '__main__':
print json.dumps(excutor('192.168.1.165','ls',' -l'),indent=4,sort_keys=True)
print copy_module(connect('192.168.1.165'),'kel.txt','/root/kel.1.txt')
exec_commands(connect('192.168.1.165'),'chmod +x %s' % '/root/kel.1.txt')
主要就是使用python中的paramiko模塊通過ssh連接linux服務(wù)器,然后執(zhí)行相關(guān)的命令,并且將文件上傳到服務(wù)器。
以上這篇python利用paramiko連接遠(yuǎn)程服務(wù)器執(zhí)行命令的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)OpenCV中文路徑圖片讀寫的詳細(xì)指南
在Python中使用OpenCV處理圖片時(shí),涉及讀取和保存圖片的操作,可能會(huì)遇到中文路徑的兼容性問題,該指南的目的是展示如何正確處理帶有中文路徑的圖片,并使用OpenCV將圖片保存到指定的中文路徑,需要的朋友可以參考下2025-03-03
Python for循環(huán)通過序列索引迭代過程解析
這篇文章主要介紹了Python for循環(huán)通過序列索引迭代過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
六種酷炫Python運(yùn)行進(jìn)度條效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了六種酷炫Python運(yùn)行進(jìn)度條的實(shí)現(xiàn)代碼,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
你知道怎么改進(jìn)Python 二分法和牛頓迭代法求算術(shù)平方根嗎
這篇文章主要介紹了Python編程實(shí)現(xiàn)二分法和牛頓迭代法求平方根代碼的改進(jìn),具有一定參考價(jià)值,需要的朋友可以了解下,希望能夠給你帶來幫助2021-08-08
安裝Python和pygame及相應(yīng)的環(huán)境變量配置(圖文教程)
下面小編就為大家?guī)硪黄惭bPython和pygame及相應(yīng)的環(huán)境變量配置(圖文教程)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
python調(diào)用自定義函數(shù)的實(shí)例操作
在本文里我們給大家整理了關(guān)于python調(diào)用自定義函數(shù)的實(shí)例操作相關(guān)內(nèi)容,有此需要的朋友們可以學(xué)習(xí)參考下。2019-06-06
Python?OpenCV實(shí)現(xiàn)圖像增強(qiáng)操作詳解
由于很多不確定因素,導(dǎo)致圖像采集的光環(huán)境極其復(fù)雜;為了提高目標(biāo)檢測模型的泛化能力,本文將使用python中的opencv模塊實(shí)現(xiàn)常見的圖像增強(qiáng)方法,感興趣的可以了解一下2022-10-10

