#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	ciscocheckpoint
#	ciscoconfigure
#	ciscoget
#	ciscoget.exp
#	ciscologin.expect
#	cleanconfig
# This archive created: Fri May  3 10:56:52 1991
export PATH; PATH=/bin:$PATH
if test -f 'ciscocheckpoint'
then
	echo shar: will not over-write existing file "'ciscocheckpoint'"
else
cat << \SHAR_EOF > 'ciscocheckpoint'
#!/usr/local/bin/expect -f
#
# restart IP accounting statistics
#
# args: ciscoget router passwd enablepasswd
#

set router [index $argv 1]
set password [index $argv 2]
set enablepass [index $argv 3]

proc timedout {args} {
	send_user "Expect timed out" $args \n
	exit
}

source ciscologin.expect

if {[length $argv] < 4} {
	print "Invalid number of arguments\n"
	exit
}

send_user \n\n\n
spawn telnet $router
login $enablepass
send clear ip accounting\r
expect *#*
send QUIT\r
expect *Closed*
exit 0
SHAR_EOF
chmod +x 'ciscocheckpoint'
fi # end of overwriting check
if test -f 'ciscoconfigure'
then
	echo shar: will not over-write existing file "'ciscoconfigure'"
else
cat << \SHAR_EOF > 'ciscoconfigure'
#!/usr/local/bin/expect -f
#
# Fetch configuration information from the specified cisco router
#
# args: ciscoget router passwd enablepasswd filename
#
set localhost [exec hostname]
set tftpdir /private/tftpboot

set router [index $argv 1]
set password [index $argv 2]
set enablepass [index $argv 3]
set filename [index $argv 4]

proc timedout {args} {
	send_user "Expect timed out" $args \n
	exit
}

source ciscologin.expect

if {[length $argv] < 5} {
	print "Invalid number of arguments\n"
	exit
}

send_user \n\n\n
spawn telnet $router
login $enablepass
send configure network\r

expect {*configuration\ file*\?\ } {} \
	timeout		{timedout waiting for config file prompt}

send network\r
expect {*IP\ address\ of\ remote\ host*\?\ } {} \
	timeout		{timedout waiting for remote host prompt}

send $localhost

expect {*configuration\ file*?\ } {} \
	timeout		{timedout waiting for filename prompt}

send ${filename}\r

expect {*Configure\ using*\[confirm\]*} {} \
	timeout		{timedout waiting for confirm}

send "Y"

