Register
It is currently Wed Oct 22, 2014 9:03 pm

[help] Ping bash script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Aug 05, 2010 7:29 pm   

Joined: Thu Aug 05, 2010 7:22 pm
Posts: 1
Sorry if it is the wrong section, I am very new to bash scripting so to me it is basics...
I am trying to write a simple program to ping random ip addresses and return successful exit codes to a log file.
I have the base of what I want running I just dont know how to output only successes through a file.
Heres what I have:
#!/bin/bash
for j in {1..255}
do
for i in {1..255}
do
ping -c 1 -W 2 72.1.$j.$i | tee -a outputfile
done
done

This is currently outputting everything to my outputfile but I want to be able to use an if [ $? -eq 0 ]; then command to output it.
Sorry if I ramble and thanks for any help.


Top
 Profile  
 PostPosted: Fri Aug 06, 2010 2:13 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
If you are interested just in successfully pinged addresses, you can use something along the following lines:
Code:
if ping -c 1 -W 2 72.1.$j.$i ; then
  echo 72.1.$j.$i > outputfile
fi


Top
 Profile  
 PostPosted: Fri Aug 06, 2010 8:24 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
tstek wrote:
#!/bin/bash
for j in {1..255}
do
for i in {1..255}
do
ping -c 1 -W 2 72.1.$j.$i >/dev/null; [ $? -eq 0 ] && echo "72.1.$j.$i UP" || : ;
done
done



My edits in red.

HTH's


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 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