The Paramiko is a Python ssh library that you can use to connect to a remote Linux host and then execute a Linux command or transfer files via SFTP.
about using Paramiko to execute remote host commands you can find a lot of references, this article on this basis to do some encapsulation, easy to expand and write scripts.
The code is given directly below:
1 #Coding:utf-82 3 ImportParamiko4 ImportRe5 fromTimeImportSleep6 7 #defines a class that represents a remote Linux host8 classLinux (object):9 #initialize a remote Linux host by IP, username, password, time-outTen def __init__(Self, IP, username, password, timeout=30): OneSelf.ip =IP ASelf.username =username -Self.password =Password -Self.timeout =Timeout the #Transport and Chanel -SELF.T ="' -Self.chan ="' - #number of retry attempts for link failures +Self.try_times = 3 - + #call this method to connect to the remote host A defConnect (self): at whileTrue: - #exceptions may be thrown during the connection, such as network connectivity, link timeouts - Try: -self.t = Paramiko. Transport (sock= (Self.ip, 22)) -Self.t.connect (Username=self.username, password=Self.password) -Self.chan =self.t.open_session () in self.chan.settimeout (self.timeout) - Self.chan.get_pty () to Self.chan.invoke_shell () + #If no exception is thrown to indicate successful connection, return directly - PrintU'Connection%s succeeded'%Self.ip the #network data received is decoded to STR * PrintSELF.CHAN.RECV (65535). Decode ('Utf-8') $ returnPanax Notoginseng #here is not possible anomalies such as socket.error, socket.timeout refinement, direct clean sweep - exceptException, E1: the ifSelf.try_times! =0: + PrintU'failed to connect to%s, retry'%Self.ip ASelf.try_times-= 1 the Else: + PrintU'3 retries failed, end program' -Exit (1) $ $ #Disconnect Connection - defClose (self): - self.chan.close () the self.t.close () - Wuyi #send the command to execute the defSend (self, cmd): -CMD + ='\ r' Wu #command execution prompt to determine if a command is complete -p = Re.compile (r':~ #') About $result ="' - #send the command to execute - self.chan.send (cmd) - #Echoing a very long command may take a long time to retrieve the echo through a cyclic batch A whileTrue: +Sleep (0.5) theret = SELF.CHAN.RECV (65535) -ret = Ret.decode ('Utf-8') $Result + =ret the ifP.search (ret): the Printresult the returnResult
The following tests are performed:
1 #Host IP Error, unable to connect condition2 if __name__=='__main__':3Host = Linux ('192.168.180.12','Root','xxxx')4 Host.connect ()5 6Host.send ('ls-l')7 host.close ()8
1 #linked to normal conditions2 if __name__=='__main__':3Host = Linux ('192.168.180.128','Root',' Love')4 Host.connect ()5Host.send ('ls-l')6 host.close ()7 8 Operation Result:9Connection 192.168.180. 128 SuccessesTenLast Login:sat 21 07:25:39 2016 from192.168.180.1 One Has a lot of fun ... A -LS-L -192:~#ls-l theTotal 28 --RW-------1 root root 21 07:17. Bash_history -Drwxr-xr-x 1 root root 21 06:02. config -drwx------1 root root 21 05:57. Dbus +drwx------1 root root 0 Sep 25 2014. GnuPG -Drwxr-xr-x 1 root root 21 06:15. Local +-RW-------1 root root 21 06:03. Xauth5mesuo A-RW-------1 root root 21 07:22. XAUTHEYQDMK at-RW-------1 root root 21 07:25. Xauthgtroho --RW-------1 root root 21 07:09. xauthp90tng --RW-------1 root root 21 07:40. Xauthjw8pi9 --RW-------1 root root 21 07:40. Xauthx8t4ed -Drwxr-xr-x 1 root root 0 Sep 25 2014bin -Drwxr-xr-x 1 root root 05:43 inst-SYS in192:~# - toProcess finished with exit code 0
9 Connection 192.168.180. 12 failed, retry the connection 192.168.180. 12 failed, retry the connection 192.168.180 . 12 failed, retry A 3 retries failed, end program - + Process finished with exit code 1
As you can see from the code above, it is very simple to execute a remote Linux host after a simple encapsulation, just a few lines of code.
Using Paramiko to execute remote Linux host commands