LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-09-2015, 07:04 AM   #1
ablopera
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`
IP=XX.XX.XX.1

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

# Destination host machine name
SRC="syscom1"
BACKUP_DIR=$LINUX/temp/ToolsData/Count
LOGFILE1=$LINUX/temp/Tools/log/Count.log

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

cd /$BACKUP_DIR
(cd /home/linux/temp/Tools/ && ./server.sh)
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.

LINUX SERVER
count
--------
331315
(1 row)

=======================

TID 07
=======================

TID 08
=======================

TID 09
=======================

TID 10
count
--------
331315
(1 row)

=======================

TID 11
count
--------
331315
(1 row)

=======================
 
Old 07-09-2015, 07:38 AM   #2
zhjim
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

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

Rep: Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525Reputation: 1525
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:
Code:
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
ablopera
LQ Newbie
 
Registered: Jul 2015
Distribution: Centos 6.2
Posts: 14

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


Reply


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


All times are GMT -5. The time now is 01:15 PM.

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