Using ping in script

Hi. I have a server with multiple network ports that need to be tested to a list of destinations. I'm trying to write a scripts to automate this but can't seem to get past an error and could use some help.

I have two test files one contains the ip addresses of the onboard NICs and the other text file has the destinations that I am trying to reach. I read the contents of the files it variables then use them in a ping command. Here is the script so far.

#!/bin/sh
for nic in `cat nic.txt`
do
	for dest in `cat dest.txt`
	do
		echo ping -c2 -I "$nic  $dest"
		ping -c2 -I "$nic  $dest"
	done
echo "" 
done

The echo command prints out the command just as it should be but the actual ping command errors like I'm not using the command correctly. I can even copy what is echoed and paste it and the ping command works correctly.

Any help would be great.

Thanks

Try:

ping -c2 -I "$nic"  "$dest"

That's also a useless use of backticks and useless use of cat.

while read nic
do
	while read dest
	do
		echo ping -c2 -I "$nic  $dest"
		ping -c2 -I "$nic  $dest"
	done < dest.txt
echo "" 
done < nic.txt

I changed the quotes and no dice. I put that echo statement in before the ping command so I could see if the variables were correct because the ping command dosent show me what I put in. So the first line is the echo command and the second line is the output of the ping command. I'm not sure where it is getting the -- - from. It is like the variable is not transfering to that line of the script.

ping -c2 -I 98.241.83.14 69.252.164.78
ping: invalid option -- -

Conoraa688, thanks for that and I will try it once I get it to work. I dont script that often and dont know all that much so thanks for giving me another tool to use. :slight_smile:

Please post the actual script you ran. My correction was subtle because it presents two parameters to "ping" not one.
Imho. Corona688's pedantic change to avoid valid Shell constructs, includes the original problem.

There is much variation in the "ping" command and we don't know what Operating System and version you have or exactly what Shell this is.

Can you show us the command-line example which works?

That is the actual script above. The only thing that I changed is the quotes like you suggested. Here is what the output looks like when run. The first line is from the echo and the rest is from ping.

ping -c2 -I 98.246.81.14 69.252.158.78
ping: invalid option -- -
usage:
ping [-adDfLnoPqQrRv] [-c count] [-g gateway] [-h host] [-i interval] [-I addr]
[-l preload] [-p pattern] [-s size] [-t tos] [-T ttl] [-w maxwait] host

If I take and type in the command that was echoed out, it works fine.

server-1% ping -c2 -I 98.246.81.14 69.252.158.78
PING 69.252.158.78 (69.252.158.78): 56 data bytes
64 bytes from 69.252.158.78: icmp_seq=0 ttl=62 time=43.205 ms
64 bytes from 69.252.158.78: icmp_seq=1 ttl=62 time=40.115 ms

----69.252.158.78 PING Statistics----
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 40.115/41.660/43.205/2.185 ms

So it seems that the ping command is not getting the variable correctly. The OS is based off of a stripped down version of Plan 9.

Plan 9 is not unix or Linux.

First impresssion is that "ping" does not like the hyphen character in your Shell script. I have read that Plan 9 used a different character set from unix (UTF-8) which could affect quote characters. Also, are you sure that you are running the correct Shell and the correct editor?

There is more than one ancient O/S called Plan 9. I've assumed that you mean the Bell Labs one.

Does the script work without any quotes?

Methyl,
Well, after I took out the quotes, it works fine. Here is the working script. Yes, it is from the Bell Labs version. For some reason when those quotes were in there, it screwed everything up. Thanks for your help!. :slight_smile:

#!/bin/sh
for nic in `cat nic.txt`
do
	for dest in `cat dest.txt`
	do
		ping -c2 -I $nic  $dest
	done
done

Glad it worked out.

If you have future questions, please post what Operating System and version you are running and what Shell you have.

I'm a bit mystified how you managed to have what appears to be a Bourne Shell on this O/S. Also fascinated how you got into this O/S.

This thread has been a bit like getting a post from Kernighan or Ritchie (RIP) because so few people know about the O/S.

Though I choke to say this on www.unix.com a tad of the technology advancement turned up in Windows NT.

I actually thought it was a flavor of Unix but just read a little about it and it does seem different. It's running on an nCUBE box. I tried to PM you the back story but I don't have enough posts. :frowning:

Please don't PM me.

Unix developed in many directions but those which were difficult to use or had obscure or illogical syntax died. Plan 9 was one of them.

Imho. If you have low-level code expertise the most useful problem to work on is the file size limits in versions of tar and umpteen other unix commands in light of modern tetrabyte filesystems.