traceroute.pl 2.47 KB
Newer Older
Olav Kvittem's avatar
Olav Kvittem committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#! /usr/bin/perl
#
# run mtr and produce json document
#  usage : traceroute.json?host=host_name_or_address&count=repetitions
#  returns json-document with mtr output
#  Olav Kvittem ( Olav.Kvittem@uninett.no )
#
# use CGI;
use CGI qw/:standard -debug/;
use JSON;
my $q = CGI->new;

print $q->header('application/json');

$feilmelding = "<h3><font color=red>%s</font></h3>\n";

if ( $q->http('HTTP_X_TOKEN') ne "virre-virre-vapp"){
    push(@mtr, "Error" , "Access to traceroute denied : unauthorized");
    print STDERR "Error: "; 

    my %headers = map { $_ => $q->http($_) } $q->http();
    print STDERR "Wrong secret : Got the following headers:\n";
    for my $header ( keys %headers ) {
	print STDERR "$header: $headers{$header}\n";
    }
} else {

# print STDERR 'host', $q->param('host'),  $q->param('nobs'),  $q->param('psize');

Olav Kvittem's avatar
Olav Kvittem committed
30
    my $host   = $q->param('host'); # && ( $host =~ m/^[a-zA-ZæøåÆØÅ0-9\.\/\-_]+$/);
Olav Kvittem's avatar
Olav Kvittem committed
31 32 33
    my $nobs   = $q->param('nobs'); # && ( $nobs =~ m/^[0-9]+$/ );
    my $psize  = $q->param('psize'); # && ( psize =~ m/^[0-9]+$/ );
    my $interval  = $q->param('interval'); # && ( psize =~ m/^[0-9]+$/ );
34
    my $ipprot = $q->param('ipprot');
Olav Kvittem's avatar
Olav Kvittem committed
35
    $host  =~ s/[^a-zA-ZæøåÆØÅ0-9\.\/\-_\:]+/_/g;  # protect 
Olav Kvittem's avatar
Olav Kvittem committed
36 37 38 39 40 41 42
    $nobs  =~ s/[^0-9]+/_/g;
    $psize =~ s/[^0-9]+/_/g;

    $host = $q->remote_host if $host eq "";
    $nobs = 3 if $nobs < 1;
    $psize = 64 if $psize < 1;
    $interval= 0.1 if $interval <= 0;
43 44 45 46 47 48 49

    my $ipopt='';
    if($ipprot eq "ipv4"){
	$ipopt='-4';
    } elsif($ipprot eq "ipv6"){
	$ipopt='-6';
    }
50 51
#    my $cmd= "/usr/bin/mtr -c $nobs -s $psize -i $interval --report --report-wide $host";
# mtr does not like interval less than 1sek from nonroot and therefore purposeless
52
    my $cmd= "/usr/bin/mtr -c $nobs -s $psize  --report --report-wide $host $ipopt";
Olav Kvittem's avatar
Olav Kvittem committed
53 54 55 56 57

# open MTR, $cmd || die "Could not open : $cmd : $!";

    my $mtr = `$cmd`;
    if ( $? != 0){
58 59 60
	$error=$!;
	push(@mtr, "Error" , $error || "Might be unknown host");
	printf STDERR "Error: $cmd : $error \n"; 
Olav Kvittem's avatar
Olav Kvittem committed
61 62 63 64 65
    } else { # ok
#	print STDERR "Kode $? : $cmd"; 

#while(<MTR>){
	foreach ( split(/\n/, $mtr) ){
66 67

	    if (/^(Start): (.*)/){ # header line
68
		push( @mtr, [$1, $2, $q->remote_host]);
69 70 71
	    } else {
		s/^\s+//; # leading blanks
		@F = split( /\s+/ );
72 73 74 75 76 77 78
		if ( /^HOST/){
		    $F[0]="Hop";
		    $F[1]="Gateway";
		} else {
		    if ($F[0] = /(\d+)[^\d]+/){
			$F[0]=$1; 
		    }
79 80
		}
		push( @mtr, [@F] );
81
	    }
Olav Kvittem's avatar
Olav Kvittem committed
82 83 84
	}
    }
}
Olav Kvittem's avatar
Olav Kvittem committed
85
print STDERR "mtr result: ".@mtr;
Olav Kvittem's avatar
Olav Kvittem committed
86 87 88 89 90 91 92 93 94 95
print encode_json \@mtr;

exit(0);