#!/usr/bin/env python # # Do a traceroute using a looking glass server # Brian Landers # # $Id: lgtrace.txt,v 1.1 2002/11/17 05:17:02 blanders Exp $ # import telnetlib import sys # # NOTE: please be polite. AT&T kindly provides a publically-available # router open to telnet. Don't hammer it with the script or they # might reconsider their generosity. # # Needless to say, I'm not responsible for any trouble you might # get into by using this script. AT&T doesn't seem to have any # terms of service on the router's MOTD precluding scripts like # this, but just in case, you use this script solely at your own # risk. # kSERVER = 'route-server.ip.att.net' kPROMPT = 'route-server>' kTIMEOUT = 15 # Get the IP or hostname to telnet to if len( sys.argv ) != 2: sys.err.write( "usage: lgtrace \n" ); sys.exit(1) print "Connecting to server %s..." % kSERVER tn = telnetlib.Telnet( kSERVER ) print "Waiting for router prompt..." tn.read_until( kPROMPT, kTIMEOUT ) print "Sending command..." tn.write( "trace " + sys.argv[1] + "\n" ) print "Here's the output..." # Skip ahead in the output to the actual trace # FIXME: handle errors line = tn.expect( ["Tracing the route.+\n"], kTIMEOUT ) # List of RE's to match against xp = [ "\n", kPROMPT ] # Read the first line of the trace line = tn.expect( xp, kTIMEOUT ) while line: # If we matched the prompt, we're done if line[0] == 1: break # Write the current line and read again sys.stdout.write( line[2] ) sys.stdout.flush() line = tn.expect( xp, kTIMEOUT ) # Close the telnet connection tn.close()