Scripting your Network

Always thought that controlling your Company's Network was difficult. Pulling up Reports was possible only using some expensive Network Monitoring tools - WRONG.

I will show you one sample script which you can modify according to your needs to control your Network. These scripts can basically be used in any Network environment where the Network Device is Manageable (i.e. TELNETing possible). Following is a script which I had written at one of my job postings to pull up report of Routers which are connected through ISDN with Leased Line down:
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#!/bin/ksh

####################
# #
# ISDN Status Script #
# #
####################

trap 'terminate' 1 2 3 6
terminate()
{
if test -f $SCRIPT_PATH/log/isdn_active.log.$$
then
rm -r $SCRIPT_PATH/log/isdn_active.log.$$
fi
if test -f $SCRIPT_PATH/log/tnet.log.$$
then
rm -r $SCRIPT_PATH/log/tnet.log.$$
fi
echo "\nScript execution terminated by user. Cleaning up Temporary Files.....Done\n"
exit 1
}
clear
echo "\n##### Current ISDN Status Script #####\n"
echo " Developed By Baudhayan Lahiri \n"
echo "\nPlease enter Router Username:"
read uname
stty -echo
echo "\nPlease enter Router Password:"
read pword
echo "\nPlease enter Router Enable Password:"
read epword
ssty echo
echo "\nGathering Router ISDN Status Information, Please Wait.....\n"
echo "To abort ISDN STATUS script execution press \n"
while read LINE
do
(
sleep 1
echo $uname;\
sleep 1
echo $pword";\
sleep 1
echo "en";\
sleep 1
echo $epword;\
sleep 1
echo "sh isdn active";\
sleep 1
echo "q";\
) tnet $LINE 3
done < $SCRIPT_PATH/db/isdn_rtrs >> $SCRIPT_PATH/log/isdn_active.log.$$ 2> $SCRIPT_PATH/log/rtr_output.log
echo "--------------------------------------------------------------------------------
ISDN ACTIVE CALLS
--------------------------------------------------------------------------------
Call Calling Called Remote Seconds Seconds Seconds Charges
Type Number Number Name Used Left Idle Units/Currency
--------------------------------------------------------------------------------"
grep -w "In" $SCRIPT_PATH/log/isdn_active.log.$$
grep -w "Out" $SCRIPT_PATH/log/isdn_active.log.$$
echo "--------------------------------------------------------------------------------\n"
rm -r $SCRIPT_PATH/log/isdn_active.log.$$


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Explaination:

  1. $SCRIPT_PATH : It is an Environment Variable defined in Linux which points to the Path where all your Scripts are stored, eg. /etc/var/home/scripts
  2. isdn_active.log.$$ : "$$" represents the current Shell's Process ID, "isdn_active.log" is the Output file which will store all the Router Names which are currently connected through ISDN.
  3. tnet.log: It is a temporary log file created for the TNET script which TELNET's only those Routers whose links are up.
  4. The entire WHILE...DO loop picks up one Router Name at a time mentioned in the "isdn_rtrs" file & redirects Output of "sh isdn active" command to "isdn_active.log.$$" file. All errors are redirected towards "rtr_output.log" file.
  5. The GREP filter along with "In" & "Out" search filters are used to filter the Router entries from the log file which are currently connected through ISDN.

No comments: