多线程重复执行命令问题

leoxqing 2013-04-03 10:49:03
#!/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

从结果中发现,第二个线程多执行了几遍命令,找不到原因,请各位帮忙。
还有个就是乱码问题,怎么解码!
...全文
106 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
bugs2k 2013-04-03
  • 打赏
  • 举报
回复
楼主 发表于: 2013-04-03 10:49:03 #!/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() [t.start() for t in threading_pool] 是不是这个? def show_result(self): self.info='%s%s%s\n%s\n' % ("=" * 30, self.host,"="* 30, self.output) print self.info 显示错误,可能需要对上面的self.output根据情况进行编解码处理

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