LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2022, 09:16 AM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Help with syntax with using For and While Loop To Ping Hosts Till They Are Up


I don't script everyday (I would like to)...so these skills are perishable.

I have a script that will ping servers, show that they are up. This is my code that works, which is a for loop.

Code:
#!/bin/bash

for hostname in server{1..4}

do 
  ping -c 1 -t 1 $hostname > /dev/null && echo "${hostname} appears up"; 
done

Output of code below:

Code:
server1 appears up
server2 appears up
server3 appears up
server4 appears up

I want to add a while loop to encompass the for loop, as I want it to continually ping all servers until the servers are up, and once all ping back successfully, then drop out of the loop and and the script.

Code:
#!/bin/bash 

UNREACHABLE=1; 

while [ $UNREACHABLE -ne "0" ];
do
  ping -c 1 -t 1 $hostname > /dev/null && echo "{hostname} appears up";
done
When I run this, I'm getting "syntax error." I'm just not clicking with how to combine the two.

thanks
 
Old 01-27-2022, 09:29 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,764

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Something like:
Code:
i=1
sp="/-\|"
echo -n ' '
while true; do
    fping -q server1 server2 server3 server4
    if [ $? -eq 0 ]; then
       echo "All servers are up"
       exit
    fi
    printf "\rWaiting..${sp:i++%${#sp}:1}"
    sleep 1 # might consider adding a sleep statement. 

done

Last edited by michaelk; 01-27-2022 at 10:26 AM.
 
1 members found this post helpful.
Old 01-27-2022, 11:26 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
See also the "continuously check processes" thread in this section of LQ. If your actual objective is to query the health of other servers, there are several good subsystems (such as Nagios) that are specifically designed for doing that sort of thing. So, this is a chore that somebody else has already well-solved.
 
1 members found this post helpful.
Old 01-27-2022, 01:28 PM   #4
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
That works. Not going to install fping, so I'll keep using ping and add -c and -t flags too. Yup, spot on about adding sleep command too.

But why did you use if statement in the while loop Vs what I was originally trying to do?

thanks

Quote:
Originally Posted by michaelk View Post
Something like:
Code:
i=1
sp="/-\|"
echo -n ' '
while true; do
    fping -q server1 server2 server3 server4
    if [ $? -eq 0 ]; then
       echo "All servers are up"
       exit
    fi
    printf "\rWaiting..${sp:i++%${#sp}:1}"
    sleep 1 # might consider adding a sleep statement. 

done
 
Old 01-27-2022, 01:49 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,764

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Its the first thing that popped into my brain...

Lots of ways... Untested

Code:
#!/bin/bash

reached=0
while true; do
  for hostname in server{1..4}
  do 
    ping -c 1 -t 1 $hostname > /dev/null; 
    if [ $? -eq 0 ]; then
       reached=$((reached+1))
    fi
  done
  if [[ $reached == 4 ]]; then
     echo "Servers up"
     exit
  fi
  reached=0
done
or
Code:
#!/bin/bash

arr=(1 0 0 0 0)
while true: do
  for num in {1..4}
  do 
    ping -c 1 -t 1 "server$num" > /dev/null; 
    if [ $? -eq 0 ]; then
      arr[$num]=1
    fi
    if [[ ! " ${arr[*]} " =~ "0" ]]; then
      echo "ready"
      exit
    fi
  done
done
You need to check the return status for each server and then determine if all 4 or up or not.

Last edited by michaelk; 01-27-2022 at 03:12 PM.
 
Old 01-30-2022, 05:55 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,817

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Try this logic:
Code:
UNREACHABLE=1
while [ "$UNREACHABLE" ]
do
  UNREACHABLE=
  for hostname in server{1..4}
  do 
    if ping -c 1 -t 1 "$hostname" > /dev/null
    then
      echo "${hostname} appears up"
    else
      UNREACHABLE=1
    fi
  done
done

Last edited by MadeInGermany; 01-30-2022 at 11:22 AM.
 
Old 01-30-2022, 07:12 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,764

Rep: Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931Reputation: 5931
Simpler the mine..

Last edited by michaelk; 01-30-2022 at 07:18 AM.
 
Old 01-30-2022, 08:54 AM   #8
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,150

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
I've personally never seen anyone try to put ; at the end of a variable declaration. That would be your syntax error I believe.

Code:
UNREACHABLE=1;
should be

Code:
UNREACHABLE=1
 
Old 01-30-2022, 09:17 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,617

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by jmgibson1981 View Post
I've personally never seen anyone try to put ; at the end of a variable declaration. That would be your syntax error I believe.
Incorrect - the semi-colon simply ends the line/command and so is basically ignored. It is not a syntax error.

You can confirm this for yourself by executing and/or using ShellCheck on:

Code:
#!/bin/sh
UNREACHABLE=1;
echo "$UNREACHABLE"

(If one wanted a literal semi-colon at the end of a value, they would need to quote or escape it.)

 
  


Reply

Tags
bash loop, bash while



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] Can ping Ubuntu hosts but not Debian hosts by hostnames garyozzy Linux - Networking 6 03-09-2012 10:32 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] Awk to extract patterns till it hits blank line (in for loop) Tauro Linux - Newbie 5 07-21-2011 11:20 PM
I cannot ping with command 'ping IP' address but can ping with 'ping IP -I eth0' sanketmlad Linux - Networking 2 07-15-2011 05:32 AM
sound in a loop...till a fresh install dhruvseth Slackware 2 12-28-2006 01:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:31 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