python 批量ssh链接执行命令

需要在当前目录下手动创建succlog,errlog,以及用户名和密码文件pcinfoa。下面是代码(如有疑问可在网站首页导航栏的联系我页面找到我的联系方式)

import paramiko
import os
class Ssh(object):
    def __init__(self,pcinfo,errorlog,succlog,*cmd):
        self.pcinfo=pcinfo
        self.errorlog=errorlog
        self.succlog=succlog
        self.cmd=cmd
    def sshclient(self):
        try:
            ssh=paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            with open(self.pcinfo,'r') as pcinfo:
                for line in pcinfo.readlines():
                    line=line.strip().split(' ')
                    ssh.connect(line[0],line[1],line[2],line[3],timeout=15)
                    for m in self.cmd:
                        stdin, stdout, stderr = ssh.exec_command(m)
                        with open(self.succlog,'a') as succinfo:
                            for out in stdout:
                                succinfo.write(line[0]+'---------'+out)
        except :
            with open(self.errorlog,'a') as errinfo:
                errinfo.write(line[0]+'---------error\n')
        ssh.close()
command = ['touch /a', 'ls -l', 'echo aaaaaa']
sshclient = Ssh('pcinfoa', 'errlog', 'succlog', *command)
sshclient.sshclient()

“python 批量ssh链接执行命令”的一个回复

  1. Hey webmaster
    When you write some blogs and share with us,that is a hard work for you but share makes you happly right?
    yes I am a blogger too,and I wanna share with you my method to make some extra cash,not too much
    maybe $100 a day,but when you keep up the work,the cash will come in much and more.more info you can checkout my blog.
    http://bit.ly/waystomakesmoney
    good luck and cheers!

发表评论

电子邮件地址不会被公开。 必填项已用*标注