LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 06-22-2017, 06:16 AM   #1
inderpal2406
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Rep: Reputation: Disabled
Ping command not working in for loop !


Hi,

I am trying to ping a list of host names in info.txt. If the hostname gets pinged successfully then I store the name of that host in separate file success.txt. If ping fails due to any reason then I store the hostname in fail.txt. I use the following code in bash script.

#!/bin/bash
for i in `cat /home/isaini/info.txt`
do
ping $i -n 4 >/dev/null 2>/dev/null;
pingresult=$?;
if [ "$pingresult" -eq 0 ]
then
echo $i >> /home/isaini/success.txt;
else
echo $i >> /home/isaini/fail.txt;
fi
done

Note: In HP-UX ping hostname -n 4 will send 4 packets to hostname to test connection (-n option suggests number of packets)


However, when I execute this script, it gives the error of "unknown host" for each host and the fail.txt file is generated with the same names in info.txt.

But when I try to ping that host on CLI, it gets successfully pinged. I guess, there is issue with for loop. Please help !

BR,
Inderpal Singh
 
Old 06-22-2017, 07:04 AM   #2
jareklakman
LQ Newbie
 
Registered: Jun 2017
Posts: 14

Rep: Reputation: Disabled
Try echo i variable on screen and check if it has reasonable value.
 
Old 06-22-2017, 07:44 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Try this for the top 2 lines of the script:

Code:
#!/bin/bash
set -x
The -x gives more output of each line of the script as it's executed.

