免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2205 | 回复: 1
打印 上一主题 下一主题

小程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-05-24 16:41 |只看该作者 |倒序浏览



  1. """
  2. Usage:bmcdebuglog.py -smmip smmip -smmpassword smmpassword -slot slot

  3. Options:
  4. -smmip smmip
  5. -smmpassword smmpassword
  6. -slot slot
  7. """

  8. import pexpect
  9. import os, sys, commands
  10. import time
  11. from pexpect import *


  12. opts = sys.argv[1:]

  13. if not len(opts) == 6:
  14.     print __doc__,
  15.     sys.exit(1)

  16. user = "root"
  17. smmip = opts[1]
  18. smmpwd = opts[3]
  19. slot = opts[5]

  20. def mkpwd(str):
  21.         print str
  22.         li = str.split(' ')
  23.         times = li[-1].split(':')
  24.         passwd = 'd pa'+times[1]+times[0]
  25.         return passwd

  26. def telnet(smmip,passwd):
  27.     child=pexpect.spawn('telnet %s 1101'%smmip,timeout=5)
  28.     index = child.expect([".*user",pexpect.TIMEOUT,"Reach Max Connect Count"])
  29.     if index == 1:
  30.        time.sleep(3)
  31.        child.sendline('telnet %s 1101'%smmip)
  32.        index = child.expect([".*user",pexpect.TIMEOUT])
  33.        if index == 1:
  34.           print child.before
  35.           print 'cant telnet to %s '%smmip
  36.           sys.exit(1)
  37.     if index == 2:
  38.        print "Reach Max Connect Count!Connection closed by foreign host"
  39.        sys.exit(1)
  40.     child.sendline("root\r")
  41.     child.expect(".*ord")
  42.     child.sendline('%s\r'%passwd)
  43.     index = child.expect([".*choice",".*user",pexpect.TIMEOUT])
  44.     if index == 1:
  45.        time.sleep(1)
  46.        child.sendline("root\r")
  47.        child.expect(".*ord")
  48.        child.sendline('%s\r'%passwd)
  49.        index = child.expect([".*choice",".*user",pexpect.TIMEOUT])
  50.        if not index == 0:
  51.           print 'smm password %s is wrong!'%passwd
  52.           sys.exit(1)
  53.     if index == 2:
  54.        print "login failed:"
  55.        print child.before
  56.        sys.exit(1)
  57.     return child

  58.    
  59. try:
  60.     child = telnet(smmip,smmpwd)
  61.     child.sendline('%s\r'%slot)
  62.     index = child.expect(["COM","the command times out",pexpect.TIMEOUT])
  63.     if index == 1:
  64.        print "the blade is not present!"
  65.        sys.exit(1)
  66.     if index == 2:
  67.        print "choose slot failed : "
  68.        print child.before
  69.        sys.exit(1)
  70.     child.sendline("2\r")
  71.     index = child.expect(["KVM","root>","debug>"])
  72.     if index == 0:
  73.        print "The blade is performing KVM connection!"
  74.        sys.exit(1)
  75.     time.sleep(1)
  76.     child.sendline("\n")
  77.     index = child.expect(["root>","debug>"])
  78.     if index == 1:
  79.        print '12'
  80.    
  81.     elif index == 0:
  82.        child.sendline("debug\r")
  83.        child.expect('Current.*')
  84.        print child.before
  85.        debugpwd = mkpwd(child.after)
  86.        print debugpwd
  87.        child.sendline(debugpwd+"\r")
  88.        child.expect("debug>")
  89.     child.sendline("io")
  90.     child.expect([pexpect.EOF,pexpect.TIMEOUT])
  91.     print child.before
  92.     print 'sen s'
  93.     child.sendline('sen s')
  94.     child.expect([pexpect.EOF,pexpect.TIMEOUT])
  95.     print child.before
  96.     print 'd 20000f'
  97.     child.sendline('d 20000f')
  98.     child.expect([pexpect.EOF,pexpect.TIMEOUT])
  99.     print child.before
  100.     child.close()       
  101.     sys.exit(0)
  102. except Exception, e:
  103.         print e
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-05-25 16:25 |只看该作者
大哥,给点注释,说明什么的啊,我等新手看起来很吃力
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP