Chinaunix首页 | 论坛 | 博客
  • 博客访问: 374948
  • 博文数量: 87
  • 博客积分: 2810
  • 博客等级: 少校
  • 技术积分: 825
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-28 22:34
文章分类

全部博文(87)

文章存档

2010年(25)

2009年(43)

2008年(19)

分类: LINUX

2009-11-06 18:32:12

公司一直需要测试一些新idc节点的网络质量;其中ping值的数据一直是个很有参考价值的指标;
高峰时间内平均ping值数据和丢包率能比较客观反应出该idc网络质量;
今天正好用python写了一个小程序,用来做这方面的工作;

运行环境只用有python就可以,当然还要有ping命令啦~
python 2.4


修正一个bug,去掉了不能ping通的IP地址和多线程支持;
目前版本0.3

使用方法:
默认字符集UTF-8
程序自带帮助应该很容易看懂的~
可以评测电信、网通和教育网络的ping值质量

# ./pingtest_net.py
USEAGE:
        ./testping_net_quality.py -n CNC|CTC|EDU [-t MINs] [-f file]
        CNC 网通线路; CTC 电信线路; EDU 教育网;
        -t MINs 测试的时间;默认为10分钟;
        -f file 输出结果到文件;默认为当前目录文本文件ping.result
        -h|-?, 帮助信息
for example:
        ./testping_net_quality.py -n CTC -t 60
        在测试网络所在主机上执行以上指令表示测试网络为电信,测试时间1小时;
输出结果样式范例:
202.201.48.2   甘肃兰州西北师范大学   23.57   0.00
第一栏IP   第二栏所在地   第三栏该时间段内平均ping的ms值   最后是丢包率

欢迎大家下载抓虫~~
有问题请加入评论,正在考虑增加一些其他的功能;

附程序代码:
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
# File: pingtest_net.py
# Date: 2008-09-28
# Author: Michael Field
# Version: 0.3 Beta
import sys
import os
import getopt
import commands
import re
import time
import threading
CNC={
   '202.108.100.166': '北京网通',
     '218.69.33.1': '天津网通',
     '220.248.112.1': '上海网通',
     '221.192.1.1': '石家庄网通',
     '218.26.176.1': '山西太原网通',
     '60.31.195.1': '呼和浩特网通',
     '218.25.255.1': '辽宁大连网通',
     '221.8.96.1': '吉林长春网通',
     '218.7.249.154': '哈尔滨网通',
     '218.59.169.109': '山东威海网通',
     '202.111.148.1': '河南淮阳网通',
     '58.240.48.43': '江苏南京网通',
     '60.12.193.1': '浙江湖州网通',
     '221.3.131.1': '云南昆明网通',
     '221.10.239.1': '四川成都网通',
     '221.11.1.1': '西安网通',
     '221.7.129.163': '南宁网通',
     '58.22.97.1': '福州网通',
     '221.5.196.1': '重庆网通一',
     '221.5.203.102': '重庆网通二',
     '221.8.18.8': '长春网通',
     '202.97.224.68': '黑龙江网通',
     '202.99.160.1': '河北网通1',
     '202.96.69.38': '大连网通',
     '60.210.17.1': '淄博网通',
     '60.209.5.254': '青岛网通',
     '60.12.166.1': '金华网通',
     '58.19.183.1': '襄樊网通',
     '221.208.172.1': '哈尔滨网通',
     '60.31.255.115': '内蒙古网通',
     '202.99.192.68': '山西网通',
     '202.99.160.68': '河北网通2',
     '222.138.109.1': '开封网通',
     '218.107.56.1': '广东网通',
     '210.52.149.2': '湖北网通',
     '60.18.94.1': '辽宁网通',
     }
CTC={
     '61.152.188.1': '上海电信1',
     '61.129.51.254': '上海电信2',
     '218.1.64.33': '上海热线',
     '61.145.125.229': '广东互联星空_广州电信',
     '61.140.60.90': '广东电信',
     '218.18.104.1': '深圳电信 ',
     '218.16.239.129': '汕头电信',
     '218.75.107.60': '浙江互联星空_杭州电信',
     '61.186.95.92': '湖南互联星空_长沙电信',
     '221.236.17.1': '四川互联星空_成都电信',
     '61.139.33.1': '绵阳电信 ',
     '222.176.2.214': '重庆电信',
     '220.179.251.234': '安徽电信',
     '219.148.197.6': '辽宁沈阳电信',
     '219.146.11.20': '山东互联星空_济南电信',
     '58.56.19.129': '山东电信',
     '222.173.123.1': '青岛电信 ',
     '219.148.62.194': '河北石家庄电信',
     '58.53.192.150': '湖北互联星空_湖北电信',
     '218.77.178.229': '海南互联星空_海口电信',
     '219.141.62.111': '贵州互联星空_贵阳电信',
     '61.166.150.110': '云南互联星空_曲靖电信',
     '218.30.85.83': '陕西互联星空_西安电信',
     '218.30.64.121': '北京互联星空_西安电信',
     '61.134.40.138': '陕西渭南 ',
     '60.164.225.1': '甘肃互联星空_甘肃电信',
     '222.217.99.18': '广西互联星空_南宁电信',
     '202.100.128.201': '青海互联星空_西宁电信',
     '202.100.109.157': '宁夏互联星空_石嘴山电信',
     '219.150.150.1': '河南互联星空_郑州电信',
     '221.238.193.1': '天津互联星空_天津电信',
     '202.109.204.150': '福建福州电信',
     '220.175.8.1': '江西电信',
     '61.186.95.92': '湖南电信',
     '60.190.223.1': '绍兴电信 ',
     '219.150.150.150': '河南电信 ',
     '219.150.32.184': '天津电信  ',
     '61.139.37.1': '南充电信',
     '219.149.194.31': '吉林互联星空_长春电信',
     '219.147.130.76': '黑龙江互联星空_哈尔滨电信',
     '220.182.54.199': '西藏互联星空_拉萨电信',
     '61.128.101.1': '新疆互联星空_阿克苏电信',
     '202.103.0.117': '武汉电信',
     '202.175.3.3': '澳门电信',
    }
