Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remotely log in to a node and execute commands in ssh2 in python

Shulou Source: shulou.com Published: 2022-06-03 15:52:45 09月23日 Update

This article introduces the knowledge of "how to remotely log in to the node and execute commands in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Root@node7:/opt# vim pyssh3.Pyssh3.PYRAMUR binRAMA binPython "SSH connect to remote machines and then execute what you need command" import paramiko # import paramiko & & help (paramiko) | | pydoc paramikoimport threading#debug = True#class SSHClass (objiect): # "class" def pyssh3 (ip, username, passwd, cmd): try: ssh = paramiko.SSHClient () ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy () ssh.connect (ip, 22, username) Passwd, timeout=5) for m in cmd: stdin, stdout, stderr = ssh.exec_command (m) # stdin.write ("Y") # simple interaction, default input 'Y'out= stdout.readlines () # output to screen for o in out: print o, print's\ tOK\ n'% (ip) Ssh.close () except Exception, e: print 'ERR: can not find remote host% s'% (ip), eif _ _ name__=='__main__': cmd = ['hostname'] # need to execute cmd username = "root" # username passwd = "123123" # password threads = [] # multi thread print "Prompt:begin to connect remote machines." For i in range (150156): ip = '10.100.1.'+str (I) a=threading.Thread (target=pyssh3,args= (ip,username,passwd,cmd)) # used default arguments# _ init__ (self, group=None, target=None, name=None, args= (), kwargs=None, verbose=None) # a=threading.Thread () # a.roominituation _ (group=None, target=pyssh, name=None, args= (ip,username,passwd,cmd)) # kwargs=None, verbose=None) a.start () "how to remotely log in to a node in python and execute commands" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Commands nodes login content more knowledge practicality achievement next dilemma reality situation articles cases editing websites industries processes high quality learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Shulou Technology vpn Xiaomi