The Paramiko module, based on SSH, is used to connect to remote servers and perform related operations.
First, installation
PIP3 Install Paramiko
Second, use
sshclient
Used to connect to a remote server and execute basic commands
connect based on user name password:
#!/usr/bin/env Python3#-*-coding:utf-8-*-#AUTHOR:DBQ (Du Baoqiang)ImportParamiko#To create an SSH objectSSH=Paramiko. Sshclient ()#allow connections to hosts that are not in the Know_hosts fileSsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#connecting to a serverSsh.connect (hostname='192.168.1.103', port=22,username='Root', password='123456')#Execute CommandStdin,stdout,stderr = Ssh.exec_command ('ls/tmp/')#Get execution Resultsresult =Stdout.read ()Print(Result.decode ())#The return is bytes#Close Connectionssh.close () copy Code
View Code
connection based on public key:
#!/usr/bin/env python#-*-coding:utf-8-*-#auth:pangguopingImportParamikoprivate_key= Paramiko. Rsakey.from_private_key_file ('/users/daniel/.ssh/id_rsa')#To create an SSH objectSSH =Paramiko. Sshclient ()#allow connections to hosts that are not in the Know_hosts fileSsh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())#connecting to a serverSsh.connect (hostname='192.168.1.103', Port=22, Username='Root', pkey=Private_key)#Execute Commandstdin, stdout, stderr = Ssh.exec_command ('DF')#Get command Resultsresult =Stdout.read ()Print(Result.decode ())#Close ConnectionSsh.close ()
View Code
sftpclient
For connecting to a remote server and performing an upload download
upload/download based on user name password
#!/usr/bin/env python#-*-coding:utf-8-*-ImportParamikotransport= Paramiko. Transport (('192.168.1.103', 22)) Transport.connect (username='Root', password='123456') sftp=Paramiko. Sftpclient.from_transport (transport)#upload local file/users/daniel/test/zhoulibo.mp4 upload to server/tmp/test.mp4Sftp.put ('/users/daniel/test/test.mp4','/tmp/test.mp4')#Download the remote/tmp/zhoulibo.mp4 to the local/users/daniel/test.mp4Sftp.get ('/tmp/zhoulibo.mp4','/users/daniel/test.mp4') Transport.close ()
View Code
upload/download based on public key/key:
#!/usr/bin/env python#-*-coding:utf-8-*-ImportParamikoprivate_key= Paramiko. Rsakey.from_private_key_file ('/users/daniel/.ssh/id_rsa') Transport= Paramiko. Transport (('192.168.1.103', 22)) Transport.connect (username='Root', pkey=private_key) sftp=Paramiko. Sftpclient.from_transport (transport)#upload local file/users/daniel/test/tt.mp4 upload to server/tmp/tt.mp4Sftp.put ('/users/daniel/test/tt.mp4','/tmp/tt.mp4')#Download the remote/tmp/tt.mp4 to the local/users/daniel/tt.mp4Sftp.get ('/tmp/tt.mp4','/users/daniel/tt.mp4') Transport.close ()
View Code
#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportParamiko,uuidclasssshconnection:def __init__(self,host='192.168.1.103', port=22,username='Root', pwd='123456'): Self.host=host Self.port=Port Self.username=username Self.pwd=pwd self.__k=NonedefCreate_file (self): file_name= str (UUID.UUID4 ())#generate a ' 46cad739-5937-4312-9156-00753b272581 ' filename uuid and convert it to STRWith open (file_name,'W') as F:#Open the file and write a SB insideF.write ('SB') returnfile_namedefRun (self):" "Main method: return:" "Self.connect () self.upload ('/tmp/test.py')#call the Upload method and upload the/tmp/test.py to the serverSelf.rename ('/tmp/test.py','/tmp/sb.py')#renamed to Sb.pyself.close ()defConnect (self):" "To establish a connection: return:" "Transport=Paramiko. Transport ((Self.host,self.port)) Transport.connect (username=self.username,password=self.pwd) self.__transport=TransportdefClose (self):" "Close connection: return:" "Self .__transport. Close ()defUpload (self,target_path):" "Connect, upload:p Aram Target_path:: Return:" "file_name=self.create_file () sftp= Paramiko. Sftpclient.from_transport (self.__transport) #uploading local files to the server test.pysftp.put (file_name, Target_path)defrename (self, Old_path, New_path):" "renamed, renamed/tmp/test.py to sb.py:p Aram Old_path::p Aram New_path:: Return:" "SSH=Paramiko. Sshclient () Ssh._transport= self.__transport #Execute Commandcmd ="mv%s%s"%(Old_path,new_path) Stdin,stdout,stderr=Ssh.exec_command (cmd) res=Stdout.read ()defcmd (self,command):" "Other commands can be executed:p Aram command:: return:" "self.connect () SSH=Paramiko. Sshclient () Ssh._transport= self.__transport #Execute Commandstdin, stdout, stderr =ssh.exec_command (command) Res=Stdout.read () self.close ( )returnResha=sshconnection () ha.run () Res= Ha.cmd ('ls')Print(Res.decode ())
Demo
Python's Paramiko Learning