- 论坛徽章:
- 0
|
下面的代码有点长,可能看起来有些费力,我先稍微解释一下.
我首先从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
还请各位大侠多多指教,不胜感激. |
|