Connect ssh using paramiko

Posted by headrush on Thu, 05 Dec 2019 04:15:51 +0100

paramiko

Paramiko is a module for remote control that allows command or file operations on remote servers. It is worth noting that remote management within fabric s and ansible is achieved using paramiko.

install

pip install paramiko

Module usage

Execute Command - User Name + Password

#!/usr/bin/env python
#coding:utf-8

import paramiko

# Create an sshclient object
ssh = paramiko.SSHClient()
# Allows trusted hosts to be automatically added to the host_allow list, which must precede the connect method
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Call the connect method to connect to the server
ssh.connect('192.168.1.108', 22, 'alex', '123')
# Execute Command
stdin, stdout, stderr = ssh.exec_command('df')
# The result is placed in stdout, and if there are any errors, in stderr
print(stdout.read().decode('utf-8'))
# Close Connection
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())

Reason

paramiko 2.4.2 relies on cryptography, while the latest cryptography==2.5 has some deprecated API s.

Solve

If you remove cryptography and install 2.4.2, you won't get it wrong.

pip uninstall cryptography
pip install cryptography==2.4.2

Execute Command-Key

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('host name ', port, 'User name', key)

stdin, stdout, stderr = ssh.exec_command('df')
print(stdout.read().decode("utf-8"))
ssh.close()

Upload and Download Files - User Name Password

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

Upload Download File - User Name Secret Key

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

Topics: Python ssh sftp pip encoding