Register
It is currently Fri Oct 24, 2014 9:31 pm

Simple Ping Script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sun Feb 22, 2009 8:07 am   

Joined: Sun Feb 22, 2009 8:03 am
Posts: 1
Hi everyone,

Basically i'm looking for a simple script to take a list of IP's from a text fie, ping each of them then give me the output to a new text file with a list of those which have replied to the ping.

I'm very new to scripting so please excuse me if this is a simple script.

Thanks

Paul


Top
 Profile  
 PostPosted: Sun Feb 22, 2009 8:32 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Hi,

All script have their difficulties :)

But I believe this could do it for you.
Code:
#/bin/bash
# Input file is first argument for the script, ex. ./ping.sh file_containing_ip_or_hosts.txt
input_file=$1
# Output file is second argument for the script, ex. ./ping.sh first_file.txt output_file.txt
output_file=$2
# Just gets the date in 2009-02-20 15:20:00 format
date=$(date +"%Y-%m-%d %H:%M:%S")
echo "Starting pings ($date)" > $output_file

# Loops throu the file supplied above
for i in $(cat $input_file); do
   # Ping just once since you want to see what they replied and redirect all the output to /dev/null
   ping -c1 $i &> /dev/null
   # Use the internal error handling from ping to determine the outcome, each line after this is just handling return codes.
   case $? in
      0) echo "Successfully pinged $i" >> $output_file ;;
      1) echo "Failed to ping $i, timed out" >> $output_file ;;
      2) echo "Failed to ping $i, Host not found" >> $output_file ;;
      *) echo "Failed to ping $i, General failure ($? returncode)" >> $output_file ;;
   esac
done


This at the moment will give you information about everything. If you wish just to have the ones that replied you can delete the lines below 0) in the case $?.
$? returns returncode for the success or unsuccess of a program. 0 being successful and every other higher number will be a failure.
If you wish to keep the errors but in another file, you can change the output destination by changing $output_file on the line you wish to send to another file.
Do not set the variable to something else thou, if you do it will fail use it for all other instances that uses that variable.

Code:
   case $? in
      0) echo "Successfully pinged $i" >> $output_file ;;
      1) echo "Failed to ping $i, timed out" >> /path/to/error_file.txt ;;
      2) echo "Failed to ping $i, Host not found" >> /path/to/error_file.txt ;;
      *) echo "Failed to ping $i, General failure ($? returncode)" >> /path/to/error_file.txt ;;
   esac

This is a more proper way to change it. But you could also do a "error_log=/path/to/file.txt" and set it to $error_log instead.

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP