#!/usr/local/bin/perl
#
# $Header: /home/netmgt/nocol/src/perlnocol/RCS/rcisco,v 1.3 1993/10/17 11:55:33 aggarwal Exp $
#
# 	rcisco - perl routine to execute a Cisco router command remotely
#
# Programmer: Christopher Sedore
# Revised by: John Wobus, jmwobus@mailbox.syr.edu
#
#    (c) Syracuse University Computing & Network Services 1993
#
# No warranty is expressed or implied.  Permission to copy and use is
# extended to all.  Permission to redistribute is granted under the
# following conditions: it is not sold for profit; this copyright
# notice remains intact; the same permissions extend to the recipient;
# and if any changes are made, a notice is added so stating.
#
# Command Format:
#
#  rcisco <router> <command> [<port>]
#
#    Signs on to Cisco router, executes command, and returns the
#    result.
#
#    <router>       - the name or IP number of the Cisco router.
#    <command>      - the Cisco router command, e.g. "show interfaces".
#    <port>         - the TCP port: 23 by default.
#
# Example Call:
#
#  rcisco mycisco.excellent.edu "show hardware"
#
#    This command signs on to myscisco.excellent.edu, executes the
#    command "show hardware" and sends the result to the standard
#    output.
#
# Depends upon:
#    hostname      - Unix command to list a host's name.
#
# To install this:
#   (1) Assign the perl variables below appropriately.
#   (2) Put this file whereever you want.
#

#

$defaultrouter="router.domain.edu";	# Default router.
$defaultpasswd="mypasswd";		# PW of the router.
$defaultcommand="show hardware";	# Command to exec on router
$defaultport=23;			# Default TCP port.

#
# main routine
#

($them,$passwd,$command,$port) = @ARGV;
$them = $defaultrouter unless $them;
$passwd = $defaultpasswd unless $passwd;
$command = $defaultcommand unless $command;
$port = $defaultport unless $port;	# usually should be port 23

$AF_INET = 2;
$SOCK_STREAM = 1;

$SIG{'INT'} = 'dokill';
sub dokill { kill 9,$child if $child; }

# require "$socketph";

$sockaddr = 'S n a4 x8';
chop($hostname = `hostname`);

($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyname($port, 'tcp')
     unless $port =~ /^\d+$/;
($name, $aliases, $type, $len, $thisaddr) =
                    gethostbyname($hostname);
if ($them =~ /^\d+/)		# IP address specified
{
    local(@a) = split (/\./,$them);
    $thataddr = pack ('C4', @a);
}
else {
    ($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
}

$thataddr || die "Unknown host: $!";

$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);

socket(S, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
bind(S, $this) || die "bind: $!";
connect(S, $that) || die "connect: $!";

select(S); $| = 1; select(STDOUT); # set socket to be command buffered


if ($child = fork) {
  print S "$passwd\n";
  print S "terminal length 0\n";	# dont need a 'more'
  sleep 1; #some routers work better with this.
  print S "$command\n";
  print S "quit\n"; 
  sleep 3;
  do dokill();
}
else {
  while (<S>) {print;}
}
