Hi All,
First time poster and bit of a newbie to boot so sorry if this is the wrong section for this.
I am currently writing a bash script for Linux / RHEL systems to check the status of each NIC the server has. The idea is to test the following for each NIC:
- IP ping test
- Gateway ping test
- Reverse lookup test
- Forward lookup test
- Routing table check
I have written script to achieve this but I am looking to improve and possibly add more checks. So far this is my code:
#! /bin/bash
LOGFILE=/tmp/NIC-check.`date '+%Y-%m-%d-%H.%M'`.log
echo "Running Network Checks, pease wait..."
echo "" >> ${LOGFILE}
# > IP ping test
ip a | grep inet | grep -v 127.0.0.* | awk '{print $2}' | awk -F/ '{print $1}' >> /tmp/tempfile
while read line; do
echo "" >> ${LOGFILE}
echo "================================" >> ${LOGFILE}
echo "" >> ${LOGFILE}
echo ">>> PING TEST for $line" >> ${LOGFILE}
ping -c 1 "$line" > /dev/null
if [ $? -eq 0 ]; then
echo "NIC $line is UP"
else
echo "ALERT: NIC $line is NOT PINGING"
fi
echo "" >> ${LOGFILE}
# > Gateway ping test
echo ">>> GATEWAY PING for $line" >> ${LOGFILE}
GATEWAY=`echo $line | awk -F'.' -vOFS='.' '{$NF=1}1;'`
ping -b -c 1 "$GATEWAY" > /dev/null
if [ $? -eq 0 ]; then
echo "GATEWAY for $line is UP"
else
echo "ALERT: GATEWAY for $line is NOT PINGING"
fi
echo "" >> ${LOGFILE}
# > Reverse lookup test
echo ">>> REVERSE NSLOOKUP for $line" >> ${LOGFILE}
NSLOOKUP=`nslookup $line | grep "name =" | awk '{print $4}'`
nslookup $line > /dev/null
if [ $? -eq 0 ]; then
echo "IP $line is assigned to $NSLOOKUP"
else
echo "ALERT: REVERSE LOOKUP for $line is NOT WORKING"
fi
echo "" >> ${LOGFILE}
# > Forward lookup test
echo ">>> FORWARD NSLOOKUP for $line" >> ${LOGFILE}
NSLOOKUP=`nslookup $line | grep "name =" | awk '{print $4}'`
nslookup $NSLOOKUP | grep "Name:" > /dev/null
if [ $? -eq 0 ]; then
echo "FQDN $NSLOOKUP is assigned to $line"
else
echo "ALERT: FORWARD LOOKUP for $line is NOT WORKING"
fi
echo "" >> ${LOGFILE}
done < /tmp/tempfile >> ${LOGFILE}
echo "...Done!"
echo "Running Routing Table lookup Check..."
# > Routing table print out
echo "================================" >> ${LOGFILE}
echo "" >> ${LOGFILE}
echo ">>> ROUTING TABLE" >> ${LOGFILE}
netstat -nr | tee -a >> ${LOGFILE}
echo "" >> ${LOGFILE}
echo "================================" >> ${LOGFILE}
rm -rf /tmp/tempfile
echo "" >> ${LOGFILE}
echo "...Done!"
The above code works ok, see sample output below:
================================
>>> PING TEST for 192.168.1.104
NIC 192.168.1.104 is UP
>>> GATEWAY PING for 192.168.1.104
GATEWAY for 192.168.1.104 is UP
>>> REVERSE NSLOOKUP for 192.168.1.104
IP 192.168.1.104 is assigned to rhel-7-test.net
>>> FORWARD NSLOOKUP for 192.168.1.104
FQDN rhel-7-test.net is assigned to 192.168.1.104
================================
>>> ROUTING TABLE
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 ens33
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
================================
I am looking for help in the following areas:
Gateway ping test - at the moment the script takes the primary IP and uses AWK to swap out the last characters after the full stop and replace them with a '1' - so if my IP was 192.168.0.999, it will swap .999 for .1
This is ok on most machines but runs into trouble when the gateway IP is different to the NICs IP. What i am looking for is for a way to identify the gateway for each NIC individually and then run a ping test to see if it is up. The above was run on a machine with only 1 NIC but i need to be able to run this script on servers with multiple NICs.
Routing table - at the moment i am only checking the kernel routing table, is there a way i can check the routing for each NIC individually?
I am also hoping for help improving the code, so is there a better approach for what i am trying to achieve? Should i use function command blocks instead of what i have used?
And finally, the purpose of this is to check the Network Cards have been setup and installed correctly for each server, do you guys know of any more checks i can add in to test this?
Sorry for the long essay!
Any help or advice will be appreciated 
Thanks in advance!