LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 08-03-2009, 05:14 AM   #1
info1686
LQ Newbie
 
Registered: Jan 2009
Posts: 15

Rep: Reputation: 0
Shell script to ping machines


Hi All,
I am trying to write a shell script to ping a set of machines and generate a report.
Here is the script I have written till now:
Code:
#!/bin/bash/

x=`cat machines.txt|wc -l`
for ((i=1;i<=$x;i++))
do
        machine=`sed -n "${i}p" machines.txt`
        echo $machine
        command=`(ping -c 4 $machine) >> output.txt`
        $command
        if [ $? -eq 0 ]
        then
                echo -e "Machine is up"
        fi
done
Still lots has to be done.

the "ping" command returns exit status non-zero if it fails.
However, here even for machines that are down, it prints
"Machine is up"

The same command outside of the script has exit status 1 for the machine that is down.

What can be the issue?

Thanks
 
Old 08-03-2009, 05:25 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,981
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
Hello info1686

It may be instructive to check the value of $command
Code:
        <stuff>
        command=`(ping -c 4 $machine) >> output.txt`
        echo "'$command'"
        $command
        <stuff>
The single quotes inside the double quotes are good practice to show any leading or trailing whitespace characters.

Best

Charles
 
Old 08-03-2009, 05:28 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,821

Rep: Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116
why are you running "$command"??? that means you'll be RUNNING the output of ping as if it were a program... remove that line and you'll possibly be OK.
 
Old 08-03-2009, 05:31 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 39,821

Rep: Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116Reputation: 1116
That's a pointless for loop too, why are you not just reading in the file itself as the loop input?

while read ipaddress < file.txt
do
...
done

or even

for ipaddress in $(cat file.txt)
do
...
done

Last edited by acid_kewpie; 08-03-2009 at 05:33 AM.
 
Old 08-03-2009, 06:43 AM   #5
info1686
LQ Newbie
 
Registered: Jan 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Hi,
It helped
thanks a lot
 
Old 08-03-2009, 06:43 AM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 95
If you're doing this for the scripting practice, then probably just ignore this...

$ nmap -sP -iL machines.txt

Dave
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Canīt ping machines with dynamic IPīs Wurzelseppi Linux - Networking 8 05-19-2009 02:16 AM
Cannot ping Fedora 7 from other machines pching Linux - Newbie 8 04-12-2009 09:54 AM
ping shell script goncalopp Linux - Newbie 5 07-14-2008 11:53 PM
Cannot ping between 2 machines on same router acampbell Linux - Wireless Networking 8 02-24-2004 03:27 AM
Ping between machines triplem Linux - Networking 1 04-14-2003 05:17 PM


All times are GMT -5. The time now is 12:00 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration