[c-nsp] Announcing "ciscoping"

Ed Ravin eravin at panix.com
Wed May 4 19:28:52 EDT 2005


I'm happy to announce the release of "ciscoping", a Unix command-line
utility that allows you to access the various extended options of ping
on older versions of IOS without having to answer all those annoying prompts.
I see that recent versions of IOS let you specify more stuff on the command
line, which means this tool is destined for happy obsolescence one day.

It uses the same technique as aclmaker, in that it can send commands
over an existing Telnet or ssh session, but can also use rsh to reach
the router.  For more information on how that works, please see the
documentation for aclmaker.

ciscoping is attached.  It requires the Perl Expect module.

aclmaker can be found at
http://prdownloads.sourceforge.net/cosi-nms/aclmaker-perl-1.03.txt?download

	-- Ed
-------------- next part --------------
#!/usr/bin/perl -w

# $Id: ciscoping,v 1.2 2005/05/04 21:33:23 root Exp root $

# invokes Cisco's clumsy ping via expect so that we can
# specify what we want in one command line.  Reaches the router
# either via rsh (-r router) or via an existing telnet session

# ciscoping was written by Ed Ravin <eravin at panix.com>, and is made
# available to the public by courtesy of PANIX (http://www.panix.com).
# This script is licensed under the GPL.  B<aclmaker> requires the Expect
# and IO-Tty Perl modules.


use strict;
use Expect;
use Getopt::Std 'getopts';
use vars qw($opt_c $opt_D $opt_F $opt_I $opt_L $opt_p $opt_R $opt_s $opt_S $opt_t $opt_T $opt_v $opt_V $opt_w );
use vars qw($opt_d $opt_E $opt_r);


my $options = "c:DF:I:L:p:Rs:S:t:TvVw:" . "dE:r:";
my $usage="Usage: ciscoping [{-r router | -F fdnum}] [options] host

Interacts with a Cisco router so you can specify most of the ping options
via command line.  Specify \"-r router\" to rsh into the router, or script
will assume you have an open telnet session on file descriptor 3.

Ping options:
   -c count     # how many packets to send [5]
   -D           # don't fragment
   -I interface # source interface
   -L gateway   # loose source route gateway
   -p pattern   # data pattern in hex
   -R           # record route
   -s size      # size of packet [100]
   -S gateway   # strict source route gateway
   -t tos       # type of service
   -v           # verbose
   -V           # validate response
   -w wait      # timeout before sending next packet

Script options:
    -d          # turn on expect debugging [off]
    -E timeout  # set expect timeout [10]
    -F fdnum    # specify file descriptor of the pre-opened Telnet session
";
getopts( $options );

my $rsh="/usr/bin/rsh";

# arguments
my $debug= $opt_d || 0;
my $ExpectTimeout= $opt_E || 10;

# which mode shall we use - rsh or existing telnet session?
my $router= $opt_r || "";
die "$0: options conflict: choose only one of \"-r router\" and \"-F fdnum\".\n"
	if $router ne "" and $opt_F;
my $fdnum= -1;
if ($router eq "")
{
	$fdnum= $opt_F || 3;
}
my $telnetfd;

my $repeatcount= $opt_c || 5;
my $dontfragment= $opt_D ? "y" : "n";	# Ext
my $loosegateway= $opt_L || "";		# Ext
my $strictgateway= $opt_S || "";	# Ext
my $interface= $opt_I || "";		# Ext
my $pattern= $opt_p || "";			# Ext
my $recordroute= $opt_R ? "R" : "";	# Ext
my $size= $opt_s || 100;
my $typeofservice= $opt_t || 0;		# Ext
my $timeout= $opt_w || 2;
my $timestamp = $opt_T ? "T" : "";	# Ext
my $verbose= $opt_v ? "V" : "";		# Ext
my $validate= $opt_V ? "y" : "n";	# Ext

# do we need extended commands?
my $extended= ($opt_D || $opt_I || $opt_L || $opt_p || $opt_R || $opt_S || $opt_t || $opt_T || $opt_v || $opt_V );

# do we need to set them fancy options?
my @fancy=
	($loosegateway ? "L" : "",
	$strictgateway ? "S" : "",
	$recordroute , $timestamp , $verbose);
@fancy= grep /./, @fancy;

my $targetip= $ARGV[0] || die $usage;

my $session;

sub transact # expect-str, send-str
{
	my ($expectme, $sendme)= @_;
	$session->expect($ExpectTimeout, $expectme) ||
		die "$0: failed to match $expectme: " . $session->exp_error() . "\n";
	print $session "$sendme\r";
}


if ($debug) {
	$Expect::Debug=1;
	$Expect::Exp_Internal=1;
}

if ($router ne "")
{
	# spawn an rsh to the router
	$session= Expect->spawn($rsh, $router, "ping");
} else {
	# connect to existing telnet session
	$telnetfd = new IO::File;
	# open fd3 by default ($fdnum=="3"), or use whatever user supplies
	my $fdpath= "+<" . $fdnum;;
	$fdpath= "+<&=$fdnum" if ($fdnum =~ /^[0-9]+$/);
	if (! $telnetfd->open($fdpath)) {
		die("$0: must be run from telnet subshell: cannot open file $fdnum: $!\n");
	}

	die "$0: fd for router session is a regular file: that can't be right. try -F nn\n" if -f $telnetfd;


	$session= Expect->exp_init($telnetfd);
	print $session "ping\r";
}

# this would be nice, if it worked...
# $session->expect_stty('raw -echo');

transact("Protocol [ip]:", "");
transact("Target IP address:", $targetip);
transact("Repeat count [5]:", $repeatcount);
transact("Datagram size [100]:", $size);
transact("Timeout in seconds [2]:", $timeout);
if (!$extended)
{
	transact("Extended commands [n]:", "n");
}
else
{
	transact("Extended commands [n]:", "y");
	transact("Source address or interface:", $interface);
	transact("Type of service [0]:", $typeofservice);
	transact("Set DF bit in IP header? [no]:", $dontfragment);
	transact("Validate reply data? [no]:", $validate);
	transact("Data pattern [0xABCD]:", $pattern);

	if (scalar @fancy)
	{
		foreach my $thing (@fancy)
		{
			if ($thing =~ /^[TRV]$/)
			{
				transact("Loose, Strict, Record, Timestamp, Verbose", $thing);
				transact("]:", "") unless $thing eq "V";
			}
			if ($thing =~ /^[LS]$/)
			{
				transact("Loose, Strict, Record, Timestamp, Verbose", $thing);
				transact("Source route:", $loosegateway || $strictgateway);
			}
		}
		transact("Loose, Strict, Record, Timestamp, Verbose", "");
	}
	else
	{
		transact("Loose, Strict, Record, Timestamp, Verbose[none]:", "");
	}
}

transact("Sweep range of sizes [n]:", "n");

if ($router ne "" )
{
	# no need to do anything, the spawned rsh will just print to stdout
	# if we exit
	# transact("Success rate", "");
} else {

        $session->expect($ExpectTimeout, [-re, '^\S+#$']);
	print $session->exp_before(), "\n";
	$telnetfd->close;
}



More information about the cisco-nsp mailing list