免费注册 查看新帖 |

Chinaunix

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

初学perl,写一个拓扑检测小工具,但是出现一个搞不定的错误. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-02 00:00 |只看该作者 |倒序浏览
下面的代码有点长,可能看起来有些费力,我先稍微解释一下.
我首先从input_path_file里面先取出得到的路径信息,然后取得这个路径的目的ip地址.然后使用traceroute -I再作一次traceroute操作.
然后将这次traceroute得到的结果与上次的比较,如果是一个不能被traceroute到的路由器,那么会看看原来的结果里面,这个路由器是不是已经找到了,如果找到了则打印出来.

#!/usr/bin/perl

use strict;
use warnings;

my $arg_num = 0;
my $black_node = 0;
my $white_node = 0;
my $all_node = 0;
my $host_ip;

$arg_num = @ARGV;

#check parameter
if ($arg_num != 2)
{
    print "usage:$0 input_path_file output_path_file\n";
    exit 1;
}

my $input_path_file = $ARGV[0];
my $output_path_file = $ARGV[1];

#open for read
open(PATH_LIST, "<$input_path_file") or die("could not open $input_path_file for read\n");

#open for write
if (-e $output_path_file)
{
    `rm -f $output_path_file`;
}

open(OUTPUT_FILE, ">$output_path_file") or die ("could not open $output_path_file for write\n");

#get this host's address
my $result = `ifconfig eth0`;
chomp($result);
$result =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
$host_ip = $1;

while (1)
{
    my $list = <PATH_LIST>;

    if (not defined($list))
    {
        last;
    }

    if ($list =~ /(.+):(.+)/)
    {
        my @org_routers = split /:/, $2;
        my $ip = $1;
        my $index = 0;
        #print destination ip
        print OUTPUT_FILE "$ip:";
        #print source's ip
        print OUTPUT_FILE "$host_ip ";

        my @new_routers = `traceroute -I -w 3 -m 8 $ip`;

        for ($index = 0; $index <= $#new_routers; $index++)
        {
            if ($new_routers[$index] =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
            {
                #print white node
                print OUTPUT_FILE "$1 ";
                $white_node ++;
            }
            else
            {
                my $new_index = $index + 1;
                print "compared with orignal node:$org_routers[$new_index]:$new_index\n";#不知道为什么$rog_routers[$new_index]打印不出来
                if ($org_routers[$new_index] =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)#问题就出在这里,具体的报错信息请看下面.这句的意思是想看看原来的路径信息里面是否有路由器的ip地址存在
                {
                    #print white node
                    print OUTPUT_FILE "$1 ";
                    print "get extra white node\n";
                    $white_node ++;
                }
                else
                {
                    #print black node
                    print OUTPUT_FILE "* ";
                    $black_node ++;
                }
            }

            $all_node ++;
        }

        #next line
        print OUTPUT_FILE "\n";
    }

}

print OUTPUT_FILE "--------------------------------------------------------\n";
print OUTPUT_FILE "total->$all_node white->$white_node black->$black_node\n"

出现的错误如下:
traceroute to 202.205.250.128 (202.205.250.128), 8 hops max, 40 byte packets
Use of uninitialized value in concatenation (.) or string at ./icmp_traceroute.pl line 73, <PATH_LIST> line 1.
compared with orignal node::1
Use of uninitialized value in pattern match (m//) at ./icmp_traceroute.pl line 74, <PATH_LIST> line 1.
Use of uninitialized value in concatenation (.) or string at ./icmp_traceroute.pl line 73, <PATH_LIST> line 1.
compared with orignal node::2
Use of uninitialized value in pattern match (m//) at ./icmp_traceroute.pl line 74, <PATH_LIST> line 1.
Use of uninitialized value in concatenation (.) or string at ./icmp_traceroute.pl line 73, <PATH_LIST> line 1.
compared with orignal node::3
Use of uninitialized value in pattern match (m//) at ./icmp_traceroute.pl line 74, <PATH_LIST> line 1.
Use of uninitialized value in concatenation (.) or string at ./icmp_traceroute.pl line 73, <PATH_LIST> line 1.
compared with orignal node::4
Use of uninitialized value in pattern match (m//) at ./icmp_traceroute.pl line 74, <PATH_LIST> line 1.
Use of uninitialized value in concatenation (.) or string at ./icmp_traceroute.pl line 73, <PATH_LIST> line 1.
compared with orignal node::5
Use of uninitialized value in pattern match (m//) at ./icmp_traceroute.pl line 74, <PATH_LIST> line 1.
traceroute to 202.205.250.129 (202.205.250.129), 8 hops max, 40 byte packets
Use of uninitialized value in concatenation (.) or string at ./icmp_traceroute.pl line 73, <PATH_LIST> line 2.
compared with orignal node::1
Use of uninitialized value in pattern match (m//) at ./icmp_traceroute.pl line 74, <PATH_LIST> line 2.
traceroute to 202.205.250.130 (202.205.250.130), 8 hops max, 40 byte packets
Use of uninitialized value in concatenation (.) or string at ./icmp_traceroute.pl line 73, <PATH_LIST> line 3.
compared with orignal node::1
Use of uninitialized value in pattern match (m//) at ./icmp_traceroute.pl line 74, <PATH_LIST> line 3.

输入文件如下:
202.205.250.128:202.205.252.198 * 202.112.42.101 202.112.42.146 202.222.222.222 202.222.222.222 202.222.222.222 * *
202.205.250.129:202.205.252.198 202.222.222.222 202.112.42.101 202.112.42.146
202.205.250.130:202.205.252.198 202.222.222.222 202.112.42.101 202.112.42.146 202.205.250.130

还请各位大侠多多指教,不胜感激.

论坛徽章:
0
2 [报告]
发表于 2007-09-02 00:09 |只看该作者
我刚才搜索了一下出错信息,问题应该是出在$org_routers[$new_index]为undef,我试了一下@org_routers是能打印出来的.但是为什么$org_routers[$index]却打印不出来呢?

论坛徽章:
0
3 [报告]
发表于 2007-09-03 09:32 |只看该作者
if ($list =~ /(.+).+)/)
    {
        my @org_routers = split /:/, $2;
        my $ip = $1;
        my $index = 0;

这里怎么用[:]split? 我看你输入文件都是空格隔开的呀。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP