#!/usr/local/bin/expect # tracepath tcl tool. calculates the AS path over a traceroute. # inspired by the broken PRIDE tool "prtraceroute" after a network # mapping project sucked it up. # Copyright 2002 Jose Nazario # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # adjust for your system and nearest IRR server. set serv rr.arin.net set tracecmd "traceroute -w 5 -f 7 -n" # go to the IRR server (in $serv) and get the origin of the IP. yes, # this is horrifically slow, resource intensive, and abusive to the # server because of the multiple lookups, but its the most reliable # way to do this behind an aggressive firewall (with short timeouts, # where keepalives get lost). proc irrLookup {addr} { global serv set found 0 set them "" set sock [socket $serv 43] puts $sock "$addr" puts $sock "" flush $sock set data [read $sock] close $sock foreach piece [split $data "\n"] { if [regexp ^origin $piece] { set them [lindex $piece 1] set found 1 } if {$found} { break } } return $them } # main loop and body. set timeout 120 spawn sh send "$tracecmd [lindex $argv 0]\r" expect {\$ $} set output $expect_out(buffer) puts "calculating AS path ..." # the first 2 lines and last line of the traceroute command # are of no interest to us. set i 0 set ippath "" foreach line [split $output \n] { if {$i<2} { incr i 1 continue } if {$i==[expr [llength $line] - 1]} { break } set ip [lindex $line 1] if {[string length $ip] > 1} { lappend ippath $ip } } set tracepath [lrange $ippath 1 end] set aspath "" foreach addr $tracepath { set as [irrLookup $addr] lappend aspath $as } puts "AS path is: $aspath" # EOF