Using ping in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using ping in script
# 1  
Old 03-23-2012
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.

Code:
#!/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

Last edited by methyl; 03-23-2012 at 06:05 PM.. Reason: please use code tags
# 2  
Old 03-23-2012
Try:
Code:
ping -c2 -I "$nic"  "$dest"

# 3  
Old 03-23-2012
That's also a useless use of backticks and useless use of cat.

Code:
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

# 4  
Old 03-23-2012
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. Smilie
# 5  
Old 03-24-2012
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?

Last edited by methyl; 03-24-2012 at 06:51 PM.. Reason: remove comment because this is probably Linux not unix
# 6  
Old 03-26-2012
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.
# 7  
Old 03-26-2012
Quote:
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?

Last edited by methyl; 03-26-2012 at 12:50 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

2. Shell Programming and Scripting

Script with ping

I have a question is there any posibility for writing a script that you can see if a pc in your network is up of down , when you run this script for ex. ./test.sh 63 45 54 , which are the ip adresses of the computers , when you give the last digit of the ip adres as argument 63 , 45 and 54 are... (5 Replies)
Discussion started by: Roggy
5 Replies

3. Shell Programming and Scripting

ping script

hello fellows, I need help with a script, I'm using this one HOSTS="192.168.10.9 192.168.10.15 " SUBJECT="Attention - Gateway San Martin DOWN " COUNT=1 EMAILID="lmail@mydomain.com" for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2... (2 Replies)
Discussion started by: lucas.fradusco
2 Replies

4. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

5. Shell Programming and Scripting

Animation Ping on Solaris Like Cisco Ping

Hi, I develop simple animation ping script on Solaris Platform. It is like Cisco ping. Examples and source code are below. bash-3.00$ gokcell 152.155.180.8 30 Sending 30 Ping Packets to 152.155.180.8 !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

6. Shell Programming and Scripting

Help With Ping Script

Hello all...I'm new to Unix and learning. What I'm trying to create is a script that will ping a known range of IP addresses, say 192.168.1.1 to 192.168.1.254. For each address that no reply is received, that address will be written to a log file that will be emailed to an administrator. My wife... (1 Reply)
Discussion started by: spmitchell
1 Replies

7. Shell Programming and Scripting

Help with Ping Script and AWK

Hey Guys, First time posting here. I keep getting back here on my google searches to help me in my scripting quest. Can i ask for some guidance please? I acquired a script, from these forums in fact that pings a list of IPs/Hostnames. Works a treat. Echos results to a file with the value... (3 Replies)
Discussion started by: stacky69
3 Replies

8. Shell Programming and Scripting

Ping script

I want find from a file , where list of hosts is given . Whether SSH is running on that Hosts and and create 2 files : HOSt-SSH-Running HOST-SSH-Not-running In the list we have some Windows and Unix Servers, windows servers do not have sshd running. (2 Replies)
Discussion started by: sriram003
2 Replies

9. UNIX for Advanced & Expert Users

script to ping servers

Hi , I would like to automate a script to ping all the unix servers perodically thru cronjob. Is there any script out there? If so Please give me. Thanks in advance. (2 Replies)
Discussion started by: sriny
2 Replies

10. Shell Programming and Scripting

Ping Script

hello, I am looking to make a script that will ping a remote ip address. Upon completion of the ping I want the program to either ping again if the # of packets transmitted is equal to the number of packets received or exit if the two values are unequal and information was lost. I am not sure if I... (3 Replies)
Discussion started by: mcrosby
3 Replies
Login or Register to Ask a Question