LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2017, 03:53 AM   #1
CruiserCZ
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Rep: Reputation: Disabled
Bash script to ping 2 IP addresses, then -->


Hi,

I need help. Currently using OpenSUSE distribution and I need to create a bash script, that will ping two IP addreses and only do an action if both are unavailable. Is that possible somehow ?

Thank you very much,
any help is much appreciated
 
Old 04-03-2017, 03:58 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Yes, see how to do conditional statements, as well as the full manual for ping

Code:
man ping
All programs produce an exit code and can thus be used in a conditional statement. If you want to save that exit code for later, then save the variable $? to another variable immediately after the program is run before doing anything else.

How far can you get with that? Which shell scripting guide or tutorial are you working from?
 
Old 04-03-2017, 08:29 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
for i in ip_one ip_two ; do ping -c 1 -w2 $i> /dev/null ; if [ $? -eq 0 ] ; then echo $i appears up; else echo $i ""may"" be down ; fi  ; done
Example:
Code:
for i in 185.156.173.235 52.202.3.120 ; do ping -c 1 -w2 $i> /dev/null ; if [ $? -eq 0 ] ; then echo $i is up; else echo $i ""may"" be down ; fi  ; done
provides
Code:
185.156.173.235 is up
52.202.3.120 may be down
Read the conditionals link provided by Turbocapatalist, it's used in my example.

That should do until "help" arrives.
 
Old 04-03-2017, 03:06 PM   #4
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,997

Rep: Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628
Random thoughts.
I am less and less a fan of ping each year. Wonder if other means would provide better tests of connectivity?
 
Old 04-03-2017, 05:46 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
I don't trust it as "reliable" and it is firmly stuck in my noggin'.

Just sayin'
script unused since 2014-01-15
 
Old 04-04-2017, 03:31 PM   #6
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,997

Rep: Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628Reputation: 3628
I was just thinking out loud. Didn't have a better way.
 
Old 04-04-2017, 04:54 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,675

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by Habitual View Post
I don't trust it as "reliable" and it is firmly stuck in my noggin'.

Just sayin'
script unused since 2014-01-15
When I don't trust ping, I use SNMP:
Code:
snmpget -v 1 -c public <some IP address/host> 1.3.6.1.2.1.1.1.0"
...and replace the version and community as needed too. That OID is the system OID, and that particular node will return the system description. 1.3.6.1.2.1.1.5 will return the system name as set in the SNMP configuration.

I trust that a *BIT* more, because I've seen systems respond to an ICMP ping, but be non-responsive otherwise. This, at least, indicates the system is functioning on SOME level past Layer 3.
 
2 members found this post helpful.
  


Reply



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
Script to ping 100 IP addresses neelavathi Programming 2 10-13-2014 11:12 AM
Script to ping multiple IP addresses one by one peddip Linux - Server 14 09-15-2011 11:44 PM
[SOLVED] Ping IP addresses using bash and linux ericasass Programming 3 06-14-2011 10:31 PM
Writing a bash script in BackTrack3 for IP Addresses RexRogan Linux - Newbie 3 12-07-2009 09:26 PM
bash script, parsing email addresses kepler Programming 6 01-26-2004 06:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:13 AM.

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