Unix Shell Script to ping systems & make a log

Hi,
I need to ping all the systems in my network and then create a log for the ones, from where I successfully get the ping-response (ICMP packet).

Now, I've used the ping command successfully, but am unable to use 'grep' command to locate the IPs for which the ping was successful (so that I can log other info about 'em).

Kindly help, please!
Thanks!

 
[user_psn@Centos ~]$ cat ping.sh
#ip1=133
#ip2=173
#ip3=174
#ip4=216
counter=1
echo "_____________________________________________________________________________"
for ip in $(seq 133 216)
do
  echo "START PING #"$counter >> psn.txt
  echo "_____________________________________________________________________________" >> psn.txt
  cat | ping -c 1 192.168.17.$ip >> psn.txt
  echo "_____________________________________________________________________________" >> psn.txt
  echo "END PING #"$counter >> psn.txt
  counter=`expr $counter + 1`
  echo "_____________________________________________________________________________" >> psn.txt
done
cat | ping -c 1 192.168.17.$ip >> psn.txt

What is the cat with the pipe good for?

You should maybe better check the return code of the pings with comparing $? != 0 instead of grepping for success or not. RC 0 would be a success.

Instead of the counter being increased I would rather use a time stamp so you know when it happened. Also maybe it is more interessting to know when it failed, not if it is successful. Either way it is always succesful until it fails, if you want to use it in further processing.
Also it might be interessting to use a timeout on ping which would be -W .

For logging I recommend using logger which will pipe you log message directly to syslog. The main advantage is that you can offload normal log maintenance. Honestly there's no sense reinventing the wheel.

Thanks 'Zaxxon, Spellbound, Mikelking'.. :slight_smile:
Since, I'm new to Unix/Linux Shell-scripting and this forum - kindly bear w/ me, wherever you find me a (dumb) rookie! :slight_smile:
________________________________________________________
My overall aim is :

  1. To ping my office-network..
    2a. Create a log for successful pings and unsuccessful ones..
    >> till here - i'm done<<
    2b. Create a DNS kinda thingy..
    >> done - but, i'm using 'if-else' here (..plz don't laugh..) <<
    >> my boss said, rather than 'if-else', use a file - where <<
    >> from you import all the names for particular IPs.. <<
  2. Generate/create a Network-Outage report, that gives the timestamps for
    whenever the ping was unsuccessful for that particular IP (as rightly mentioned by 'Zaxxon')..
  3. Question arises - what's the need for successful IPs..!?
    Answer - I need to run network-related commands on those successful IPs,
    to generate another log that gives me all the system-related-info (such as Memory/Disk-usage, N/W-usage, blah! blah! blah!) of/for those particular (successful) IPs..

[in short - have to make my own NMS (n/w mgmt. sys.) for my company :slight_smile: ]
________________________________________________________
My problem :

  1. I'm not good at googling.. :cry:
  2. I've a few commands, like :
    vmstat, netstat, nslookup, traceroute, ifconfig, df, du, iostat, iptables, chkconfig, etc.
    but these work for my system (not all f 'em) and i need to sit on my system
    and use the above commands (or, similar) on all the systems on the n/w via. IPs..
  3. As of now, the bit I'm struggling w/ is:
    using 'grep' to fetch the IP from the same line where I'm grepping - "unreachable host"..
    ________________________________________________________
    ~!!~Cheers~!!~ :slight_smile:

I don't know what a DNS thingy is :wink:
Maybe read the file into a while-loop like

while read IP; do
   ping $IP #with some options
   if [ $? != 0 ]; then
      echo "$(date) --- IP $IP not responding." >> $YOURLOGFILE # you have to declare that somewhere up there
   else
      echo $IP >> the.list.that.will.checked.further.on
   fi
done < file.with.a.list.of.ip.addresses.to.ping

What is a successful IP? If that is what I guess, you should get it from my former post where you test $? .

Can't believe that :slight_smile:

You don't need to grep it, as testing the return code of ping you know which IP you are currently processing and can just redirect it to your further to be processed list; see up there after the else .

Hi,

Another solution is below.
Put all of your ip address to multiple.txt than run script gokku

Script Code is below. Script is going to sent only one ping packet to remote node. If reply successfull print the UP else Print Down.

bash-3.00$ more multiple.txt 
10.202.40.53
10.202.40.66
10.202.40.67
10.202.40.55
10.202.40.56
10.202.40.58
10.202.40.59
10.202.40.61
10.202.40.62
10.202.40.38
10.202.40.37
10.202.40.3
10.202.40.1
10.202.40.2
10.202.40.7
10.202.40.5
10.202.40.6
10.202.40.11
10.202.40.9
10.202.40.10
10.202.40.15
10.202.40.13
10.202.40.14
10.202.40.41
10.202.40.40
10.202.40.19
10.202.40.17
10.202.40.18
10.202.40.23
10.202.40.21
10.202.40.22
10.202.40.27
10.202.40.25
10.202.40.26
10.202.40.31
10.202.40.29
10.202.40.30
10.202.40.35
10.202.40.33
10.202.40.34
10.202.40.44
10.202.40.43
10.202.40.64
bash-3.00$ more gokku 

#!/bin/ksh 
count1=0
count2=0
start=`date +"%d-%m-%Y-%T"`
echo
cat multiple.txt | while read line 
do
   sonuc=`/usr/sbin/ping -s $line 64 1 | grep packet | awk '{print $(NF-2)}'`
   if [[ "$sonuc" == "0%" ]]
   then
      count1=$((count1 + 1))
      echo  $line"    "Up"    " 
   else 
      count2=$((count2 + 1))
      echo $line"     "Down"  "
   fi
done
end=`date +"%d-%m-%Y-%T"`
echo
echo Start:$start 
echo End  :$end
echo $count2 side Down $count1 side Up
echo

bash-3.00$ gokku
10.202.40.53    Up      
10.202.40.66    Up      
10.202.40.67    Up      
10.202.40.55    Up      
10.202.40.56    Up      
10.202.40.58    Up      
10.202.40.59    Up      
10.202.40.61    Up      
10.202.40.62    Up      
10.202.40.38    Up      
10.202.40.37    Up      
10.202.40.3     Up      
10.202.40.1     Up      
10.202.40.2     Up      
10.202.40.7     Up      
10.202.40.5     Up      
10.202.40.6     Up      
10.202.40.11    Up      
10.202.40.9     Up      
10.202.40.10    Up      
10.202.40.15    Up      
10.202.40.13    Up      
10.202.40.14    Up      
10.202.40.41    Up      
10.202.40.40    Up      
10.202.40.19    Up      
10.202.40.17    Up      
10.202.40.18    Up      
10.202.40.23    Up      
10.202.40.21    Up      
10.202.40.22    Up      
10.202.40.27    Up      
10.202.40.25    Up      
10.202.40.26    Up      
10.202.40.31    Up      
10.202.40.29    Up      
10.202.40.30    Up      
10.202.40.35    Down    
10.202.40.33    Down    
10.202.40.34    Down    
10.202.40.44    Down    
10.202.40.43    Down    
10.202.40.64    Up      
Start:04-12-2011-05:26:18
End :04-12-2011-05:27:09
5 side Down 38 side Up

Regards,

Goksel Yangin
Computer Engineer