Chinaunix首页 | 论坛 | 博客
  • 博客访问: 59909
  • 博文数量: 17
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-07 11:14
个人简介

。roth lower(substr(ename,2,length(ena)))

文章分类

全部博文(17)

文章存档

2017年(14)

2013年(3)

我的朋友

分类: Windows平台

2013-09-11 11:40:05

感谢版主,
在此记录下一个方法,上一个博文中提到的问题1,后来并没有去检查具体的返回结果,而是与ping 127.0.0.1的返回值进行对比,127.0.0.1肯定是通的,如果对比结果一直,则为alive状态,如果不一致则为down状态,如此便可正确判断设备的联通性了,同时实现的功能做了一些优化,ip地址和数目可以手动输入到文件中,如果ping包数没有人工输入则使用默认值

点击(此处)折叠或打开

  1. file = open(r'D:\test_case\ip.txt','r') #读取要检查的ip地址
  2. lines=file.readlines()
  3. file.close()

  4. source = r'D:\iptmp.txt'
  5. ks = int(time.time()) #记录开始时间
  6. for ip in lines:
  7.     ip_list=ip.replace('\n','')
  8.     ip_new=ip_list.split(',')
  9.     ip = ip_new[0]
  10.     new_num = int(ip_new[1])
  11.     if new_num > 0: #检查输入的num值,并赋予新值
  12.         ip_num = new_num
  13.         pass
  14.     ret=subprocess.call('ping %s -w 1 -n %d ' %(ip,ip_num),shell = True,stdout=open(r'D:\test_case\iptmp.txt','w'), stderr=subprocess.STDOUT)
  15.     ret1=os.system('ping -n %d -w 1 127.0.0.1' %ip_num)
  16.    
  17.     if ret == ret1: #对比两种结果并作出在时间范围内设备的连接状态判断,备份检查过程
  18.             print ("at time from %d to %s %s: is alive" % (ks,js,ip))
  19.             js = time.strftime('%d%H%M%S')
  20.             bak_command = "copy /B %s %s" %(source,target)
  21.             if os.system(bak_command) == 0:
  22.                 print ('Successful backup to', target)
  23.             else:
  24.                 print ('Backup FAILED' )
  25.     else:
  26.             print ("at time from %d to %s %s is down" % (ks,js,ip))
  27.             target =today_path+os.sep+ip+'_'+js+'---fail---'+'.txt'
  28.             bak_command = "copy /B %s %s" %(source,target)
  29.             if os.system(bak_command) == 0:
  30.                 print ('Successful backup to', target)
  31.             else:
  32.                 print ('Backup FAILED' )



运行结果:
     at time from 1378870460 to 11113459 192.168.12.100: is alive
      at time from 1378870460 to 11113547 192.168.12.102 is down
备份的ping操作过程
第一个文件(192.168.12.100_11113459---ok---.txt):
正在 Ping 192.168.12.100 具有 32 字节的数据:
....此处省略19条
来自 192.168.12.100 的回复: 字节=32 时间<1ms TTL=64

192.168.12.100 的 Ping 统计信息:
    数据包: 已发送 = 20,已接收 = 20,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

第二个文件(192.168.12.102_11113547---fail---.txt):
正在 Ping 192.168.12.102 具有 32 字节的数据:
...此处省略19条
请求超时。

192.168.12.102 的 Ping 统计信息:
    数据包: 已发送 = 20,已接收 = 0,丢失 = 20 (100% 丢失),


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