ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 !
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.
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...
#!/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
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.