小程序

  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
复制代码

作者: chinaboywg   发布时间: 2011-05-24

大哥,给点注释,说明什么的啊,我等新手看起来很吃力

作者: e2fsck   发布时间: 2011-05-25