Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347519
  • 博文数量: 26
  • 博客积分: 495
  • 博客等级: 下士
  • 技术积分: 562
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-26 13:50
文章分类

全部博文(26)

文章存档

2015年(9)

2014年(6)

2013年(7)

2012年(2)

2011年(2)

分类: 系统运维

2015-03-19 18:48:26

时间总是流淌的那么快, 5年前学习python的热情劲头恍如昨日,10年写的python多进程脚本,以后备用

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #coding:utf-8
  3. import pexpect
  4. from Queue import Queue
  5. from threading import Thread
  6. import time
  7. import sys

  8. ''' just a practise

  9. '''

  10. max_threads = 25
  11. queue =Queue()

  12. def Dssh_Cmd(i,q):
  13.     hello_world = 'Are you sure to continue connecting'
  14.     while True:
  15.         a = q.get()
  16.         child = pexpect.spawn('ssh %s@%s "%s"' %(a[1],a[0],a[3]))
  17.         fout = open('log.txt','a')
  18.         child.logfile = fout
  19.         index = child.expect([hello_world,'password:',pexpect.EOF])
  20.         if index == 0:
  21.             child.sendline('yes')
  22.             index = child.expect([hello_world,'password:',pexpect.EOF])
  23.         elif index == 1:
  24.             child.sendline(a[2])
  25.             index = child.expect(pexpect.EOF)
  26.         elif index == 2:
  27.             child.close()
  28.         print '--host:%s --cmd:%s' % (a[0],a[3])
  29.         print child.before
  30.         q.task_done()

  31. def main():
  32.     f = open(sys.argv[1],'r')
  33.     for line in f.readlines():
  34.         queue.put(line.strip().split(":"))

  35.     for t in range(max_threads):
  36.          worker = Thread(target=Dssh_Cmd,args=(t,queue))
  37.          worker.setDaemon(True)
  38.          worker.start()
  39.     print "main Thread waiting....."
  40.     queue.join()
  41.     f.close()
  42.     print "All Of Done"

  43. if __name__ == "__main__":
  44.     main()
热情保持下去,努力~

阅读(1381) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~