多线程重复执行命令问题
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import ConfigParser
import paramiko
import os,sys
import threading
class analyseconfig(ConfigParser.ConfigParser):
def __init__(self,cfile,sections):
ConfigParser.ConfigParser.__init__(self)
self.file=cfile
self.sections=sections
self.conf_info = {}
def parser(self):
if not os.path.isfile(self.file):
print "conf file not found!"
sys.exit(0)
else:
try:
self.read(self.file)
except:
print "File read error %s" % (self.file)
sys.exit(1)
kvs=self.items(self.sections)
return kvs
class working(threading.Thread,paramiko.SSHClient):
def __init__(self,*argv):
threading.Thread.__init__(self)
paramiko.SSHClient.__init__(self)
self.user='root'
self.port=22
self.timeout = 10
self.ssh_login_status = 1
self.exec_status = 1
self.host,self.password,self.cmd=argv
self.run()
def run(self):
print "++++++++++++++++++++++++++++++++++"
self.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
self.connect(self.host,self.port,self.user,self.password)
except:
print "can't connect:" %self.host
else:
self.stdin,self.stdout,self.stderr = self.exec_command(self.cmd)
self.stderr_out = self.stderr.read()
if self.stderr_out:
self.output = self.stderr_out
else:
self.exec_status = 0
self.output = self.stdout.read()
self.show_result()
def show_result(self):
self.info='%s%s%s\n%s\n' % ("=" * 30, self.host,"="* 30, self.output)
print self.info
def main():
c=analyseconfig('test.conf','host')
kvs=c.parser()
threading_pool=[]
num=len(kvs)
print "%d machines run..." % num
for host,password in kvs:
print host,password
t=''
t=working(host,password,'df -h')
t.setDaemon(True)
threading_pool.append(t)
[t.start() for t in threading_pool]
[t.join() for t in threading_pool]
if __name__=='__main__':
main()
执行结果:
[root@yetest01 app]# ./remotecmd.py
2 machines run...
192.168.1.131 ciwong2013
++++++++++++++++++++++++++++++++++
==============================192.168.1.131==============================
?? ? ?? ??à??% 1???/dev/sda3 58G 50G 5.6G 90% /
/dev/sda5 333G 279G 37G 89% /data
/dev/sda1 99M 12M 83M 13% /boot
tmpfs 16G 0 16G 0% /dev/shm
192.168.2.6 ciwong2012
++++++++++++++++++++++++++++++++++
==============================192.168.2.6==============================
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 19G 17G 1.2G 94% /
/dev/sda6 397G 29G 347G 8% /home
/dev/sda5 95G 454M 90G 1% /var
/dev/sda1 494M 17M 452M 4% /boot
tmpfs 12G 0 12G 0% /dev/shm
++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++
==============================192.168.2.6==============================
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda3 19G 17G 1.2G 94% /
/dev/sda6 397G 29G 347G 8% /home
/dev/sda5 95G 454M 90G 1% /var
/dev/sda1 494M 17M 452M 4% /boot
tmpfs 12G 0 12G 0% /dev/shm
==============================192.168.1.131==============================
?? ? ?? ??à??% 1???/dev/sda3 58G 50G 5.6G 90% /
/dev/sda5 333G 279G 37G 89% /data
/dev/sda1 99M 12M 83M 13% /boot
tmpfs 16G 0 16G 0% /dev/shm
从结果中发现,第二个线程多执行了几遍命令,找不到原因,请各位帮忙。
还有个就是乱码问题,怎么解码!