expect	{*Failed*#}	{send_user TFTP get failed\n; exit 1} \
	{*OK*#}		{send_user TFTP get succeeded\n}

send "write\r"
expect {*\[OK\]*} {} \
	timeout  {send_user Write failed\n; exit 1}
send QUIT\r
expect *Closed*
exit 0
SHAR_EOF
chmod +x 'ciscoconfigure'
fi # end of overwriting check
if test -f 'ciscoget'
then
	echo shar: will not over-write existing file "'ciscoget'"
else
cat << \SHAR_EOF > 'ciscoget'
#!/bin/sh
tftpdir=/private/tftpboot
export tftpdir
ciscoget.exp cschub-gw Password EnablePassword cschub-gw
ciscoget.exp dcs1hub-gw Password EnablePassword dcs1hub-gw
ciscoget.exp dcs2hub-gw Password EnablePassword dcs2hub-gw
ciscoget.exp sdnhub-gw  Password EnablePassword sdnhub-gw
ciscoget.exp madhub-gw Password EnablePassword madhub-gw
ciscoget.exp avwhub-gw Password EnablePassword avwhub-gw
ciscoget.exp rchhub-gw Password EnablePassword rchhub-gw


SHAR_EOF
chmod +x 'ciscoget'
fi # end of overwriting check
if test -f 'ciscoget.exp'
then
	echo shar: will not over-write existing file "'ciscoget.exp'"
else
cat << \SHAR_EOF > 'ciscoget.exp'
#!/usr/local/bin/expect -f
#
# Fetch configuration information from the specified cisco router
#
# args: ciscoget router passwd enablepasswd filename
#
set localhost [exec hostname]
set tftpdir /private/tftpboot
set archivedir /n/netmap/HUBS/config

set router [index $argv 1]
set password [index $argv 2]
set enablepass [index $argv 3]
set filename [index $argv 4]

proc timedout {args} {
	send_user "Expect timed out" $args \n
	exit
}

source ciscologin.expect

if {[length $argv] < 5} {
	print "Invalid number of arguments\n"
	exit
}

spawn telnet $router
login $enablepass
send write network\r

expect {*Remote\ host*\?\ } {} \
	timeout		{timedout waiting for Remote host prompt}

send ${localhost}

expect {*write*?*} {} \
	timeout		{timedout waiting for filename prompt}

if {[file ${tftpdir}/${filename} exists]} {
	if {[file ${tftpdir}/${filename}.old exists]} {
		if {[file ${tftpdir}/${filename}.old isfile] == 0} {
		      send_user "${tftpdir}/${filename}.old: Not a plain file\n"
		      exit 1
		}
		system rm -f  ${tftpdir}/${filename}.old
	}
	system mv -f ${tftpdir}/${filename} ${tftpdir}/${filename}.old
}
system touch ${tftpdir}/${filename}
system chmod 666 ${tftpdir}/${filename}


send ${filename}\r
expect {*Write\ file*\[confirm\]*} {} \
	timeout		{timed out waiting for confirm}

send "Y"
expect	{*Failed*#}	{set failed 1; send_user \nTFTP Write failed\n} \
	{*OK*#}		{set failed 0; send_user \nTFTP Write succeeded\n}

send QUIT\r
if {$failed == 0} {
	system cp ${tftpdir}/${filename} ${archivedir}/${filename}
	send_user  "Saved configuration.. "
}
system cleanconfig ${tftpdir}/${filename}
send_user "  cleaned configuration\n"
exit 
SHAR_EOF
chmod +x 'ciscoget.exp'
fi # end of overwriting check
if test -f 'ciscologin.expect'
then
	echo shar: will not over-write existing file "'ciscologin.expect'"
else
cat << \SHAR_EOF > 'ciscologin.expect'

proc login {args} {
	global password
	global enablepass

	expect	{*Password:*} {} \
		timeout {timedout waiting for password prompt}
	# ugly!
	system sleep 1
	send ${password}\r
	expect {*Password:*}	{send_user Bad password\n; exit} \
		{*>}		{} \
		timeout		{timedout waiting for login prompt}

	if {[length $args]} {
		send enable\r
		expect {*Password:*}	{} \
			timeout	{timedout waiting for enable password prompt; exit}
		# ugly!
		system sleep 1
		send [index $args 0]\r
		expect {*#}	{} \
		   {*Password:*} {send_user Wrong enable password\n; exit} \
		   timeout	{timedout waiting for enabled prompt; exit}
	}
}
	
SHAR_EOF
fi # end of overwriting check
if test -f 'cleanconfig'
then
	echo shar: will not over-write existing file "'cleanconfig'"
else
cat << \SHAR_EOF > 'cleanconfig'
#!/usr/bin/perl

$file = $ARGV[0];
$newfile = $file . ".new";
die "No file specified\n" unless $file;

open(CFG, "<$file") || die "Can't open $file\n";
open(NEWCFG, ">$newfile") || die "Can't open $newfile\n";

print NEWCFG <<"EOM";
!
! Note: this file ($file) has had the passwords removed for
!       security reasons.
!
EOM
while (<CFG>) {
	if (/^(enable-password|password)\s+/) {
		print NEWCFG "! $1 CENSORED\n";
		next;
	}
	print NEWCFG;
}
close(CFG);
close(NEWCFG);

rename($newfile, $file) || die "Can't rename $newfile to $file: $!\n";

SHAR_EOF
chmod +x 'cleanconfig'
fi # end of overwriting check
#	End of shell archive
exit 0
