The Paramiko module for Python third-party modules

Source: Internet
Author: User
Tags stdin

Directory:
    • Paramiko Module Introduction
    • Paramiko Module Installation
    • Paramiko module Use
First, Paramiko module introduction
Paramiko is a module for remote control, which can be used to command or file the remote server, it is worth saying that the fabric and ansible internal remote management is the use of Paramiko to reality. It contains two common modules, sshclient () module, sftpclient () module.
Second, Paramiko module installation
Pycrypto, since the Paramiko module is internally dependent on Pycrypto, download the installation PYCRYPTOPIP3 install PYCRYPTOPIP3 installed Paramiko
Third, Paramiko module use

1. Execute remote command sshclient () module

1 #!/usr/bin/python2 3 ImportParamiko4 5  6 7SSH =Paramiko. Sshclient ()8 9 Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())Ten  OneSsh.connect ("an IP address", 22,"User name","Password") A  -stdin, stdout, stderr = Ssh.exec_command ("your orders.") -  the PrintStdout.readlines () -  -Ssh.close ()
View Code

2, execute the remote Command sshclient () module key Login

1 ImportParamiko2 3Private_key_path ='/home/auto/.ssh/id_rsa'4Key =Paramiko. Rsakey.from_private_key_file (Private_key_path)5 6SSH =Paramiko. Sshclient ()7 Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())8Ssh.connect ('Host name'Port'User name', key)9 Tenstdin, stdout, stderr = Ssh.exec_command ('DF') One PrintStdout.read () ASsh.close ()
View Code
1 ImportParamiko2 3Pravie_key_path ='/home/auto/.ssh/id_rsa'4Key =Paramiko. Rsakey.from_private_key_file (Pravie_key_path)5 6t = Paramiko. Transport (('182.92.219.86', 22))7T.connect (username='Wupeiqi', pkey=key)8 9SFTP =Paramiko. Sftpclient.from_transport (t)TenSftp.put ('/tmp/test3.py','/tmp/test3.py') One  A t.close () -  - ImportParamiko the  -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='Wupeiqi', pkey=key) +  ASFTP =Paramiko. Sftpclient.from_transport (t) atSftp.get ('/tmp/test3.py','/tmp/test4.py') -  -T.close ()
View Code

3. Uploading files to the remote sftpclient () module

1 #!/usr/bin/python2 3 ImportParamiko4 5  6 7t = Paramiko. Transport (("an IP address", 22))8 9T.connect (username ="User name", password ="Password")Ten  OneSFTP =Paramiko. Sftpclient.from_transport (t) A  -Remotepath='/tmp/test.txt' -  theLocalpath='/tmp/test.txt' -  - sftp.put (Localpath,remotepath) -  +T.close ()
View Code

4. Remote download to local sftpclient () module

1 ImportParamiko2 3  4 5t = Paramiko. Transport (("an IP address", 22))6 7T.connect (username ="User name", password ="Password")8 9SFTP =Paramiko. Sftpclient.from_transport (t)Ten  OneRemotepath='/tmp/test.txt' A  -Localpath='/tmp/test.txt' -  the Sftp.get (RemotePath, LocalPath) -  -T.close ()
View Code

5. Integrated use:

1 #Coding:utf-82 ImportParamiko3 ImportUUID4 5 classsshconnection (object):6 7     def __init__(Self, host='192.168.2.103', Port=22, Username='Root', pwd='123456'):8Self.host =Host9Self.port =PortTenSelf.username =username OneSelf.pwd =pwd ASelf.__k=None -  -     defConnect (self): theTransport =Paramiko. Transport ((self.host,self.port)) -Transport.connect (username=self.username,password=self.pwd) -Self.__transport=Transport -  +     defClose (self): -Self.__transport. Close () +  A     defUpload (self,local_path,target_path): at         #Connect, upload -         #file_name = Self.create_file () -SFTP = Paramiko. Sftpclient.from_transport (self.__transport) -         #uploading location.py to the server/tmp/test.py - sftp.put (Local_path, Target_path) -  in     defDownload (self,remote_path,local_path): -SFTP = Paramiko. Sftpclient.from_transport (self.__transport) to sftp.get (Remote_path,local_path) +  -     defcmd (self, command): theSSH =Paramiko. Sshclient () *Ssh._transport = self.__transport $         #Execute CommandPanax Notoginsengstdin, stdout, stderr =ssh.exec_command (command) -         #Get command Results theresult =Stdout.read () +         Print(Str (result,encoding='Utf-8')) A         returnresult the  +SSH =sshconnection () - Ssh.connect () $Ssh.cmd ("ls") $Ssh.upload ('s1.py','/tmp/ks77.py') -Ssh.download ('/tmp/test.py','KKKK',) -Ssh.cmd ("DF") theSsh.close ()
View Code

The Paramiko module for Python third-party modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.