LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-15-2017, 03:44 AM   #1
riahc3
Member
 
Registered: Dec 2002
Posts: 221

Rep: Reputation: 1
Check if ping is successful


Im trying to do a script that if a host is up, it needs to run code

Would:

Code:
check=ping -c 1 192.168.100.15 ;


if [ "$check" = 0 ]
then
  echo "Host is up and rest of code here"
else
  echo "Host is down and nothing here"
fi
Work?
 
Old 05-15-2017, 04:34 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,224

Rep: Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834Reputation: 6834
so what do you think? Does it work as expected? Did you try it already?

http://www.tldp.org/LDP/abs/html/exit-status.html
http://bencane.com/2014/09/02/unders...-bash-scripts/
http://stackoverflow.com/questions/5...ds-efficiently
 
1 members found this post helpful.
Old 05-15-2017, 05:26 AM   #3
riahc3
Member
 
Registered: Dec 2002
Posts: 221

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
Thats why Im asking before adding it to the code.
 
Old 05-15-2017, 06:30 AM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 3,994

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Your assignment to check is missing quotes, and you are not executing anything in the if.
 
Old 05-15-2017, 06:59 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,789
Blog Entries: 13

Rep: Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831
Quote:
Originally Posted by riahc3 View Post
Thats why Im asking before adding it to the code.
pan64's point is that you should run that fragment. You do not need to add it to any code, you can run that script fragment as is, by adding a shebang at the top and putting that portion into a temporary script file, debug it and find out if it runs. Per smallpond's comment, you need to properly quote the ping command when you assign the result to a variable, give this a read about some ways to assign the output of a bash command to a variable.

A further suggestion is that you check the manual page for ping(8) where it talks about what ping returns on error. I do believe you are using it correctly, however I always do prefer to double check.

A final suggestion is to put "set -xv" at the top of that script, along with the shebang at the top of the script. Yes I realize you were saying that this is a code fragment, however we recommend that you test just this fragment to validate that you have this section correct.

Last edited by rtmistler; 05-15-2017 at 08:03 AM. Reason: edit: added reference for command result to variable
 
Old 05-15-2017, 07:50 AM   #6
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
you're not passing to $check what you think you are passing to $check, thus why you need to read the links pan64 linked. So to answer the question, no it would not work.
 
Old 05-15-2017, 08:34 AM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by riahc3 View Post
Thats why Im asking before adding it to the code.
Try this:
Code:
HOST=192.168.100.15

ping -c1 $HOST 1>/dev/null 2>/dev/null
SUCCESS=$?

if [ $SUCCESS -eq 0 ]
then
  echo "$HOST has replied"
else
  echo "$HOST didn't reply"
fi
#EOF
Enjoy the Goodness.
Be certain to look for Conditional Expressions in the topic links pan64 gave us.
 
1 members found this post helpful.
Old 05-15-2017, 03:02 PM   #8
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,695

Rep: Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582
Notes.

Might consider a different connection method. Ping may be blocked while tcp connections still work. More and more ping is becoming a poor network tool.
 
Old 05-15-2017, 03:32 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,452

Rep: Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061Reputation: 1061
ping and traceroute are good debugging tools; only paranoid or masochistic persons block ICMP.
Even shorter is
Code:
if ping -c1 $HOST 1>/dev/null 2>/dev/null
then
The code after then is run if the code between if and then has exit status zero(success).
 
1 members found this post helpful.
Old 05-15-2017, 09:45 PM   #10
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,695

Rep: Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582
Switches block ping when under loads by design.
 
Old 05-16-2017, 11:22 AM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
ping is unreliable as a test.
 
Old 05-16-2017, 12:10 PM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,789
Blog Entries: 13

Rep: Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831
Well perhaps the suggestion therefore would be to try multiple contact techniques and have a few if-else tests, once one of them succeeds, then the host is deemed available.

I'm also assuming that their script once it deems a host to be available, tries to contact it using some other protocol. Therefore a test for verification that the next command succeeds would be in order. I tend to code defensively and check return status for all that I can verify.

Not sure this needs to iterate as a discussion over whether or not to use ping. The coder may know their network or their situation and feel that ping is perfectly acceptable, and may always have been. They may be pinging an internal network address, they may be pinging something which is on a purely private network. As an example, chassis based systems for Telco which I've worked on years ago, had internal networks which were 10.x.x.x and they were internal to the chassis. Finding another card meant engaging it using some form of network protocol, one of which could be ping.
 
Old 05-16-2017, 12:18 PM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,243
Blog Entries: 4

Rep: Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776Reputation: 3776
Be careful that you are not re-inventing Nagios, the hard way . . .

(Nagios is a corporation with a number of products, but "Nagios Core" is free.)
 
  


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
[SOLVED] need to get "code" field from ping return to see if ping has been successful. franmf Programming 5 10-24-2012 07:46 PM
[SOLVED] Programming C. Need to know if a PING is successful. franmf Programming 1 10-24-2012 01:56 PM
how to check whether execve is successful or not? krisonearth Linux - Software 0 08-28-2009 02:10 PM
Ping to IP and router successful, but not DNS D: openSUSE 11 Kbiscu1t Linux - Networking 4 06-28-2008 11:35 AM
Successful ping to proxy but cannot go to internet melverx Linux - Newbie 6 04-07-2006 03:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:32 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
Open Source Consulting | Domain Registration