Paramiko是Python的一個SSH客戶端和服務(wù)器庫,可以實現(xiàn)SSHv2協(xié)議的加密和身份驗證功能,支持SFTP和SCP協(xié)議。在本文中,我們將介紹如何使用Paramiko庫來建立SSH連接、執(zhí)行遠程命令、傳輸文件等。
安裝Paramiko
要使用Paramiko庫,我們需要先安裝它。可以使用pip命令進行安裝:
pipinstallparamiko
建立SSH連接
使用Paramiko庫建立SSH連接非常簡單,只需指定主機名、用戶名和密碼即可。以下是一個簡單的示例:
importparamiko ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('hostname',username='username',password='password') stdin,stdout,stderr=ssh.exec_command('ls') print(stdout.read().decode()) ssh.close()
在上述示例中,我們首先創(chuàng)建一個SSHClient對象,并將主機名、用戶名和密碼作為參數(shù)傳遞給connect()方法。然后,使用exec_command()方法執(zhí)行l(wèi)s命令,并將結(jié)果打印到控制臺。最后,我們關(guān)閉SSH連接。
執(zhí)行遠程命令
使用Paramiko庫執(zhí)行遠程命令也很簡單,只需將命令作為字符串傳遞給exec_command()方法。以下是一個示例:
importparamiko ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('hostname',username='username',password='password') command='ls-l' stdin,stdout,stderr=ssh.exec_command(command) forlineinstdout.readlines(): print(line.strip()) ssh.close()
在上述示例中,我們使用exec_command()方法執(zhí)行l(wèi)s -l命令,并使用readlines()方法讀取輸出結(jié)果,并打印到控制臺。需要注意的是,在某些情況下,需要使用strip()方法刪除輸出結(jié)果中的換行符。
傳輸文件
使用Paramiko庫傳輸文件也很簡單,只需使用SFTPClient類即可。以下是一個示例:
importparamiko ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('hostname',username='username',password='password') sftp=ssh.open_sftp() sftp.put('local_file_path','remote_file_path') sftp.get('remote_file_path','local_file_path') sftp.close() ssh.close()
在上述示例中,我們首先創(chuàng)建一個SFTPClient對象,并使用put()方法將本地文件傳輸?shù)竭h程主機上。然后,使用get()方法將遠程文件傳輸?shù)奖镜刂鳈C上。最后,我們關(guān)閉SFTPClient和SSH連接。
使用密鑰認證
除了使用用戶名和密碼進行身份驗證外,我們還可以使用SSH密鑰進行身份驗證。以下是一個示例:
importparamiko ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('hostname',username='username',pkey=key) stdin,stdout,stderr=ssh.exec_command('ls') print(stdout.read().decode()) ssh.close()
在上述示例中,我們首先使用from_private_key_file()方法加載SSH私鑰文件,并將私鑰作為參數(shù)傳遞給connect()方法。然后,我們使用exec_command()方法執(zhí)行l(wèi)s命令,并將結(jié)果打印到控制臺。最后,我們關(guān)閉SSH連接。
高級選項
Paramiko庫還提供了許多高級選項,例如超時、端口轉(zhuǎn)發(fā)、密鑰管理等。以下是一個示例:
importparamiko ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('hostname',username='username',password='password',port=22,timeout=10) #開啟端口轉(zhuǎn)發(fā) transport=ssh.get_transport() local_port=8080 remote_host='google.com' remote_port=80 transport.request_port_forward('',local_port,remote_host,remote_port) #密鑰管理 key=paramiko.RSAKey.generate(2048) private_key_string=key.export_key('PEM') public_key_string=key.get_base64() ssh.close()
在上述示例中,我們首先使用connect()方法連接到遠程主機,并指定了端口號和超時時間。然后,我們使用get_transport()方法獲取SSH傳輸對象,并使用request_port_forward()方法開啟端口轉(zhuǎn)發(fā)。最后,我們使用generate()方法生成RSA密鑰,并使用export_key()和get_base64()方法導(dǎo)出私鑰和公鑰。
結(jié)論
在本文中,我們介紹了如何使用Python Paramiko庫實現(xiàn)SSH客戶端和服務(wù)器功能,包括建立SSH連接、執(zhí)行遠程命令、傳輸文件等。此外,我們還介紹了如何使用SSH密鑰進行身份驗證,并討論了一些高級選項。使用Paramiko庫,我們可以輕松地編寫Python腳本來管理遠程服務(wù)器和網(wǎng)絡(luò)設(shè)備。
審核編輯:劉清
-
服務(wù)器
+關(guān)注
關(guān)注
12文章
9400瀏覽量
86417 -
SSH
+關(guān)注
關(guān)注
0文章
190瀏覽量
16481 -
python
+關(guān)注
關(guān)注
56文章
4812瀏覽量
85233 -
SCP
+關(guān)注
關(guān)注
0文章
29瀏覽量
9340
原文標題:網(wǎng)絡(luò)工程師學Python-34-SSH客戶端和服務(wù)器庫Paramiko
文章出處:【微信號:網(wǎng)絡(luò)技術(shù)干貨圈,微信公眾號:網(wǎng)絡(luò)技術(shù)干貨圈】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論