Python Paramiko實(shí)現(xiàn)SSHv2協(xié)議輕松管理遠(yuǎn)程服務(wù)器
前言
Python的Paramiko庫,它是一個(gè)用于實(shí)現(xiàn)SSHv2協(xié)議的客戶端和服務(wù)器的庫。通過使用Paramiko,我們可以在Python程序中輕松地實(shí)現(xiàn)遠(yuǎn)程服務(wù)器的管理、文件傳輸?shù)裙δ?。特別做智能硬件產(chǎn)品的同學(xué)要熟悉它,因?yàn)樗転槟銣p少很多麻煩,在我以前的工作中,對(duì)智能硬件做各種測(cè)試就是通過Paramiko連接設(shè)備下發(fā)各種命令
一.安裝
pip install paramiko
二.基本用法
1.創(chuàng)建一個(gè)ssh連接客戶端
import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
2.添加服務(wù)器的SSH密鑰(可選)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
3.connect()創(chuàng)建連接,連接遠(yuǎn)程服務(wù),各種智能硬件,服務(wù)器等
hostname = '192.168.31.230' port = 22 username = 'kali' password = 'kali' ssh.connect(hostname, port, username, password)
4.執(zhí)行遠(yuǎn)程命令
stdin, stdout, stderr = ssh.exec_command('ls')
print(stdout.read().decode()) # 打印輸出結(jié)果
print(stderrh.read().decode())# 輸出錯(cuò)誤信息
# 返回結(jié)果
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos5.文件下載
local_file = 'D:/code/ssh/local_file.txt' # 本地文件路徑和名稱 remote_file = '/home/kali/remote_file.txt' # 遠(yuǎn)程文件路徑和名稱 sftp = ssh.open_sftp() # 打開SFTP會(huì)話 sftp.get(remote_file, local_file) # 從遠(yuǎn)程服務(wù)器下載文件到本地 sftp.close() # 關(guān)閉SFTP會(huì)話

6.文件上傳
local_file = 'D:/code/ssh/local_file2.txt' # 本地文件路徑和名稱 remote_file = '/home/kali/remote_file2.txt' # 遠(yuǎn)程文件路徑和名稱 sftp = ssh.open_sftp() # 打開SFTP會(huì)話 sftp.put(local_file, remote_file) # 上傳本地文件到遠(yuǎn)程服務(wù)器 sftp.close() # 關(guān)閉SFTP會(huì)話

7.關(guān)閉連接
ssh.close() #關(guān)閉連接
三.完整代碼如下
import paramiko
ssh = paramiko.SSHClient() # 創(chuàng)建客服端
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 添加秘鑰
hostname = '192.168.31.230'
port = 22
username = 'kali'
password = 'kali'
ssh.connect(hostname, port, username, password) # 創(chuàng)建連接
stdin, stdout, stderr = ssh.exec_command('ls') # 執(zhí)行l(wèi)s
print(stdout.read().decode())
# 下載
local_file = 'D:/code/ssh/local_file.txt' # 本地文件路徑和名稱
remote_file = '/home/kali/remote_file.txt' # 遠(yuǎn)程文件路徑和名稱
sftp = ssh.open_sftp() # 打開SFTP會(huì)話
sftp.get(remote_file, local_file) # 從遠(yuǎn)程服務(wù)器下載文件到本地
# 上傳
local_file2 = 'D:/code/ssh/local_file2.txt' # 本地文件路徑和名稱
remote_file2 = '/home/kali/remote_file2.txt' # 遠(yuǎn)程文件路徑和名稱
sftp = ssh.open_sftp() # 打開SFTP會(huì)話
sftp.put(local_file2, remote_file2) # 上傳本地文件到遠(yuǎn)程服務(wù)器
sftp.close() # 關(guān)閉SFTP會(huì)話
ssh.close() # 關(guān)閉連接以上就是Python Paramiko實(shí)現(xiàn)SSHv2協(xié)議輕松管理遠(yuǎn)程服務(wù)器的詳細(xì)內(nèi)容,更多關(guān)于Python Paramiko SSHv2協(xié)議的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談pytorch grad_fn以及權(quán)重梯度不更新的問題
今天小編就為大家分享一篇淺談pytorch grad_fn以及權(quán)重梯度不更新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python3.x爬蟲下載網(wǎng)頁圖片的實(shí)例講解
今天小編就為大家分享一篇Python3.x爬蟲下載網(wǎng)頁圖片的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python Pygal庫生成SVG(可縮放矢量圖形)圖表示例
這篇文章主要為大家介紹了python Pygal庫生成SVG(可縮放矢量圖形)圖表示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python使用pyecharts創(chuàng)建交互式圖表的示例代碼
本文詳細(xì)介紹了如何使用Python的策略庫創(chuàng)建折線圖、柱狀圖、餅圖和散點(diǎn)圖,并演示了安裝、基礎(chǔ)圖表制作和配置,適合數(shù)據(jù)分析師和開發(fā)者學(xué)習(xí)數(shù)據(jù)可視化技術(shù),需要的朋友可以參考下2025-12-12
Django中select_related查詢優(yōu)化的具體實(shí)現(xiàn)
本文介紹了Django ORM中的N+1查詢問題及其解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-12-12

