I am a novice at shell scripting. I have managed to cobble together a script that does exactly what I need it to do. However I am gathering information from 700+ devices. This script takes hours to complete the task. Is there a better way of doing it than what I have listed here? This script processes line by line from the file. Might there be a way to process multiple lines at the same time? Any help would be great.
I would also like to hear feed back about the script itself. How did I do for my first unix script, what would make it better.
I have posted the working script below. (community strings changed of course)
NOTE: Has to be shell script, Perl is on the box but I don't have access to it.
#
#======================================================================#
#This section tests for the file device.log, and removes if it exists #
#======================================================================#
if [ device.log ]
then
rm device.log
fi
cat adoa.txt | while read device
do
#======================================================================#
#This section will pull the ip address from DNS and assign the value #
#To the variable "ip". It will then test to see if the variable "ip" #
#Has a Null (value of no length) or if it contains data. If a value is #
#Found it is stored in the variable "dns" If no data is found in the #
#variable "ip" it will assign the string value of "Not in DNS" to the #
#variable "dns" #
#======================================================================#
#
ip=`(host $device | awk ' /has/ {print $4}')`
if [ "$ip" ]
then
dns="$ip"
#
#
#
#
#======================================================================#
#This section will Ping the variable "device" and record a yes or no #
#Answer in the variable "routable". Error messages such as unknown #
#Host are sent to /dev/null to prevent them from displaying #
#======================================================================#
#
answer=`(ping -c 1 -q -w 2 -n $device 2>/dev/null | grep received | awk '{print $4}')`
if
[ $answer = 0 ];
then answer="NO"
else answer="YES"
fi
#
#======================================================================#
#This section will test the read and read/write snmp community strings #
#Yes or No answer is stored in the variable "snmpread" and "snmpwrite" #
#This section also collects the device system name and stores it in #
#the variable "routername"
#======================================================================#
#
if
[ $answer = "YES" ];
then
routername=`(snmpget -t 2 -r 0 -Oqv -Os -v 2c -c (Read only community string) $device 2>/dev/null SNMPv2-MIB::sysName.0)`
if [ "$routername" = "" ];
then snmpread="NO"
routername="N/A"
else snmpread="YES"
fi
snmpwrite=`(snmpget -t 2 -r 0 -Oqv -Os -v 2c -c (Read Write community string) $device 2>/dev/null SNMPv2-MIB::sysName.0)`
if [ "$snmpwrite" = "" ];
then snmpwrite="NO"
else snmpwrite="YES"
fi
if [ "$snmpwrite" = "YES" ];
then
devicenumber=`(snmpwalk -v 2c -OQs -c (Read Write community string) $device 1.3.6.1.2.1.4.20.1.2 | grep $ip | awk ' /=/ {print $3}')`
interface=`(snmpwalk -v 2c -OQs -c (Read Write community string) $device 1.3.6.1.2.1.2.2.1.2 | grep -w ifDescr.$devicenumber | awk '/=/ {print $3}')`
else
interface="N/A"
fi
#======================================================================#
#this section takes the values of the variables and outputs them to #
#the file device.log and echos the values on the screen as well #
#this section is for the ouput of completed script #
#======================================================================#
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface" >> device.log
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface"
#
#======================================================================#
#when device is not reachable by ping this section assigns values to #
#variables not tested and exits script #
#======================================================================#
#
else
snmpread="NO"
snmpwrite="NO"
interface="N/A"
routername="N/A"
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface" >> device.log
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface"
fi
#
#=====================================================================#
# If device is not is DNS this section assigns values to variables #
# Not tested and exits script #
#=====================================================================#
#
else
dns="Not in DNS"
answer="NO"
snmpread="NO"
snmpwrite="NO"
interface="N/A"
routername="N/A"
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface" >> device.log
echo "$device,$routername,$dns,$answer,$snmpread,$snmpwrite,$interface"
fi
done