国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python利用paramiko連接遠程服務器執行命令的方法

2020-01-04 16:35:48
字體:
來源:轉載
供稿:網友

python中的paramiko模塊是用來實現ssh連接到遠程服務器上的庫,在進行連接的時候,可以用來執行命令,也可以用來上傳文件。

1、得到一個連接的對象

在進行連接的時候,可以使用如下的代碼:

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函數中,參數是一個主機的IP地址或者是主機名稱,在執行這個方法之后,如果成功的連接到服務器,那么就會返回一個sshclient對象。

第一步是建立一個SSHClient的對象,然后設置ssh客戶端允許連接不在know_host文件中的機器,然后就嘗試連接服務器,在連接服務器的時候,可以使用兩種方式:一種方式是使用秘鑰的方式,也就是參數look_for_keys,這里用設置密碼尋找,也可以直接使用密碼的方式,也就是直接使用參數password,從而最后返回一個連接的對象。

2、 獲取設置的命令

在進行paramiko連接之后,那么必須要得到需要執行的命令,如下代碼所示:

def command(args,outpath):  'this is get the command the args to return the command'  cmd = '%s %s' % (outpath,args)  return cmd

在參數中,一個是args,一個outpath,args表示命令的參數,而outpath表示為可執行文件的路徑,例如/usr/bin/ls -l。在其中outpath也就是/usr/bin/ls ,而參數為-l

這個方法主要是用來組合命令,將分開的參數作為命令的一部分進行組裝。

3、 執行命令

在連接過后,可以進行直接執行命令,那么就有了如下的函數:

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

在此函數中,傳入的參數一個為連接的對象conn,一個為需要執行的命令cmd,最后得到執行的結果,也就是stdout.read(),最后返回得到的結果

4、 上傳文件

在使用連接對象的時候,也可以直接進行上傳相關的文件,如下函數:

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

此函數的主要參數為,一個是連接對象conn,一個是上傳的文件名稱,一個上傳之后的文件名稱,在此必須寫入完整的文件名稱包括路徑。

做法主要是打開一個sftp對象,然后使用put方法進行上傳文件,最后關閉sftp連接,最后返回一個上傳的文件名稱的完整路徑

5、 執行命令得到結果

最后就是,執行命令,得到返回的結果,如下代碼:

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]

首先,進行連接服務器,得到一個連接對象,如果連接不成功,那么返回主機名和None,表示沒有連接成功,如果連接成功,那么修改文件的執行權限,從而可以執行文件,然后得到執行的命令,最后,進行執行命令,得到結果,將結果用json格式表示返回,從而結果能得到一個美觀的json格式,最后和主機名一起返回相關的信息

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')

第一步測試命令執行,第二步測試上傳文件,第三部測試修改上傳文件的權限。

完整代碼如下:

#!/usr/bin/env pythonimport jsonimport paramikodef 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 Nonedef command(args,outpath):  'this is get the command the args to return the command'  cmd = '%s %s' % (outpath,args)  return cmddef 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 resultsdef 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 outpathif __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服務器,然后執行相關的命令,并且將文件上傳到服務器。

以上這篇python利用paramiko連接遠程服務器執行命令的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南通市| 来凤县| 梧州市| 中山市| 屏山县| 湘潭市| 勐海县| 小金县| 德惠市| 井陉县| 泾源县| 广宁县| 武夷山市| 南阳市| 和顺县| 沙田区| 唐河县| 光山县| 博乐市| 乌恰县| 大悟县| 天祝| 樟树市| 巨野县| 馆陶县| 阳朔县| 磐石市| 邹城市| 元阳县| 伽师县| 巨野县| 嘉祥县| 嵊泗县| 大兴区| 民丰县| 会东县| 新绛县| 旬邑县| 新乡市| 岚皋县| 铜梁县|