标签:name color err sys.argv readline bsp logs stdout es2017
写一个脚本,实现同时对多台主机执行远程命令
[root@localhost ~]# cat ip.txt # ip 列表 192.168.5.131 192.168.5.134
[root@localhost ~]# cat 1.py #!/usr/bin/env python #-*- coding: utf8 -*- import sys import paramiko import threading def ssh(ip, cmd): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect(hostname=ip, port=22, username=‘root‘, password=‘root‘) except Exception: print ‘%s is not exist.‘ % ip sys.exit() stdin, stdout, stderr = ssh.exec_command(cmd) print "\033[1;31;40m%s 结果:\n\033[0m%s" % (ip.rstrip(‘\n‘), stdout.read()), ssh.close() if __name__ == ‘__main__‘: with open(‘ip.txt‘, ‘r‘) as fd: data = fd.readlines() for ip in data: try: t = threading.Thread(target=ssh, args=(ip, sys.argv[1])) t.start() except Exception: print "%s must follow a command" % __file__ sys.exit()
结果:
标签:name color err sys.argv readline bsp logs stdout es2017
原文地址:http://www.cnblogs.com/pzk7788/p/7932227.html