Introduction to Python's Paramiko module

Source: Internet
Author: User
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.

All Python-supported platforms, such as Linux, Solaris, BSD, MacOS X, Windows, and so on, are supported by the use of Python, which can run across platforms, so Paramiko Paramiko is one of the best tools when you need to use SSH to connect to another platform from one platform and perform a series of operations.

First, install the Paramiko module

[Root@yaoliang ~]# pip Install Paramiko

Second, remote connection

1, Method one

Import Paramiko ssh = Paramiko. Sshclient ()                      # Creates a client connection to the service-side object Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) # Allow connections to hosts that are not in the Know_hosts file Ssh.connect (ip,port,username,password)          # Connect to a remote server

2. Method Two

Import Paramiko tus = (IP, port) t = Paramiko. Transport (TUS)                     # Create Transfer Object T.connect (Username=self.username, Password=self.password)

Third, File transfer

tus = (IP, port) t = Paramiko. Transport (TUS) t.connect (Username=username, password=password) sftp = Paramiko. Sftpclient.from_transport (t)    # Create Download Transfer object Sftp.get (LocalPath, RemotePath)                 # download Sftp.put (LocalPath, RemotePath)                 # upload T.close ()

Iv. examples

Import paramikofrom Switch Import switch_ip, switch_pwd, switch_usernamefrom lib.read_ini import Configfrom Lib.logsys im         Port Logsysimport Threadingimport Time Class UpDate (object): Def __init__ (self, Dev, pname): self.cf = config () Self.log = Logsys () self.pname = PName Self.ip = switch_ip (dev) # based on        Dev Gets the remote server IP self.username = switch_username (dev) # Gets the user name Self.password = switch_pwd (dev) # get the password self.port = Int (self.cf.getvalue (' server_conf ', ' Port ')) Self.localpath = Self.cf.getvalue (' Write_path ', ' upfile Path ') + Self.pname + ". zip" Self.remotepath = self.cf.getvalue (' Write_path ', ' topath ') + self.pname + ". zip" D EF __trans_file (self): # file Transfer try:tus = (Self.ip, self.port) t = Paramiko. Transport (TUS) # Create transport Object T.connect (Username=self.username, Password=self.password) # Connect remote server S FTP = Paramiko. Sftpclient. From_transport (t) # Create download Transfer object Sftp.put (Self.localpath, Self.remotepath) # Upload file T.close () Print "TRANS_OK" return 5 except Exception, E:print "put_file:" + str (e) RET Urn 0 def __excuteup (self): # remote Operation ssh = Paramiko. Sshclient () Comm = '/root/test.sh ' + self.pname try:ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Ssh.connect (self.ip,self.port,self.username,self.password,timeout=10) # Connect to remote server std            In, stdout, stderr = Ssh.exec_command (comm) # Execute script on remote server out = Stdout.readlines () ssh.close ()             Return 5 except Exception, e:errmsg = "Excuteup error.__excute" + str (e) ssh.close ()                          Self.log.writelog (errmsg) # Save the error message to the log return 0 def runup (self): # Action to perform upload and remote execute command if Self.__trans_file (): If SELf.__excuteup (): print "update success." Return 5 else:print "Trans OK.                Excute err! " Return 1 Else:return 1
Related Article

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.