Python paramiko module learning and sharing, pythonparamiko
Paramiko is a module written in python. It complies with the SSH2 protocol and supports remote server connection through encryption and authentication. Paramiko supports Linux, Solaris, BSD, MacOS X, Windows and other platforms to connect from one platform to another through SSH. This module facilitates ssh connection and sftp protocol for sftp file transmission.
First, let's clarify the following terms:
SSHClient: Channel, Transport, SFTPClient
Channel: it is a Socket type and a Secure SSH transmission Channel;
Transport: an encrypted Session (but the Session of such an object is not created), and an encrypted tunnels is created. This tunnels is called a Channel;
Session: the object that the client and Server keep connected to. Use connect ()/start_client ()/start_server () to start the Session.
Paramiko reference http://docs.paramiko.org/en/2.0/index.html
Download and install
Pycrypto. Because the paramiko module depends on pycrypto, download and install pycrypto first.
Pip3 install pycrypto
Pip3 install paramiko
Use of specific modules
SSHClient:
There are two types of remote connections: (1) user name and password-based connection (2) Public Key-based connection
Paramiko is used for remote operations. In fact, there are two types of operations: (1) only use SSHClient (2) create a transport
Connect based on user name and password
Import paramiko # create an SSH object ssh = paramiko. SSHClient () # Allow connection to host ssh that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoAddPolicy () # connect to the server ssh. connect (hostname = 'host', port = 22, username = 'root', password = '000000') # execute the stdout command result, stderr error stdin, stdout, stderr = ssh.exe c_command ('LS') # obtain the command result = stdout. read () # Close the ssh connection. close ()
SSHClient encapsulation Transport
import paramiko transport = paramiko.Transport(('hostname', 22))transport.connect(username='root', password='123') ssh = paramiko.SSHClient()ssh._transport = transport stdin, stdout, stderr = ssh.exec_command('df')print(stdout.read()) transport.close()
Public Key-based connection
Import paramiko private_key = paramiko. RSAKey. from_private_key_file ('/home/auto /. ssh/id_rsa ') # create an SSH object ssh = paramiko. SSHClient () # Allow connection to host ssh that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoAddPolicy () # connect to the server ssh. connect (hostname = 'host', port = 22, username = 'root', key = private_key) # Run stdin, stdout, stderr = ssh.exe c_command ('df ') # obtain the command result = stdout. read () # Close the ssh connection. close ()
SSHClient encapsulation Transport
import paramiko private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')transport = paramiko.Transport(('hostname', 22))transport.connect(username='wupeiqi', pkey=private_key)ssh = paramiko.SSHClient()ssh._transport = transportstdin, stdout, stderr = ssh.exec_command('df')transport.close()
SFTPClient:
Connect to the remote server and perform the upload/download function.
Upload and download based on user name and password
Import paramiko transport = paramiko. transport ('hostname', 22) transport. connect (username = 'root', password = '000000') sftp = paramiko. SFTPClient. from_transport (transport) # Set location. upload py to the server/tmp/test. pysftp. put ('/tmp/location. py', '/tmp/test. py ') # download remove_path to the local local_pathsftp.get ('remove _ path', 'local _ path') transport. close ()
Upload and download Based on the Public Key
Import paramiko private_key = paramiko. RSAKey. from_private_key_file ('/home/auto /. ssh/id_rsa ') transport = paramiko. transport ('hostname', 22) transport. connect (username = 'root', pkey = private_key) sftp = paramiko. SFTPClient. from_transport (transport) # Set location. upload py to the server/tmp/test. pysftp. put ('/tmp/location. py', '/tmp/test. py ') # download remove_path to the local local_pathsftp.get ('remove _ path', 'local _ path') transport. close ()
Demo: Remote Command Execution and File Upload
Import paramiko class SSHConnection (object): def _ init _ (self, host = '192. 168.12.68 ', port = 22, username = 'locojoy', pwd = '123321qq! '): Self. host = host self. port = port self. username = username self. pwd = pwd self. _ k = None def run (self): self. connect () # connect to the remote server self. upload ('db. py', '/tmp/1. py ') # convert the local db. upload the py file to the/tmp/directory of the remote server and change it to 1.py self. cmd ('df ') # execute the df command self. close () # close connection def connect (self): transport = paramiko. transport (self. host, self. port) transport. connect (username = self. username, password = self. pwd) self. _ transport = transport def close (self): self. _ transport. close () def upload (self, local_path, target_path): sftp = paramiko. SFTPClient. from_transport (self. _ transport) sftp. put (local_path, target_path) def cmd (self, command): ssh = paramiko. SSHClient () ssh. _ transport = self. _ transport # execute the command stdin, stdout, stderr = ssh.exe c_command (command) # obtain the command result = stdout. read () print (result) return result obj = SSHConnection () obj. run ()
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.