tracemap
changeset 0:0d498f80f832 tip
Repository initialization.
author | Igor Chubin <igor@chub.in> |
---|---|
date | Sun Jun 29 17:36:44 2008 +0300 (2008-06-29) |
parents | |
children | |
files | tracemap.pl xguru_fun |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tracemap.pl Sun Jun 29 17:36:44 2008 +0300 1.3 @@ -0,0 +1,218 @@ 1.4 +#!/usr/bin/perl -w 1.5 +# 1.6 +use Net::IP qw(:PROC); 1.7 + 1.8 +our $traceroute_options="-q1 -n -I"; 1.9 +our $hops_to_highlight=4; 1.10 +our $verbose =1; 1.11 +our $prefixes="prefixes.txt"; 1.12 +our $scale=1; 1.13 + 1.14 +our $dot_file="tracemap.dot"; 1.15 +our $svg_file="tracemap.svg"; 1.16 +our $png_file="tracemap.png"; 1.17 + 1.18 +#our @printed_hops; 1.19 +our $graph=""; 1.20 +our $hop_id = 0; 1.21 +our %hop_id; 1.22 + 1.23 +our $previous_hop; 1.24 +our $previous_time; 1.25 +our $hop; 1.26 +our $time; 1.27 +our $ttl; 1.28 + 1.29 +our %color_table=qw( 1.30 +194.150.0.0/16 red 1.31 +10.0.0.0/16 seagreen2 1.32 +10.1.0.0/16 seashell2 1.33 +10.2.0.0/16 sienna2 1.34 +10.3.0.0/16 skyblue2 1.35 +10.4.0.0/16 slateblue2 1.36 +10.5.0.0/16 slategray2 1.37 +10.6.0.0/16 snow1 1.38 +10.7.0.0/16 springgreen1 1.39 +10.8.0.0/16 steelblue1 1.40 +); 1.41 + 1.42 +our @prefixes; 1.43 + 1.44 +sub load_prefixes 1.45 +{ 1.46 + my @prefixes=(); 1.47 + open(PREFIXES, $prefixes); 1.48 + while (<PREFIXES>) { chomp; push @prefixes, $_;}; 1.49 + close(PREFIXES); 1.50 + return @prefixes; 1.51 +} 1.52 + 1.53 +sub ip_in_range 1.54 +{ 1.55 + my $ip=shift; 1.56 + my $range_found=0; 1.57 + for my $range (@_) { 1.58 + my ($range_ip, $range_prefix) = split m@/@, $range; 1.59 + my ($ip1, $ip2) = ip_prefix_to_range($range_ip, $range_prefix,4); 1.60 +# print "$range\n"; 1.61 + $range_found=ip_bincomp(ip_iptobin($ip1,4),'lt',ip_iptobin($ip,4)) 1.62 + && ip_bincomp(ip_iptobin($ip,4),'lt',ip_iptobin($ip2,4)); 1.63 + if ($range_found) { 1.64 + return $range; 1.65 + } 1.66 + } 1.67 + return 0; 1.68 +} 1.69 + 1.70 +sub color_of_ip($) 1.71 +{ 1.72 + my $ip = shift; 1.73 + for $range (keys %color_table) { 1.74 + if (ip_in_range($ip,$range)) { 1.75 + return $color_table{"$range"}; 1.76 + } 1.77 + } 1.78 + return "indigo"; 1.79 +} 1.80 + 1.81 +sub print_hop($) 1.82 +{ 1.83 + $hop = shift; 1.84 + unless (defined($hop_id{$hop})) { 1.85 + $hop_id++; 1.86 + $color=""; 1.87 + $shape=""; 1.88 + if (ip_in_range($hop, @prefixes)) { 1.89 + $color=",color=green" 1.90 + } 1.91 + $color=",color=".color_of_ip($hop); 1.92 + if ($ttl == $hops_to_highlight+1) { 1.93 + $shape=",shaper=rectangle" 1.94 + } 1.95 + $graph .= "hop$hop_id [ label=\"$hop\"$color$shape];"; 1.96 + $hop_id{$hop} = $hop_id; 1.97 + } 1.98 +} 1.99 + 1.100 +sub time_to_len($$) 1.101 +{ 1.102 + $a=shift; 1.103 + $b=shift; 1.104 + $l=$a-$b; 1.105 + $l = 0.25 if $l<0.25; 1.106 + return (3+1.2*log($l))*0.3285*$scale; 1.107 +} 1.108 +sub say 1.109 +{ 1.110 + print @_ if $verbose; 1.111 +} 1.112 + 1.113 +sub ping_sweep($) 1.114 +{ 1.115 + my $net = shift; 1.116 + my @res; 1.117 + say("Doing ping sweep of the $net IP-addresses block"); 1.118 + open(NMAP, "nmap -sP $net|grep 'appears to be up' | awk '{print \$2}'|") 1.119 + or die "can't make ping sweep of $net. Have you nmap installed?\n$!"; 1.120 + while (<NMAP>) { 1.121 + chomp; 1.122 + push (@res, ($_)); 1.123 + say("."); 1.124 + } 1.125 + say("Done. $#res host(s) found.\n"); 1.126 + return @res; 1.127 +} 1.128 + 1.129 +sub dns_axfr($) 1.130 +{ 1.131 + my @res; 1.132 + my ($zone,$ns) = split /@/; 1.133 + if ($ns eq '') { 1.134 + } 1.135 + else 1.136 + { 1.137 + say("Doing axfr of the zone $zone from the server $ns\n"); 1.138 + open(DIG, "dig \@$ns axfr $zone | awk '/\tA\t/ {print \$1}' |") 1.139 + or die "can't make axfr of the zone $zone from the server $ns. Have you dig installed?\n$!"; 1.140 + while (<DIG>) { 1.141 + chomp; 1.142 + push (@res, ($_)); 1.143 + } 1.144 + say("Done. $#res host(s) found.\n"); 1.145 + return @res; 1.146 + } 1.147 +} 1.148 + 1.149 +my @dests=(); 1.150 +while(<>) 1.151 +{ 1.152 + chomp; 1.153 + next if /^#/; 1.154 + if (m@http:@) { 1.155 + } 1.156 + elsif (m@/@) { 1.157 + push(@dests,ping_sweep($_)) 1.158 + } 1.159 + elsif (m'@') { 1.160 + push (@dests,dns_axfr($_)) 1.161 + } 1.162 + else { 1.163 + push(@dests,($_)); 1.164 + } 1.165 +} 1.166 + 1.167 +@prefixes=load_prefixes(); 1.168 + 1.169 +for $dest (@dests) 1.170 +{ 1.171 + say("Tracing path to $dest"); 1.172 + open (TRACE,"traceroute $traceroute_options $dest|") 1.173 + or die "Can't run traceroute:$!"; 1.174 + my $hop=''; 1.175 + $ttl=0; 1.176 + my $total=0; 1.177 + while (<TRACE>) { 1.178 + chomp; 1.179 + next if not /ms/; 1.180 + say("."); 1.181 + $ttl++; 1.182 + s@^\s*@@; 1.183 + $previous_hop=$hop; 1.184 + $previous_time=$time; 1.185 + my $a; 1.186 + ($a, $hop, $time) = split(/\s+/, $_, 3); 1.187 + $time =~ s/\s*ms\s*$//; 1.188 + $total += $time; 1.189 + if ($previous_hop eq '') { 1.190 + print_hop($hop); 1.191 + $graph =~ s@color=[^],]*[],]@@; 1.192 + $graph =~ s@(\ label=\")[^"]*(\".*?)$@fontsize=10,color=red,style=filled,label="(YOU)$2@; 1.193 + } 1.194 + elsif (not defined $hop_id{$hop} ) { 1.195 + print_hop($previous_hop); 1.196 + print_hop($hop); 1.197 + $graph .= "hop".$hop_id{$previous_hop}." -- hop".$hop_id{$hop}. 1.198 + " [label=\"".sprintf("%1.2f",+$time-$previous_time)."\",len=".time_to_len($time,$previous_time)."];\n"; 1.199 + } 1.200 + } 1.201 + $graph =~ s@(\ label=\")[^"]*(\".*?)$@fontsize=8,style=filled,label=\"$dest$2@; 1.202 + close(TRACE); 1.203 + say("Done [last $time, total $total]\n"); 1.204 +} 1.205 +#$temp=rand.rand; 1.206 + 1.207 +open (DOT, ">$dot_file") 1.208 + or die "can;t open dot file file for writing: $!"; 1.209 +print DOT <<EOF; 1.210 +graph G { 1.211 + margin =1 ; 1.212 + node [fontname=times,fontsize=7,shape=circle,width=0.1,height=0.1,fixedsize=true,color=blue]; 1.213 + edge [fontname=times,fontsize=5,color=black]; 1.214 +$graph 1.215 +} 1.216 +EOF 1.217 +close(DOT); 1.218 + 1.219 +print `neato -o $png_file -Tpng $dot_file > /dev/null 2>&1; neato -o $svg_file -Tsvg $dot_file > /dev/null 2>&1 ` 1.220 + or die "can't run neato: $!;\ngraphviz is installed?\nYou can tracemap.dot to the hosts where graphviz in installed and run\nneato -o tracemap.png -Tpng tracemap.dot" 1.221 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/xguru_fun Sun Jun 29 17:36:44 2008 +0300 2.3 @@ -0,0 +1,12 @@ 2.4 +get_mx() 2.5 +{ 2.6 + dig "$1" mx | grep -v '^;' | grep MX | sort -nk 5 | head -1 | awk '{print $6}' | sed s/.$// 2.7 +} 2.8 +get_ttl() 2.9 +{ 2.10 + traceroute -q1 -n "$1" | grep ms | wc -l 2.11 +} 2.12 +get_location() 2.13 +{ 2.14 + lynx -dump 'http://www.geoiptool.com/en/?IP='$1 | egrep 'Longitude|Latitude|City|Country|Region' 2.15 +}