需要手动安装paramiko模块,还需要在当前目录用户名和密码文件,文件名为pcinfoa(pcinfoa文件内容格式如下,中间以空格 隔开)。

1.1.1.1 22 user password

下面是程序代码(如有疑问可在网站首页导航栏的联系我页面找到我的联系方式)

import paramikoimport osclass 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()