本篇内容介绍了“python中ssh2怎么远程登录节点并执行命令”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联建站主营胶州网站建设的网络公司,主营网站建设方案,成都app软件开发,胶州h5小程序制作搭建,胶州网站营销推广欢迎胶州等地区企业咨询root@node7:/opt# vim pyssh3.py #!/usr/bin/python"""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), e if __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(150,156): 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.__init__(group=None, target=pyssh, name=None, args=(ip,username,passwd,cmd),# kwargs=None, verbose=None) a.start()
“python中ssh2怎么远程登录节点并执行命令”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!