python paramiko連接ssh實(shí)現(xiàn)命令
paramiko
paramiko是一個(gè)用于做遠(yuǎn)程控制的模塊,使用該模塊可以對(duì)遠(yuǎn)程服務(wù)器進(jìn)行命令或文件操作,值得一說(shuō)的是,fabric和ansible內(nèi)部的遠(yuǎn)程管理就是使用的paramiko來(lái)現(xiàn)實(shí)。
安裝
pip install paramiko
模塊使用
執(zhí)行命令—用戶名+密碼
#!/usr/bin/env python
#coding:utf-8
import paramiko
# 建立一個(gè)sshclient對(duì)象
ssh = paramiko.SSHClient()
# 允許將信任的主機(jī)自動(dòng)加入到host_allow 列表,此方法必須放在connect方法的前面
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 調(diào)用connect方法連接服務(wù)器
ssh.connect('192.168.1.108', 22, 'alex', '123')
# 執(zhí)行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 結(jié)果放到stdout中,如果有錯(cuò)誤將放到stderr中
print(stdout.read().decode('utf-8'))
# 關(guān)閉連接
ssh.close();paramiko\ecdsakey.py:164: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
self.ecdsa_curve.curve_class(), pointinfoparamiko\kex_ecdh_nist.py:39: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.
m.add_string(self.Q_C.public_numbers().encode_point())paramiko\kex_ecdh_nist.py:96: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
self.curve, Q_S_bytesparamiko\kex_ecdh_nist.py:111: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.
hm.add_string(self.Q_C.public_numbers().encode_point())
原因
paramiko 2.4.2 依賴 cryptography,而最新的cryptography==2.5里有一些棄用的API。
解決
刪掉cryptography,安裝2.4.2,就不會(huì)報(bào)錯(cuò)了。
pip uninstall cryptography
pip install cryptography==2.4.2
執(zhí)行命令 -秘鑰
import paramiko
private_key_path = '/home/auto/.ssh/id_rsa'
key = paramiko.RSAKey.from_private_key_file(private_key_path)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('主機(jī)名 ', 端口, '用戶名', key)
stdin, stdout, stderr = ssh.exec_command('df')
print(stdout.read().decode("utf-8"))
ssh.close()上傳下載文件—用戶名密碼
import os,sys
import paramiko
t = paramiko.Transport(('182.92.219.86',22))
t.connect(username='derek',password='123')
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put('/tmp/test.py','/tmp/test.py')?
t.close()
import os,sys
import paramiko
t = paramiko.Transport(('182.92.219.86',22))
t.connect(username='derek',password='123')
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get('/tmp/test.py','/tmp/test2.py')
t.close()上傳下載文件-用戶名秘鑰
import paramiko
pravie_key_path = '/home/auto/.ssh/id_rsa'
key = paramiko.RSAKey.from_private_key_file(pravie_key_path)
t = paramiko.Transport(('182.92.219.86',22))
t.connect(username='derek',pkey=key)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put('/tmp/test3.py','/tmp/test3.py')?
t.close()
import paramiko
pravie_key_path = '/home/auto/.ssh/id_rsa'
key = paramiko.RSAKey.from_private_key_file(pravie_key_path)
t = paramiko.Transport(('182.92.219.86',22))
t.connect(username='derek',pkey=key)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get('/tmp/test3.py','/tmp/test4.py')?
t.close()以上就是python paramiko連接ssh實(shí)現(xiàn)命令的詳細(xì)內(nèi)容,更多關(guān)于python paramiko連接ssh的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 如何使用Python連接?SSH?服務(wù)器并執(zhí)行命令
- Python如何實(shí)現(xiàn)SSH遠(yuǎn)程連接與文件傳輸
- Python基于ssh遠(yuǎn)程連接Mysql數(shù)據(jù)庫(kù)操作
- Python用SSH連接到網(wǎng)絡(luò)設(shè)備
- Python3 SSH遠(yuǎn)程連接服務(wù)器的方法示例
- python下paramiko模塊實(shí)現(xiàn)ssh連接登錄Linux服務(wù)器
- Python實(shí)現(xiàn)建立SSH連接的方法
- Python自動(dòng)連接ssh的方法
- Python自動(dòng)連接SSH的實(shí)現(xiàn)步驟
相關(guān)文章
python實(shí)現(xiàn)自動(dòng)生成C++代碼的代碼生成器
這篇文章介紹了python實(shí)現(xiàn)C++代碼生成器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
torch 中各種圖像格式轉(zhuǎn)換的實(shí)現(xiàn)方法
這篇文章主要介紹了torch 中各種圖像格式轉(zhuǎn)換的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
pandas dataframe rolling移動(dòng)計(jì)算方式
在Pandas中,rolling()方法用于執(zhí)行移動(dòng)窗口計(jì)算,常用于時(shí)間序列數(shù)據(jù)分析,例如,計(jì)算某商品的7天或1個(gè)月銷售總量,可以通過(guò)rolling()輕松實(shí)現(xiàn),該方法的關(guān)鍵參數(shù)包括window(窗口大?。?min_periods(最小計(jì)算周期)2024-09-09
Python使用atexit模塊實(shí)現(xiàn)Golang的defer功能
這篇文章主要為大家詳細(xì)介紹了Python如何使用atexit模塊實(shí)現(xiàn)Golang的defer功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04
一百行python代碼將圖片轉(zhuǎn)成字符畫(huà)
這篇文章主要為大家詳細(xì)介紹了一百行python代碼將圖片轉(zhuǎn)成字符畫(huà) ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

