- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: multiple exit codes
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 06:10 PM
04-06-2002 06:10 PM
This is a question from a recent query regarding a ping
If i have a file
HOSTNAME=hosts
for host in $(cat $HOSTNAME)
do
ping $host -n 1 | grep -i received >> /tmp/success
done
if [ $? = 0 ]
then grep -i $host /
My question is, only the last hostname is recorded in success2
How do I record each exit code from each grep
ie
have each hostname that is successful recorded in success2
Thanks in adavance
Steve
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 06:22 PM
04-06-2002 06:22 PM
Re: multiple exit codes
HOSTNAME=hosts
for host in $(cat $HOSTNAME)
do
ping $host -n 1 | grep -i received >> /tmp/success
done
if [ $? = 0 ]
then grep -i $host /
echo $? >> /tmp/success2
else
echo $? >> /tmp/success
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 06:39 PM
04-06-2002 06:39 PM
Re: multiple exit codes
HOSTNAME=hosts
for host in $(cat $HOSTNAME)
do
ping $host -n 1 | grep -i received >> /tmp/success
echo $hosts " " $? >> /tmp/grep_host_values
done
if [ $? = 0 ]
then grep -i $host /
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 06:40 PM
04-06-2002 06:40 PM
Re: multiple exit codes
Sorry I don't think I explained correctly
What I meant was
Each grep produces an exit code ( success = 0)
(failure = 1)
If the grep is a success then continue with the next grep from the hosts file
So if all sites are up and ok their details from the /etc/hosts will be in success2
At the moment only the last hostname is
Cheers
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 06:44 PM
04-06-2002 06:44 PM
Re: multiple exit codes
Your last post only returns the last exit code
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 06:51 PM
04-06-2002 06:51 PM
Re: multiple exit codes
sorry I didnt understand, your problem is the grep for details on hosts lies outside of the loop so is only performed on the last hostname in the list. Try this
HOSTNAME=hosts
for host in $(cat $HOSTNAME)
do
ping $host -n 1 | grep -i received >> /tmp/success
if [ $? = 0 ]
then grep -i $host /
fi
done
cheers
John.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 06:51 PM
04-06-2002 06:51 PM
Re: multiple exit codes
The initial host file will be different each week.
To build the host file i'm performing an ls
Is there a sed command to replace all the entries in the host file to lowercase?
Thanks
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 07:22 PM
04-06-2002 07:22 PM
Re: multiple exit codes
Cool, thats great thanks
Regards
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 07:25 PM
04-06-2002 07:25 PM
Re: multiple exit codes
Im still a little confused as to what your trying to do have a look at teh file attached I did a small similar script here and if you look at the output from the pings they all have received even when my network is presently switched off so I think you will have a problem.
cheers
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 07:32 PM
04-06-2002 07:32 PM
Re: multiple exit codes
Im betting you need to change your grep from bieng "recieved" to
grep -v "100% packet loss"
John.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 07:57 PM
04-06-2002 07:57 PM
Re: multiple exit codes
Basically, I have a whole bunch of files in a directory, within the file name is the hostname.
I've extracted the hostname from the filename using the cut command and output to host. I then need to see if the hostname is available before initiating a connect direct process to resend the files to each server
I did post another question regarding the connect direct part of the script
I've only just done the scripting course so still trying to get used to all the syntax. I'm trying to automate all the boring stuff I have to done whilst on the support desk
Here's the link to the other question
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xa1eb935c6049d611abda0090277a778c,00.html
I posted it in the wrong area so it may not have been picked up by all
Regards
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2002 08:05 PM
04-06-2002 08:05 PM
Re: multiple exit codes
my last for today well its probably not the last of today but its 6am here in UK and I have been up all night so now I need some sleep before the kids get me.
your description makes sense to me now and as I sad in my last post you definetly need to change the grep statement at the moment it will say all hosts are contactable even if they are down.
later
John.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2002 05:57 AM
04-07-2002 05:57 AM
Solutionif you use $?, it will give you the returnvalue of the for-do-done statement, which always be a success since your for-do-done syntax is correct and always finish after the last host, therefor succes2 contains only the last hostname of the list.
for
do
ping......
done
if [ $? ...
should be
for
do
ping .....
if [ $? ...
done
Now, $? gives the result of the ping it self.
I would suggest a script like:
$HOSTFILE="/etc/hosts"
for host in `cat $HOSTFILE`
do
ping $host -n 2
if [ $? = 0 ]
then grep $host $HOSTFILE >> /tmp/pingablehosts
fi
done
,where /tmp/pingablehosts is the pingable subset of /etc/hosts.
Success
Ceesjan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2002 10:43 AM
04-07-2002 10:43 AM
Re: multiple exit codes
Thanks for the reply
Yes your are right, this is what I ended up with
host_name=hosts
for host in $(cat $host_name)
do
/etc/ping $host -n 1 | grep -q '1 packets received'
if [ $? = 0 ]
then
echo "$host" >> /home/H092AHE/scripts/success
else
echo "$host" >> /home/H092AHE/scripts/failure
fi
done
Cheers
Steve