LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2006, 01:44 PM   #1
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Rep: Reputation: 30
Need help with a basic script


I have a text file that has 1 server per line listed in it. I need writting a script that will ping each server and report back if it is up or not.

Any help is appreciated.

Thank You.
 
Old 06-28-2006, 02:12 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Code:
for HOST in `cat hostlist`
do
  if ping -c 1 $HOST >/dev/null 2>&1
  then echo "Successfully pinged ${HOST}."
  else echo "ERROR: Unable to ping ${HOST}."
  fi
done
You would substitute the name of your file for "hostlist" above. HOST is an abitrary variable name - it can be anything you want (e.g. SERVER) but remember to change the variable call in the rest of it (e.g. ${SERVER}).

Note that the characters in the first line are back ticks rather than apostrophes.
 
Old 06-28-2006, 02:15 PM   #3
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
You want something like this:
Code:
IFS='
'
for host in $(cat file-of-hosts.txt); do
  if ping $host; then
    echo $host is up
  else
   echo $host is down
  fi
done
Check what the exit status of ping is if the host is up and down, respectively, and what other exit status can occur. Some hosts don't respond to pings, though, so you may get false negatives. You may want to have a look at host, tcptraceroute and (if you want to really get into the nuts and bolts) the ARP protocol.

Also, xargs might be useful as an alternate to the for loop.
 
Old 06-28-2006, 02:30 PM   #4
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Original Poster
Rep: Reputation: 30
Thanks!

Quote:
Originally Posted by jlightner
Code:
for HOST in `cat hostlist`
do
  if ping -c 1 $HOST >/dev/null 2>&1
  then echo "Successfully pinged ${HOST}."
  else echo "ERROR: Unable to ping ${HOST}."
  fi
done
You would substitute the name of your file for "hostlist" above. HOST is an abitrary variable name - it can be anything you want (e.g. SERVER) but remember to change the variable call in the rest of it (e.g. ${SERVER}).

Note that the characters in the first line are back ticks rather than apostrophes.

Thanks to both of you. It looks so simple, I just haven't gotten my arms around scripting yet!
 
Old 06-29-2006, 03:28 AM   #5
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Have a look at Advanced Bash Scripting guide, at http://www.tldp.org/LDP/abs/html/.
 
Old 07-03-2006, 06:28 AM   #6
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a basic UNIX script mike9287 Linux - Newbie 28 06-02-2006 02:14 AM
Some basic script q's viniosity Programming 11 02-02-2005 05:57 PM
basic shell script help lin00b Linux - Newbie 2 10-08-2004 11:32 PM
Basic shell script, please help colly Linux - General 4 10-05-2004 11:24 AM
basic script question xjphil Linux - Newbie 4 11-24-2003 08:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:06 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration