Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-09-2015, 07:04 AM   #1
LQ Newbie
Registered: Jul 2015
Distribution: Centos 6.2
Posts: 14

Rep: Reputation: Disabled
Unhappy How to write ping result in a log file/text file

Hi Everyone, Im doing a script which will query a database and log the result in a log file/text file. Part of the script is to check if the remote computer is online and to do that I inserted a ping command. What I need to do is log the ping result if the remote computer has no connection and log the result of the database query if remote computer is offline. So far heres what I have written.

CLI=`printf %02d $1`

if [ $# -lt 1 ]
echo "usage:" `basename $0`" Comp_nmbr"
exit 1

# Destination host machine name

VAR=`ping -s 1 -c 1 $SRC > /dev/null; echo $?` >> Data.log

(cd /home/linux/temp/Tools/ && ./
echo TID $CLI >> Data.log
rsh $IP$CLI -l pass "psql -U linux -c "select count(*) from table"" >> Data.log

exit 0

After running the script, ping for those remote computer tht were not online were not included in the log.

Heres the sample output of the log. Those TID with any values are those that are offline.

(1 row)


TID 07

TID 08

TID 09

TID 10
(1 row)


TID 11
(1 row)

Old 07-09-2015, 07:38 AM   #2
Senior Member
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
you dont alter the value of the SRC variable. You would need a list of computers you want to ping then loop through them

SRC="td1 td2 td3 td4"
for i in $SRC; do
ping $SRC; echo $?
something like that.
1 members found this post helpful.
Old 07-09-2015, 07:57 AM   #3
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,222
Blog Entries: 13

Rep: Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276Reputation: 4276
Keep the "-c 1" term in your ping.

Using the result code as zhjim suggests is the right thing to do. Ping will return a zero result code when it works, and non-zero result codes if the address is bad or not active.

Further you may want to consider using the "-w <seconds>" term as well to give it a different timeout besides the default. However the default is usually small like 3-5 seconds.

If you want to echo that into a log file, then you can do something like:
ping -c 1 -w 2 $SRC;
echo "Result for address $SRC was $?" >> Data.log;
And you could also convert the result code numbers to strings matching:
0 - Host detected
1 - Destination host unreachable
2 - Unknown host

Last edited by rtmistler; 07-09-2015 at 08:01 AM.
1 members found this post helpful.
Old 07-13-2015, 07:56 AM   #4
LQ Newbie
Registered: Jul 2015
Distribution: Centos 6.2
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks zhjim and rtmistler for the help.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
i need to write a c code to ping IP addresses which are read from a text file vinothtitan Linux - Newbie 5 10-13-2014 12:23 AM
Read and count each character in text file and then write the result in other text fi Fadly Massere Programming 5 11-17-2013 06:11 PM
Clean Up Log - Search for Pattern in Log file and Output result bridrod Linux - Newbie 10 01-05-2010 10:49 AM
Want to write a script for calculating usage of software from text file contains log sheraz Linux - Newbie 7 10-06-2009 04:05 AM
Write Error Log to Text File jamez15 Linux - General 3 07-02-2006 08:32 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:06 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration