Use paramiko to execute remote linux host commands (detailed description) and paramikolinux
Paramiko is a python SSH library that can be used to connect to a remote linux host, and then run linux commands or transmit files through SFTP.
Many references can be found for executing remote host commands using paramiko. This article provides some encapsulation to facilitate the extension and writing of scripts.
The following code is provided:
# Coding: utf-8import paramikoimport refrom time import sleep # defines a class that represents a remote linux host class Linux (object): # By IP, user name, password, timeout value: def _ init _ (self, ip, username, password, timeout = 30): self. ip = ip self. username = username self. password = password self. timeout = timeout # transport and chanel self. t = ''self. chan = ''# Number of Retries for Link failure self. try_times = 3 # Call this method to connect to the remote host def connect (self): while True: # Exceptions may be thrown during connection, such as network disconnection and connection timeout try: self. t = paramiko. transport (sock = (self. ip, 22) self. t. connect (username = self. username, password = self. password) self. chan = self. t. open_session () self. chan. settimeout (self. timeout) self. chan. get_pty () self. chan. invoke_shell () # If no exception is thrown, the connection is successful and print U' connection % s successful '% self. ip # decodes the received network data to str print self. chan. recv (1, 65535 ). decode ('utf-8') return # possible exceptions, such as socket. error, s Ocket. timeout is refined. Skip t Exception, e1: if self. try_times! = 0: print U' connection % s failed, retry '% self. ip self. try_times-= 1 else: print U' retry 3 failed, end program 'exit (1) # Disconnect def close (self): self. chan. close () self. t. close () # send the command def send (self, cmd): cmd + = '\ R' # Use the command execution prompt to determine whether the command has been executed successfully. compile (R ':~ # ') Result = ''# send the command self to be executed. chan. send (cmd) # A long echo command may be executed for a long time. The ECHO can be retrieved in batches through a loop. while True: sleep (0.5) ret = self. chan. recv (1, 65535) ret = ret. decode ('utf-8') result + = ret if p. search (ret): print result return result
Perform the following tests:
# If the host IP address is incorrect and cannot be connected, if _ name _ = '_ main _': host = Linux ('2017. 168.180.12 ', 'root', 'xxxx') host. connect () 6 host. send ('LS-l') host. close () press Ctrl + C copy code connection 192.168.180.12 failed, retry connection 192.168.180.12 failed, retry connection 192.168.180.12 failed, retry three failed, process finished with exit code 1
# If _ name _ = '_ main _': host = Linux ('2017. 168.180.128 ', 'root', 'love') host. connect () host. send ('LS-l') host. close () running result: Connection 192.168.180.128 successful Last login: Sat May 21 07:25:39 2016 from 192.168.180.1Have a lot of fun... ls-l192 :~ # Ls-ltotal 28-rw ------- 1 root 18 May 21 07:17. bash_historydrwxr-xr-x 1 root 28 May 21. configdrwx ------ 1 root 22 May 21 05:57. dbusdrwx ------ 1 root 0 Sep 25 2014. gnupgdrwxr-xr-x 1 root 10 May 21. local-rw ------- 1 root 55 May 21 06:03. xauth5mesuo-rw ------- 1 root 55 May 21. xauthEYqDmK-rw ------- 1 root 55 May 21 07:25. xauthGTrohO-r W ------- 1 root 55 May 21 07:09. xauthP90TnG-rw ------- 1 root 48 May 21. xauthjW8pI9-rw ------- 1 root 48 May 21. xauthx8T4EDdrwxr-xr-x 1 root 0 Sep 25 2014 bindrwxr-xr-x 1 root 38 May 21 inst-sys192 :~ # Process finished with exit code 0
Based on the above example, using paramiko to execute remote linux host commands (detailed explanation) is all the content that I have shared with you. I hope you can give us a reference and support the help house.