Chinaunix首页 | 论坛 | 博客
  • 博客访问: 167672
  • 博文数量: 45
  • 博客积分: 622
  • 博客等级: 中士
  • 技术积分: 400
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-02 01:43
文章分类

全部博文(45)

文章存档

2012年(45)

分类:

2012-08-15 16:56:23

在做后台的自动化测试中,我们可能会遇到一些中断服务器网络的情况,来测试程序在这种网络异常情况下的可靠性和健壮性。那之前采用的简单易用的方法是到机房去,将对应服务器的网线直接拔掉,这种效率低的方法无法满足自动化测试的需求。
我们借助python telnetlib库提供的方法,可以有效的解决这个问题。

点击(此处)折叠或打开

  1. import time
  2. import telnetlib
  3. class OperateSw:
  4.     def swLogin(swip):
  5.         try:
  6.             handler = telnetlib.Telnet(swip)
  7.         except Exception:
  8.             return {'rc':-1}
  9.         handler.read_until("Password: ")
  10.         handler.write("******\n")
  11.         handler.write("enable\n")
  12.         handler.read_until("Password: ")
  13.         handler.write("******\n")
  14.         return {'rc':0,'result':handler}
  15.     swLogin = staticmethod(swLogin)
  16.     def swShutPort(handler,port):
  17.         if OperateSw.swPortIsUp(handler,port):
  18.             handler.write("conf terminal\n")
  19.             handler.write("int %s\n" % port)
  20.             handler.write("shutdown\n")
  21.             handler.write("end\n")
  22.             handler.write("write\n")
  23.         else:
  24.             return True
  25.         i = 0
  26.         while OperateSw.swPortIsUp(handler,port):
  27.             time.sleep(1)
  28.             i += 1
  29.             if i > 30:
  30.                 return False
  31.         return True
  32.     swShutPort = staticmethod(swShutPort)
  33.     def swNoShutPort(handler,port):
  34.         if not OperateSw.swPortIsUp(handler,port):
  35.             handler.write("conf terminal\n")
  36.             handler.write("int %s\n" % port)
  37.             handler.write("no shutdown\n")
  38.             handler.write("end\n")
  39.             handler.write("write\n")
  40.         else:
  41.             return True
  42.         i = 1
  43.         while OperateSw.swPortIsUp(handler,port) == False:
  44.             time.sleep(1)
  45.             i += 1
  46.             if i > 30:
  47.                 return False
  48.         return True
  49.     swNoShutPort = staticmethod(swNoShutPort)
  50.     
  51.     def swPortIsUp(handler,port):
  52.         handler.write("show int %s\n" % port)
  53.         handler.write(" ")
  54.         time.sleep(1)
  55.         string = handler.read_very_eager()
  56.         if string.find("connected") != -1:
  57.             return True
  58.         elif string.find("down") != -1:
  59.             return False
  60.     swPortIsUp = staticmethod(swPortIsUp)
  61.     
  62. class NetActions:
  63.     def noShutSwPort(swip,swport):
  64.         __login = OperateSw.swLogin(swip)
  65.         if __login['rc'] == -1:
  66.             return {'rc':-1,'result':"Can't login switch %s"%swip}
  67.         if OperateSw.swNoShutPort(__login['result'], swport):
  68.             return {'rc':0}
  69.         else:
  70.             return {'rc':-1,'result':"Execute Switch configuration command failed"}
  71.     noShutSwPort = staticmethod(noShutSwPort)
  72.     
  73.     def shutSwPort(swip,swport):
  74.         __login = OperateSw.swLogin(swip)
  75.         if __login['rc'] == -1:
  76.             return {'rc':-1,'result':"Can't login switch %s"%swip}
  77.         if OperateSw.swShutPort(__login['result'], swport):
  78.             return {'rc':0}
  79.         else:
  80.             return {'rc':-1,'result':"Execute Switch configuration command failed"}
  81.     shutSwPort = staticmethod(shutSwPort)
  82.     
  83. if __name__=="__main__":
  84.     MyAction = NetActions()
  85.     #MyAction.noShutSwPort("10.224.29.147","f0/6")
  86.     MyAction.shutSwPort("10.224.29.147","f0/6")

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