Last edited by TenTenths; 06-22-2017 at 07:46 AM. Reason: Realised my original answer was rubbish :(
 
Old 06-22-2017, 07:47 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by TenTenths View Post
RTFM!

Code:
man ping
-n     Numeric output only.  No attempt will be made to lookup symbolic names for host addresses.
To send only 4 packets:
Code:
man ping
-c count
              Stop  after  sending  count ECHO_REQUEST packets. With deadline option, ping waits for count
              ECHO_REPLY packets, until the timeout expires.
OP mentioned that they were using HP-UX (or implied it), where the ping command options are different.

http://nixdoc.net/man-pages/hp-ux/man1/ping.1m.html

RTFOP!
 
Old 06-22-2017, 07:47 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by hydrurga View Post
OP mentioned that they were using HP-UX (or implied it), where the ping command options are different.

http://nixdoc.net/man-pages/hp-ux/man1/ping.1m.html

RTFOP!
Yeah, it was a personal FAIL! That's why I edited my post a couple seconds ago!
 
Old 06-22-2017, 07:51 AM   #6
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by TenTenths View Post
Yeah, it was a personal FAIL! That's why I edited my post a couple seconds ago!
No worries. You've got to get up before you go to bed to get in there before me. I have to admit that I'm intrigued as to the reason(s) for the OP's issue. I hope they get back to us...
 
Old 06-22-2017, 08:21 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Added this question into the FAQ: http://www.linuxquestions.org/questi...0/#post5725686

Nonetheless you still should edit your Original Post and add [code] and [/code] tags

Last edited by NevemTeve; 06-22-2017 at 08:25 AM.
 
Old 06-22-2017, 08:39 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
not to speak about the misleading title....
 
Old 06-22-2017, 09:15 AM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
#!/bin/bash
for i in $(cat /home/isaini/info.txt) ; do ping -n 4 $i> /dev/null ; if [ $? -eq 0 ] ; then echo $i is up; else echo $i ""may"" be down ; fi  ; done
perhaps?

Last edited by Habitual; 06-22-2017 at 09:16 AM.
 
Old 06-22-2017, 09:16 AM   #10
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
Can't ping take a while to run, especially if there's no response? You might have to do this in C or Perl or something and fork() then wait() for it...

Does your system respond with ping with something like this:

Code:
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.059 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.068 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.072 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.052 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.065 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.069 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.126 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.092 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.064 ms
or like this:
Code:
127.0.0.1 is alive

Last edited by Laserbeak; 06-22-2017 at 09:18 AM.
 
Old 06-22-2017, 09:18 AM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
I usually utilize "-c1 -w1" in my code. but I took the OP's inputs here.

-c1 is count=1
-w1 is wait=1s

Last edited by Habitual; 06-22-2017 at 09:53 AM. Reason: Added switches meanings
 
Old 06-22-2017, 09:50 AM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
if it has not been pointed out exactly. If you ever ran ping by itself you'd see that it will not stop on its own, so logical questions.

How to get only one return from ping to see if it is connected?

The bigger question would then be.

how to get only one return off ping then put that into a variable while ending ping, take that variable to check it and/or perform necessary operations on that return value then move to next ip address using ping and repeat for each IP by using a loop?

man ping
-n Numeric output only. No attempt will be made to lookup symbolic names for host addresses.

Modding @Habitual script
Code:
#!/bin/bash
set -x

for i in $(cat /home/userx/info.txt) 
do 
ping $i -c 1 > /dev/null 
if [ $? -eq 0 ] ; 
then echo $i is up
else 
echo $i ""may"" be down 
fi  
done
test file
Code:
yahoo.com
75.126.162.205
google.com
http://www.linuxquestions.org
linuxquestions.org
75.126.162.205 is LinuxQuestions ip off of find IP
https://www.site24x7.com/find-ip-add...-web-site.html

linuxquestions.org used to get 75.126.162.205

results
Code:
userx%slackwhere ⚡ testing ⚡> ./Pinning                                         
++ cat /home/userx/info.txt
+ for i in '$(cat /home/userx/info.txt)'
+ ping yahoo.com -c 1
+ '[' 0 -eq 0 ']'
+ echo yahoo.com is up
yahoo.com is up
+ for i in '$(cat /home/userx/info.txt)'
+ ping 75.126.162.205 -c 1
+ '[' 0 -eq 0 ']'
+ echo 75.126.162.205 is up
75.126.162.205 is up
+ for i in '$(cat /home/userx/info.txt)'
+ ping google.com -c 1
+ '[' 0 -eq 0 ']'
+ echo google.com is up
google.com is up
+ for i in '$(cat /home/userx/info.txt)'
+ ping http://www.linuxquestions.org -c 1
ping: unknown host http://www.linuxquestions.org
+ '[' 2 -eq 0 ']'
+ echo http://www.linuxquestions.org may be down
http://www.linuxquestions.org may be down
+ for i in '$(cat /home/userx/info.txt)'
+ ping linuxquestions.org -c 1
+ '[' 0 -eq 0 ']'
+ echo linuxquestions.org is up
linuxquestions.org is up
userx%slackwhere ⚡ testing ⚡>
Notice the last two results and compare them to the test file naming conventions.

man ping

Quote:
-c count
Stop after sending count ECHO_REQUEST packets. With deadline
option, ping waits for count ECHO_REPLY packets, until the time-
out expires.

Last edited by BW-userx; 06-22-2017 at 10:31 AM.
 
Old 06-22-2017, 09:52 AM   #13
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
and ping is unreliable as a "test".
 
Old 06-22-2017, 10:39 AM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Habitual View Post
and ping is unreliable as a "test".
what is inxi ?
Code:
userx%slackwhere ⚡ testing ⚡> inxi -Fszr -c0 | nc termbin.com 9999
bash: inxi: command not found
^C
here I get returned 0 for reachable
Code:
userx%slackwhere ⚡ testing ⚡> ping -c 1 yahoo.com > /dev/null ; echo $?
0
 
Old 06-22-2017, 10:47 AM   #15
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by BW-userx View Post
what is inxi ?
I believe it's a system information tool and is actually (IMHO misleadingly) the posters signature and shouldn't be construed as part of the reply.
 
1 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
dig command not working whereas ping and nslookup works fine nitinprabhu Linux - Networking 6 12-08-2016 09:03 PM
cat command inside foreach loop not working Praveen waliitagi Linux - Newbie 6 10-17-2013 02:09 AM
Writing a command (for loop) that would ping a Class C subnet medeiom Linux - Newbie 3 03-17-2012 08:49 AM
[SOLVED] Host and ping command not working dheerajsuthar2008 Linux - Networking 26 03-08-2010 02:45 PM
ping command not working in tcl/tk? Passions Programming 2 02-17-2010 05:24 PM

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

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