Net2/usr/src/contrib/isode/snmp/gawk-2.11/s-gawk/s-traceroute

: run this script through /bin/sh

agent= community= dest= flags=

for A in $*
do
    case $A in
	-*)	echo "$A: unknown flag" 1>&2
		exit 1 ;;

	*)	if [ "x$dest" = "x" ]; then
		    dest="-v DEST=$A"
		elif [ "x$community" = "x" ]; then
		    community="-v COMMUNITY=$A"
		elif [ "x$agent" = "x" ]; then
		    agent="-v AGENT=$A"
		else
		    echo "usage: s-traceroute [switches] destination [community [agent]]" 1>&2
		    exit 1
		fi ;;
    esac
done

if [ "x$dest" = "x" ]; then
    echo "no destination specified" 1>&2
    exit 1
fi

exec gawk $flags $agent $community $dest '
BEGIN	{
	    printf "from %s to %s:\n", AGENT, DEST;
	    tried[AGENT] = 1;
	    INVALID = 2; DIRECT = 3;

	    split (DEST, dest, ".");

	    if (dest[1] < 128)
		net = dest[1];
	    else
		if (dest[1] < 192)
		    net = dest[1] "." dest[2];
		else
		    net = dest[1] "." dest[2] "." dest[3];

	    while (DEST != AGENT) {
		dr = 0;
		gotit = 0;
		for (i in ipRouteMask, net) {
		    if ((type = ipRouteType) == INVALID)
		        continue;

		    split(ipRouteMask, mask, ".");
		    mask = bit_and(dest[1],mask[1]) "." \
			   bit_and(dest[2],mask[2]) "." \
			   bit_and(dest[3],mask[3]) "." \
			   bit_and(dest[4],mask[4]);
		    if (mask == ipRouteDest) {
			hop = ipRouteNextHop;
			gotit = 1;
			break;
		    }
		}
		if (!gotit) {
		    if ((hop = ipRouteNextHop[addr = DEST]) \
			    && (type = ipRouteType[addr]) == INVALID)
			hop = 0;
		    if (!hop \
			    && (hop = ipRouteNextHop[addr = net]) \
			    && (type = ipRouteType[addr]) == INVALID)
			hop = 0;
		    if (!hop && (hop = ipRouteNextHop[addr = "0.0.0.0"])) {
			if ((type = ipRouteType[addr]) == INVALID)
			    hop = 0;
			else
			    dr = 1;
		    }
		}

		if (hop) {
		    printf "    via %-15s metric %2d%s\n",
			    hop, ipRouteMetric1[addr],
			    dr ? " (default route)" : "";
		}
		else {
		    printf "\nno path to %s from %s\n%s\n", 
			    DEST, AGENT, DIAGNOSTIC;
		    exit(1);
		}

		if (type == DIRECT) {
		    printf "\tdirect route.\n";
		    exit(0);
		}

		if (hop in tried)
		    printf "\nrouting loop!\n";
		else
		    tried[hop] = 1;
		AGENT = hop;
	    }

	    printf "\tdone.\n";
	}
'