Friday, July 20, 2018

Create script to get gateway address, DNS, NTP, & Service Status in Linux

Copy the below content and paste it in a file, it will give you the output at /opt/ location, if you wants to change the location you can change.

You have to change the NTP server IP Address in file rest of the things will work on all environment.

#!/bin/bash
rm -rf /opt/ping_result
echo -e "\nTIMESTAMP: `date`" >> /opt/ping_result
echo -e "HOSTNAME: `hostname`"  >> /opt/ping_result
#hostname >> /opt/ping_result
GW=`/sbin/ip route | awk '/default/ { print $3 }'`
checkdns=`cat /etc/resolv.conf | awk '/nameserver/ {print $2}' | awk 'NR == 1 {print; exit}'`
checkntp1=10.200.247.21
checkntp2=10.200.247.22
output=/opt/ping_result
GN='\033[0;32m'
RD='\033[0;31m'
NC='\033[0m'
function pingdns 
{
  tput setaf 6; echo -e "Pinging DNS server in resolv.conf ($checkdns) to check name resolution" >> $output; tput sgr0;
  ping $checkdns -c 4
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${GN}Primary DNS is Pingable.${NC} Proceeding with NTP Server 1 Check. \n" >> $output; tput sgr0;
    else
      echo -e "Could not establish internet connection to DNS. ${RD}Something may be wrong here.${NC} \n" >> $output >&2
  fi
}
function pingntp1
{
  tput setaf 6; echo -e "Pinging ($checkntp1) to check NTP status"  >> $output; tput sgr0;
  ping $checkntp1 -c 4
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${GN}NTP Server 1 is Pingable.${NC} Proceeding with NTP Server 2 check. \n" >> $output; tput sgr0;
    else
      echo -e "Could not establish connection to NTP Server 1. ${RD}Something may be wrong here.${NC} \n" >> $output >&2
  fi
}
function pingntp2
{
  tput setaf 6; echo -e "Pinging ($checkntp2) to check NTP status" >> $output; tput sgr0;
  ping $checkntp2 -c 4
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${GN}NTP Server 2 is Pingable.${NC} \n" >> $output; tput sgr0;
    else
      echo -e "Could not establish connection to NTP Server 2. ${RD}Something may be wrong here.${NC} \n" >> $output >&2
  fi
}
function services1
{
  tput setaf 6; echo -e "${GN}Running Services${NC}" >> $output; tput sgr0;
  service --status-all | grep running >> $output;
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${GN}Above Given Services are Running.${NC} \n" >> $output; tput sgr0;
    else
      echo -e "${GN}Above Given Services are Running.${NC} \n" >> $output >&2
  fi
}
function services2
{
  tput setaf 6; echo -e "${RD}Stopped Services${NC}" >> $output; tput sgr0;
  service --status-all | grep stopped >> $output;
    if [ $? -eq 0 ]
    then
      tput setaf 6; echo -e "${RD}Above Given Services are Stopped.${NC} \n" >> $output; tput sgr0;
    else
      echo -e "${RD}Above Given Services are Stopped.${NC} \n" >> $output >&2
  fi
}
tput setaf 6; echo -e "\nPinging Defaut gateway ($GW) to check for LAN connectivity">> $output; tput sgr0;
if [ "$GW" = "" ]; then
    tput setaf 1;echo -e "${RD}There is no gateway. Probably disconnected...${NC} \n" >> $output; tput sgr0;
fi
ping $GW -c 4
if [ $? -eq 0 ]
then
  tput setaf 6; echo -e "${GN}Default Gateway Pingable.${NC} Proceeding with internet connectivity check.\n " >> $output; tput sgr0;
  pingdns
  pingntp1
  pingntp2
  services1
  services2
  exit 0
else
  echo -e "${RD}Something is wrong with LAN (Gateway unreachable)${NC} >> /opt/ping_result "
  pingdns
  pingntp1
  pingntp2
  services1
  services2
fi


The output of the file will as mentioned below.