欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python paramiko連接ssh實(shí)現(xiàn)命令

 更新時(shí)間:2022年07月28日 16:08:21   作者:wangfeng7399  
這篇文章主要為大家介紹了python paramiko連接ssh實(shí)現(xiàn)的命令詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jì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(), pointinfo

paramiko\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_bytes

paramiko\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)文章!

相關(guān)文章

  • Python 3.8中實(shí)現(xiàn)functools.cached_property功能

    Python 3.8中實(shí)現(xiàn)functools.cached_property功能

    這篇文章主要介紹了Python 3.8中實(shí)現(xiàn)functools.cached_property功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Python?sklearn庫(kù)三種常用編碼格式實(shí)例

    Python?sklearn庫(kù)三種常用編碼格式實(shí)例

    這篇文章主要為大家介紹了Python?sklearn庫(kù)三種常用編碼格式實(shí)例展示,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • python2與python3共存問(wèn)題的解決方法

    python2與python3共存問(wèn)題的解決方法

    這篇文章主要為大家詳細(xì)介紹了python2與python3共存問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • django框架模型層功能、組成與用法分析

    django框架模型層功能、組成與用法分析

    這篇文章主要介紹了django框架模型層功能、組成與用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Django框架中模型層的基本概念、原理、常用組件構(gòu)成與相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • Python實(shí)現(xiàn)使用request模塊下載圖片demo示例

    Python實(shí)現(xiàn)使用request模塊下載圖片demo示例

    這篇文章主要介紹了Python實(shí)現(xiàn)使用request模塊下載圖片,結(jié)合完整實(shí)例形式分析了Python基于requests模塊的流傳輸文件下載操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-05-05
  • Python3 中作為一等對(duì)象的函數(shù)解析

    Python3 中作為一等對(duì)象的函數(shù)解析

    這篇文章主要介紹了Python3 中作為一等對(duì)象的函數(shù),本文通過(guò)實(shí)例代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python列表的淺拷貝與深拷貝

    Python列表的淺拷貝與深拷貝

    這篇文章主要介紹了Python列表的淺拷貝與深拷貝,對(duì)列表深拷貝就是無(wú)論怎樣改動(dòng)新列表,單維or多維,原列表都不變,需要的小伙伴可以參考下面更詳細(xì)內(nèi)容
    2022-03-03
  • Python實(shí)現(xiàn)一個(gè)自助取數(shù)查詢工具

    Python實(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包爬取微信好友頭像形成矩形頭像集的方法

    今天小編就為大家分享一篇使用python itchat包爬取微信好友頭像形成矩形頭像集的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • Python之re模塊詳解

    Python之re模塊詳解

    這篇文章主要介紹了Python編程之Re模塊下的函數(shù)介紹,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考,希望能夠給你帶來(lái)幫助
    2021-09-09

最新評(píng)論