#!/usr/local/bin/perl -- -*-Perl-*-
#
# $Id$
# $Source$
# Paul Traina (Feburary 1994)
#
# configure multiple cisco boxes at the same time
#

require 'chat2.pl';
require 'getopts.pl';

@router_firewalls = ( "fire1", "fire2" );
$router_acls      = "walls/rtr-acl";

@ts_firewalls     = ( "ts1", "ts2" );
$ts_acls	  = "walls/ts-vty";

@cio_firewalls	  = ( "cio-firewall" );
$cio_acls	  = "walls/cio-acl";

$user             = $ENV{'USER'};
$tftp_server      = "tftp-server.cisco.com";

#-----------------------------------------------------------------------

&Getopts("dtc:Cs:u:");

if ($opt_t) {
    @servers = @ts_firewalls;
    $config  = $ts_acls;
} else {
    if ($opt_C) {
	@servers = @cio_firewalls;
	$config  = $cio_acls;
    } else {
	@servers = @router_firewalls;
	$config  = $router_acls;
    }
}

$debug       = $opt_d if $opt_d;
$user	     = $opt_u if $opt_u;
$config      = $opt_c if $opt_c;
$tftp_server = $opt_s if $opt_s;

print "Configuring:";
for $rtr (@servers) {
    print " $rtr";
}
print "\n";

$user 		 = &askdefault("Username", $user);
$password	 = &askdefault("Password", "");
$enable_password = &askdefault("Enable password", "");
$config		 = &askdefault("Configuration file", $config);
$tftp_server	 = &askdefault("TFTP server", $tftp_server);

#-----------------------------------------------------------------------

$sec = 1000;

foreach $server (@servers) {
    print "Connecting to $server...\n";

    &chat'open_port($server, 23);

    $ret = &expect(20 * $sec,
			"Username: ", 1);

    &send($user);

    $ret = &expect(20 * $sec,
			"Password: ", 1);

    &send($password);

    $ret = &expect(20 * $sec,
			"$server.*>", 1,
			"Access denied", 2,
			"Password:", 2,
			"enable password", 3);

    if ($ret == 3) {
	&send($enable_password);
	&expect(60 * $sec,
			"$server.*>", 1,
			"Password:", 2,
			"Access denied", 2);
    }

    if ($ret != 1) { goto quit; }

    &send("enable");
    $ret = &expect(5 * $sec,
			"Password: ", 1);
    if ($ret != 1) { goto quit; }

    &send($enable_password);
    $ret = &expect(5 * $sec,
			"$server.*#", 1,
			"Password: ", 2,
			"Bad passwords", 3);
    if ($ret != 1) { goto quit; }


    &send("configure network");
    $ret = &expect(5 * $sec,
			"\[host\]\? ", 1);
    if ($ret != 1) { goto quit; }

    &send("host");
    $ret = &expect(5 * $sec,
			"of remote host .*\\? ", 1);
    if ($ret != 1) { goto quit; }

    &send($tftp_server);
    $ret = &expect(5 * $sec,
			"Name of configuration file .*\\? ", 1);
    if ($ret != 1) { goto quit; }

    &send($config);
    $ret = &expect(5 * $sec,
			"\[confirm\]", 1);
    if ($ret != 1) { goto quit; }

    &send("");
    $ret = &expect(30 * $sec,
			"OK", 1,
			"timed out", 2);
    if ($ret != 1) { goto quit; }

    print STDERR "\n";

    $ret = &expect(5 * $sec,
 			"$server.*#", 1);
    if ($ret != 1) { goto quit; };

    &send("write memory");
    $ret = &expect(20 * $sec,
			"$server.*#", 1,
			"error", 2);

    $record{$server} = "successful";
    goto quit_ok;

  quit:
    $record{$server} = "failed";
  quit_ok:
    &send("quit");
    &chat'close();
}

foreach $s (keys(%record)) {
    print "Configured of $s $record{$s}\n";
}

sub send {
    local ($arg) = @_[0];
    print STDERR " $arg\n";
    &chat'print($arg . "\r");
}

sub expect {
    local(@expect_args);
    local($time_out);
    local($ret);

    @expect_args = ();
    $time_out = shift(@_);

    while(@_) {
	local($match) = shift(@_);
	local($result) = shift(@_);
	push(@expect_args, $match);
	push(@expect_args,
	     "print STDERR \"$server: $match\"; $result");
    }

    push(@expect_args, 'TIMEOUT');
    push(@expect_args, "print STDERR \"$server: TIMEOUT\n\"; exit");
    push(@expect_args, 'EOF');
    push(@expect_args, "print STDERR \"$server: EOF\n\"; exit");

    $ret = &chat'expect($time_out, @expect_args);
    return $ret;
}

sub askdefault {
    local ($prompt, $default) = @_;
    local ($answer);

    if ($default ne "") {
	print "$prompt [$default]? ";
	$answer = <STDIN>;
	chop $answer;
	$answer = $default if ($answer eq "");
    } else {
	$answer = "";
	while ($answer eq "") {
	    print "$prompt? ";
	    $answer = <STDIN>;
	    chop $answer;
	    print "-- response required\n" if ($answer eq "");
        }
    }
    return $answer;
}

#-------------------------------------------------------------------
# $Log$
#-------------------------------------------------------------------
