1833059 Members
2397 Online
110049 Solutions
New Discussion

Re: multiple exit codes

 
SOLVED
Go to solution
steven Burgess_2
Honored Contributor

multiple exit codes

Hi

This is a question from a recent query regarding a ping

If i have a file containing more than one hostname. I do

HOSTNAME=hosts
for host in $(cat $HOSTNAME)
do
ping $host -n 1 | grep -i received >> /tmp/success
done
if [ $? = 0 ]
then grep -i $host // >> /tmp/success2

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
take your time and think things through
13 REPLIES 13
John Carr_2
Honored Contributor

Re: multiple exit codes

Hi


HOSTNAME=hosts
for host in $(cat $HOSTNAME)
do
ping $host -n 1 | grep -i received >> /tmp/success
done
if [ $? = 0 ]
then grep -i $host // >> /tmp/success2
echo $? >> /tmp/success2
else
echo $? >> /tmp/success
fi
John Carr_2
Honored Contributor

Re: multiple exit codes

Steven please ignore my last posting I misread your neds . try this

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 // >> /tmp/success2

steven Burgess_2
Honored Contributor

Re: multiple exit codes

Hi

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
take your time and think things through
steven Burgess_2
Honored Contributor

Re: multiple exit codes

John

Your last post only returns the last exit code

Steve
take your time and think things through
John Carr_2
Honored Contributor

Re: multiple exit codes

Hi

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 // >> /tmp/success2
fi

done

cheers
John.
steven Burgess_2
Honored Contributor

Re: multiple exit codes

Also

The initial host file will be different each week.

To build the host file i'm performing an ls | cut -c16-22 > host

Is there a sed command to replace all the entries in the host file to lowercase?

Thanks

Steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: multiple exit codes

John

Cool, thats great thanks

Regards

Steve
take your time and think things through
John Carr_2
Honored Contributor

Re: multiple exit codes

Steven

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
John Carr_2
Honored Contributor

Re: multiple exit codes

Steven

Im betting you need to change your grep from bieng "recieved" to

grep -v "100% packet loss"

John.
steven Burgess_2
Honored Contributor

Re: multiple exit codes

Hi John

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
take your time and think things through
John Carr_2
Honored Contributor

Re: multiple exit codes

Hi Steven

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.
Ceesjan van Hattum
Esteemed Contributor
Solution

Re: multiple exit codes

Sorry to interfere with your discussion, but as far as i understand, you want a list of successes.
if 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
steven Burgess_2
Honored Contributor

Re: multiple exit codes

Hi

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
take your time and think things through