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