Unix Shell Script to ping systems & make a log


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Shell Script to ping systems & make a log
# 1  
Old 11-02-2011
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!

Code:
 
[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

Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by zaxxon; 11-02-2011 at 09:46 AM.. Reason: code tags, see PM
# 2  
Old 11-02-2011
Code:
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.
# 3  
Old 11-02-2011
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.
# 4  
Old 12-01-2011
Thanks a Lot !!!

Thanks 'Zaxxon, Spellbound, Mikelking'.. Smilie
Since, I'm new to Unix/Linux Shell-scripting and this forum
- kindly bear w/ me, wherever you find me a (dumb) rookie! Smilie
________________________________________________________
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.. <<
3. 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')..
4. 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 Smilie ]
________________________________________________________
My problem :
1. I'm not good at googling.. :'(
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~!!~ Smilie
# 5  
Old 12-01-2011
I don't know what a DNS thingy is Smilie
Maybe read the file into a while-loop like
Code:
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

Quote:
4. Question arises - what's the need for successful IPs..!?
What is a successful IP? If that is what I guess, you should get it from my former post where you test $?.

Quote:
1. I'm not good at googling.. :'(
Can't believe that Smilie

Quote:
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"..
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.
# 6  
Old 12-03-2011
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.

Code:
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

Moderator's Comments:
Mod Comment Thank you for helping (making this only a warning, no infraction), but you should use code tags too, thanks. See your PM for a guide how to use them.

Last edited by zaxxon; 12-04-2011 at 05:18 AM.. Reason: code tags, see PM
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to ping multiple servers

Hi I did the following script to ping multiple servers, but I keep on receiveing duplicate emails for one server that is down: #!/bin/bash date cat /var/tmp/servers.list | while read output do ping -c 1 "$output" > /dev/null if ; then echo "node $output is up" else ... (10 Replies)
Discussion started by: fretagi
10 Replies

2. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

3. Shell Programming and Scripting

Ping shell script - need urgent help

Hi friends, i have a file contains IP address like below cat file.txt 10.223.20.1 10.223.20.2 10.223.20.3 10.223.20.4 10.223.10.5 . . . like this Now i want to make a script which gives output whether each ip is pinging or not... the result will be like this 10.223.20.1 up... (9 Replies)
Discussion started by: siva kumar
9 Replies

4. Shell Programming and Scripting

Script problems in hp unix systems

test.sh -------------- #This script deletes the temporary files created on the server when the user opens the output files # FILE_PATH=$1 P_FILE_PATH=$2 FRQ=$3 #FRQ=`expr $FRQ*60*24 | bc` #FRQ= 60 echo $FILE_PATH echo $P_FILE_PATH echo $FRQ if then find $FILE_PATH -mmin... (7 Replies)
Discussion started by: arjunbodduuxlx
7 Replies

5. Shell Programming and Scripting

Shell Script for ping, Linux

I woul like to create a script in order to make a ping to a server and save in a variable a 1 if respond or a 0 if it doesnt. Then with that I could make a graffic of the server, for how long it is up.:b: So far I have this: if ; then #if the ip respond the ping shows online echo... (3 Replies)
Discussion started by: jsebastiang0
3 Replies

6. Shell Programming and Scripting

shell script for ping

hi anyone, i want shell script for ping command. any one post here............ (10 Replies)
Discussion started by: rameshreddy.ema
10 Replies

7. Shell Programming and Scripting

Shell script delete log files from folder & subfolders on space usage

Hi, I am trying to write a shell script to delete logs generate by db when space in the folder reaches 70%. i am getting space values from db, find the files at OS and remove them by using a cron job runs every 5minutes. I have to keep the latest 5 files at any time, my problem is that log files... (3 Replies)
Discussion started by: saha
3 Replies

8. Shell Programming and Scripting

ping from Unix script

I have two queries in regard to a Unix script. 1) From a Unix script, I want to read the IP of another remote HP-UX server and want to just check if that machine is reachable or the IP exists. I don't know the login Id and password of the remote machine. I could not properly manage with ping. How... (2 Replies)
Discussion started by: asutoshch
2 Replies
Login or Register to Ask a Question