Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report