LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-27-2008, 03:18 AM   #1
kaujot
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Rep: Reputation: 0
Handling a "No route to host" message in a script


Okay,

So the script I'm trying to write looks at every connected computer in our lab, and prints out the ones who have no users. However, if a machine responds with "No route to host," I don't want that message nor the name of the machine to the printed.

Here's my script so far:

Code:
#!/bin/bash

machines=$(cshosts publinux)
for mach in $machines; do
    if [ "$($mach who)" == "" ]; then
      if [ "$($mach who)" != "$mach.url: No route to host" ]; then
        echo $mach
      fi
    fi
done
My output:
(Machine)
(Machine)
(Machine.url: No route to host)
(Machine.url: No route to host)
(Machine) [same as machine with no route found]

What's wrong with my logic? I can't quite seem to figure this out.

Thanks for your help.
 
Old 03-27-2008, 05:56 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
The easiest way (if possible) would be to avoid string ops, set a flag like error=0; do
output=$(thecommand --options $item 2>&1); and use the exit value, like error=$?.
[ $error -ge 0 ] && { echo you have a problem with this host; so you can skip the rest; }
If you need to use the output then you could echo "$output"|grep -q 'No.route' && error=1
or case "$output" in *route*) error=1;; esac to set a flag. So based on [ $error -eq 0 ] you
know if you want to && echo "$machine $output".

Code:
#!/bin/bash
LIST=$(cshosts publinux); for ITEM in $LIST; do
 ERROR=0; OUTPUT=$($CMD --options $ITEM 2>&1)
 ERROR=$?; [ $ERROR -eq 0 ] && echo "${ITEM}: \
${OUTPUT}"; done; exit 0
 
Old 03-27-2008, 06:14 PM   #3
kaujot
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you! Worked wonderfully.
 
  


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
shorewall routing issue: "no route to host" from dmz spargonaut Linux - Networking 0 06-07-2007 10:09 AM
a/p connected, route correct, ping router: "Destination Host Unreachable". DebianEtch shinyblue Linux - Wireless Networking 1 08-29-2006 09:34 PM
"No route to host" on RH 8.0 + W2K LAN szatki Linux - Networking 8 02-19-2004 12:30 PM
could "no route to host" be caused by non-crossover cable? brandonweinberg Linux - Networking 13 01-31-2004 09:47 AM
Permanently set "route add" -host and default gw sacants Linux - Newbie 1 07-18-2003 04:04 AM

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

All times are GMT -5. The time now is 07:46 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