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
[[Email protected] ~]# pip install Paramiko
Second, remote connection
1, Method one
Import paramikossh = 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 paramikotus = (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, Remotepa TH) # upload T.close ()
Four , Instance
Import paramikofrom switch import switch_ip, switch_pwd, switch_usernamefrom lib.read_ini import configfrom lib.logsys import LogSysimport Threadingimport timeclass update (object): def __init__ (Self, dev, pname): self.cf = config () self.log = logsys () self.pname = pname self.ip = switch_ IP (Dev) # Get remote server ip self.username = switch_username (Dev) based on Dev # Get user name &nbSp; self.password = switch_pwd (Dev) # Get Password self.port = int (Self.cf.getvalue (' Server_ Conf ', ' Port ') self.localpath = Self.cf.getvalue (' Write_path ', ' Upfilepath ') + self.pname + ". zip" self.remotepath = self.cf.getvalue (' Write_path ', ' Topath ') + self.pname + ". zip" def __trans_file (self): # File Transfer try: tus = (Self.ip, self.port) t = paramiko.Transport (TUS) # Creating a Transport object t.connect (Username=self.username, password=self.password) # Connecting remote servers sftp = Paramiko. Sftpclient.from_transport (t) # create download Transfer object sftp.put (Self.localpath, self.remotepath) # uploading files t.close () print "TRANS_OK" return 5 except exception, e: print "Put_file : " + str (e) &nbSp; return 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 remote server stdin, stdout, stderr = ssh.exec_command (Comm) # execute scripts on the remote server &nbsP; 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): # perform the 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
Python's Paramiko module