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 3.8中實(shí)現(xiàn)functools.cached_property功能
這篇文章主要介紹了Python 3.8中實(shí)現(xiàn)functools.cached_property功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Python?sklearn庫(kù)三種常用編碼格式實(shí)例
這篇文章主要為大家介紹了Python?sklearn庫(kù)三種常用編碼格式實(shí)例展示,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Python實(shí)現(xiàn)使用request模塊下載圖片demo示例
這篇文章主要介紹了Python實(shí)現(xiàn)使用request模塊下載圖片,結(jié)合完整實(shí)例形式分析了Python基于requests模塊的流傳輸文件下載操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05Python3 中作為一等對(duì)象的函數(shù)解析
這篇文章主要介紹了Python3 中作為一等對(duì)象的函數(shù),本文通過(guò)實(shí)例代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Python實(shí)現(xiàn)一個(gè)自助取數(shù)查詢工具
在數(shù)據(jù)生產(chǎn)應(yīng)用部門,取數(shù)分析是一個(gè)很常見的需求,實(shí)際上業(yè)務(wù)人員需求時(shí)刻變化,最高效的方式是讓業(yè)務(wù)部門自己來(lái)取,減少不必要的重復(fù)勞動(dòng),本文介紹如何用Python實(shí)現(xiàn)一個(gè)自助取數(shù)查詢工具2021-06-06使用python itchat包爬取微信好友頭像形成矩形頭像集的方法
今天小編就為大家分享一篇使用python itchat包爬取微信好友頭像形成矩形頭像集的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02