LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   For loop iterating over a list of IP addresses from a txt file (https://www.linuxquestions.org/questions/programming-9/for-loop-iterating-over-a-list-of-ip-addresses-from-a-txt-file-4175624900/)

hebruiser 03-03-2018 08:39 PM

For loop iterating over a list of IP addresses from a txt file
 
It's me again. I have the script working, but the script is not pinging the address 3 times and moving on to the next address. Also, the message that I want to display before each new ping session is not printing.

As always, any help is great.

Here's the cute little codelette I have so far...

Code:

#!/bin/bash

# Script to test server connectivity using ping

for addr in $(cat vms.txt)
do
        echo "Pinging IP address "$addr
        ping -c 3 "$addr"
done


BW-userx 03-03-2018 09:08 PM

seems like it worked for me
what is your test file looking like?
Code:

#!/bin/bash

# Script to test server connectivity using ping

for addr in $(cat pingy)
do
    echo "Pinging IP address "$addr
    ping -c 3 "$addr"
done

Code:

yahoo.com
google.com
www.linuxquestions.org

Code:

$ ./pingloop
Pinging IP address yahoo.com
PING yahoo.com (206.190.39.42) 56(84) bytes of data.
64 bytes from media-router-fp1.prod.media.vip.gq1.yahoo.com (206.190.39.42): icmp_seq=1 ttl=46 time=85.9 ms
64 bytes from media-router-fp1.prod.media.vip.gq1.yahoo.com (206.190.39.42): icmp_seq=2 ttl=46 time=88.4 ms
64 bytes from media-router-fp1.prod.media.vip.gq1.yahoo.com (206.190.39.42): icmp_seq=3 ttl=46 time=87.8 ms

--- yahoo.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 85.923/87.426/88.496/1.094 ms
Pinging IP address google.com
PING google.com (172.217.6.174) 56(84) bytes of data.
64 bytes from dfw25s17-in-f14.1e100.net (172.217.6.174): icmp_seq=1 ttl=53 time=23.9 ms
64 bytes from dfw25s17-in-f14.1e100.net (172.217.6.174): icmp_seq=2 ttl=53 time=25.3 ms
64 bytes from dfw25s17-in-f14.1e100.net (172.217.6.174): icmp_seq=3 ttl=53 time=27.6 ms

--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 23.951/25.640/27.601/1.502 ms
Pinging IP address www.linuxquestions.org
PING www.linuxquestions.org (75.126.162.205) 56(84) bytes of data.
64 bytes from www.linuxquestions.org (75.126.162.205): icmp_seq=1 ttl=53 time=30.5 ms
64 bytes from www.linuxquestions.org (75.126.162.205): icmp_seq=2 ttl=53 time=21.1 ms
64 bytes from www.linuxquestions.org (75.126.162.205): icmp_seq=3 ttl=53 time=30.1 ms

--- www.linuxquestions.org ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 21.113/27.277/30.548/4.365 ms


hebruiser 03-03-2018 10:23 PM

Perfect. Thank you. It appears, since I was experimenting with Geany (I use VIM all the time), I hadn't saved the script so the changes were not registering. Thank you for everything! I am the crowned prince of making things harder than they need to be.

BW-userx 03-03-2018 10:33 PM

Quote:

Originally Posted by hebruiser (Post 5827067)
Perfect. Thank you. It appears, since I was experimenting with Geany (I use VIM all the time), I hadn't saved the script so the changes were not registering. Thank you for everything! I am the crowned prince of making things harder than they need to be.

perhaps slowing down a bit and double checking your work might help.
No harm no foul.
Cheers!
:D


All times are GMT -5. The time now is 10:53 PM.