This article mainly for you to share the Python Paramiko module learning materials, with a certain reference value, interested in small partners can refer to
Paramiko is a module written in the Python language that follows the SSH2 protocol and supports the connection of remote servers in a way that is encrypted and authenticated. Paramiko supports Linux, Solaris, BSD, MacOS X, Windows and other platforms to connect to another platform via SSH from one platform. With this module, it is convenient to make an SSH connection and SFTP protocol for SFTP file transfer.
First let's clarify the following nouns:
sshclient: packaged channel, Transport, Sftpclient
Channel: is a kind of socket, a secure SSH transmission channel;
Transport: is an encrypted session (but the session of such an object is not established) and an encrypted tunnels is created, the tunnels is called the channel;
Session : is the object that the client remains connected to the server, starting the conversation with Connect ()/start_client ()/start_server ().
Download installation
Pycrypto, since the Paramiko module is internally dependent on Pycrypto, download the installation first Pycrypto
PIP3 Install Pycrypto
PIP3 Install Paramiko
Specific module use
Sshclient:
The remote connection is divided into two types: (1) connection based on username and password (2) based on public key key connection
Through the use of Paramiko remote operation, in fact, the essence is divided into two types: (1) only use Sshclient (2) to create a transport
Connect based on user name password
Import Paramiko # ssh object created ssh = Paramiko. Sshclient () # allows connections to host Ssh.set_missing_host_key_policy (Paramiko) that are not in the Know_hosts file. Autoaddpolicy ()) # Connect server Ssh.connect (hostname= ' host ', port=22, username= ' root ', password= ' 123 ') # Execute command stdout command result, stderr error stdin, stdout, stderr = Ssh.exec_command (' ls ') # get command result = Stdout.read () # Close connection Ssh.close ()
Sshclient Package Transport
Import Paramiko transport = Paramiko. Transport ((' hostname ')) Transport.connect (username= ' root ', password= ' 123 ') ssh = Paramiko. Sshclient () Ssh._transport = Transport stdin, stdout, stderr = Ssh.exec_command (' df ') print (Stdout.read ()) Transport.close ()
Connection based on public key secret key
Import Paramiko Private_key = Paramiko. Rsakey.from_private_key_file ('/home/auto/.ssh/id_rsa ') # create ssh object ssh = Paramiko. Sshclient () # allows connections to host Ssh.set_missing_host_key_policy (Paramiko) that are not in the Know_hosts file. Autoaddpolicy ()) # Connection Server Ssh.connect (hostname= ' host ', port=22, username= ' root ', key=private_key) # Execute command stdin, stdout, stderr = Ssh.exec_command (' df ') # gets the command result = Stdout.read () # Close connection Ssh.close ()
Sshclient Package Transport
Import Paramiko Private_key = Paramiko. Rsakey.from_private_key_file ('/home/auto/.ssh/id_rsa ') transport = Paramiko. Transport (' hostname ', ()) Transport.connect (username= ' Wupeiqi ', pkey=private_key) ssh = Paramiko. Sshclient () Ssh._transport = Transportstdin, stdout, stderr = Ssh.exec_command (' df ') transport.close ()
Sftpclient:
Used to connect to a remote server and upload and download functions.
Upload and download based on user name password
Import Paramiko transport = Paramiko. Transport (' hostname ', ()) Transport.connect (username= ' root ', password= ' 123 ') sftp = Paramiko. Sftpclient.from_transport (transport) # upload location.py to Server/tmp/test.pysftp.put ('/tmp/location.py ', '/tmp/test.py ') # download Remove_path to local local_pathsftp.get (' Remove_path ', ' Local_path ') transport.close ()
Upload and download based on public key secret key
Import Paramikoprivate_key = Paramiko. Rsakey.from_private_key_file ('/home/auto/.ssh/id_rsa ') transport = Paramiko. Transport (' hostname ', ()) Transport.connect (username= ' root ', pkey=private_key) sftp = Paramiko. Sftpclient.from_transport (transport) # upload location.py to Server/tmp/test.pysftp.put ('/tmp/location.py ', '/tmp/test.py ') # download Remove_path to local local_pathsftp.get (' Remove_path ', ' Local_path ') transport.close ()
Demo: Implement 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 D EF Run (self): Self.connect () # Connect remote server self.upload (' db.py ', '/tmp/1.py ') # upload the local db.py file to the/tmp/directory of the remote server and rename it to 1.py SE Lf.cmd (' DF ') # Execute 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 = tr Ansport 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 = Paramik O.sshclient () Ssh._transport = self.__transport # Execute command stdin, stdout, stderr = Ssh.exec_command (command) # Get life Results = Stdout.read () print (result) return result obj = Sshconnection () obj.run ()