真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

使用paramiko連接ssh

paramiko

paramiko是一個用于做遠(yuǎn)程控制的模塊,使用該模塊可以對遠(yuǎn)程服務(wù)器進(jìn)行命令或文件操作,值得一說的是,fabric和ansible內(nèi)部的遠(yuǎn)程管理就是使用的paramiko來現(xiàn)實(shí)。

專業(yè)從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),高端網(wǎng)站制作設(shè)計(jì),成都微信小程序,網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團(tuán)隊(duì)竭力真誠服務(wù),采用H5高端網(wǎng)站建設(shè)+CSS3前端渲染技術(shù),響應(yīng)式網(wǎng)站建設(shè),讓網(wǎng)站在手機(jī)、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項(xiàng)小組,與您實(shí)時在線互動,隨時提供解決方案,暢聊想法和感受。

安裝

pip install paramiko

模塊使用

執(zhí)行命令—用戶名+密碼
#!/usr/bin/env python
#coding:utf-8

import paramiko

# 建立一個sshclient對象
ssh = paramiko.SSHClient()
# 允許將信任的主機(jī)自動加入到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中,如果有錯誤將放到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,就不會報錯了。

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()

名稱欄目:使用paramiko連接ssh
URL標(biāo)題:http://weahome.cn/article/jpopse.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部