EDU={
     '202.112.128.1': '北京航空航天大学',
     '166.111.8.28': '北京清华大学',
     '202.114.0.242': '湖北武汉华中科技大学',
     '202.117.0.20': '陕西西安交通大学',
     '202.112.26.34': '上海交通大学',
     '202.203.128.33': '云南昆明教育网',
     '202.115.64.33': '四川成都西南交通大学',
     '202.201.48.2': '甘肃兰州西北师范大学',
     '202.114.0.242': '华中科大',
     '202.116.160.33': '广州华南农业大学',
     '202.202.128.33': '重庆医科大一',
     }
RESULT={}
def usage():
    print "USEAGE:"
    print "\t%s -n CNC|CTC|EDU [-t MINs] [-f file]" %sys.argv[0]
    print "\tCNC 网通线路; CTC 电信线路; EDU 教育网;"
    print "\t-t MINs 测试的时间;默认为10分钟;"
    print "\t-f file 输出结果到文件;默认为当前目录文本文件ping.result"
    print "\t-h|-?, 帮助信息"
    print "for example:"
    print "\t./testping_net_quality.py -n CTC -t 60"
    print "\t在测试网络所在主机上执行以上指令表示测试网络为电信,测试时间1小时;"
def pin(IP):
    xpin=commands.getoutput("ping -c1 %s" %IP)
    ms='time=\d+.\d+'
    mstime=re.search(ms,xpin)
    if not mstime:
        MS='timeout'
        return MS
    else:
        MS=mstime.group().split('=')[1]
        return MS
def count(min,I):
    global RESULT
    nowsecond = int(time.time())
    second = min * 60
    endtime = nowsecond + second
    nums = 0
    oknums = 0
    timeout = 0
    lostpacket = 0.0
    total_ms = 0.0
    avgms = 0.0
    while nowsecond <= endtime:
        nums += 1
        MS = pin(I)
        if MS == 'timeout':
            timeout += 1
            lostpacket = timeout*100.0 / nums
        else:
            oknums += 1
            total_ms = total_ms + float(MS)
            if oknums == 0:
                oknums = 1
                avgms = total_ms / oknums
            avgms = total_ms / oknums
        RESULT[I]=(I,avgms,lostpacket)
        nowsecond = int(time.time())
if __name__ == '__main__':
    file = 'ping.result'
    mins = 10
    network = ''
    args = sys.argv[1:]
    try:
        (opts, getopts) = getopt.getopt(args, 'n:f:t:h?')
    except:
        print "\nInvalid command line option detected."
        usage()
        sys.exit(1)
    for opt, arg in opts:
        if opt in ('-n'):
            network = arg
        if opt in ('-h', '-?'):
            usage()
            sys.exit(0)
        if opt in ('-f'):
            file = arg
        if opt in ('-t'):
            mins = int(arg)
    if os.path.dirname(file):
        if os.path.exists(os.path.dirname(file)):
            f = open(file, 'w')
        else:
            print "File's path is wrong. please check it."
            usage()
            sys.exit(0)
    else:
        f = open(file, 'w')
    if network not in ['CTC','CNC','EDU'] or not isinstance(mins,int):
        usage()
        sys.exit(0)
    else:
        if network == 'CTC':
            ips = CTC
        if network == 'CNC':
            ips = CNC
        if network == 'EDU':
            ips = EDU
        print 'Starting...'
        threads = []
        loops = range(len(ips)) 
        print 'Total %s Threads is working...' %len(ips)
        for IP in ips:
            t = threading.Thread(target=count,args=(mins,IP))
            threads.append(t)
        for i in loops:
            threads[i].start()
        for i in loops:
            threads[i].join()
        for line in RESULT:
            value = RESULT[line]
            f.write(line + '\t'+ ips[line] + '\t'+ str('%.2f'%value[1]) + '\t'+ str('%.2f'%value[2]) + '\n')
        f.close
        print 'Work Done. please check result %s.'%file
阅读(4584) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~