Break command

have a query on this break. I am using a program where I am using a while loop to execute it. It will get into a file take the first file and then ping it and if the ip is reachable then it will mail. Now what happens is that when i ping the ip it does nopt come out of the loop and it says "reply from reply from" . I need to usse a break statemetn to come out of this. Can you assist me in this.

Let me know if i need to explain more.

To really help you with your problem it would be best to post your code - otherwise we are just left to guessing what the error might be.

Generally speaking it is better to not use "break" at all: it is basically a "GOTO" (in fact it is a "GOTO END OF LOOP", which is a hallmark of unstructured programming. You might want to read the famous article A Case against the GO TO statement by Edsger W. Dijkstra to better understand why this is bad practice.

The same is true, btw., for the "continue" statement.

I hope this helps.

bakunin

Absolutely!

Nonsense!

Hello there,
I think You should consider using ping with the count option, otherwise it will continue pinging forever and You will never get out of the loop. Example:

ping -c 1 host

And after that, read the return value ($?) if You need to take action if it fails or is successful. Another example:

lakris@landet:~/projekt$ ping -c 1 localhost; echo $?
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.039 ms

--- localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.039/0.039/0.039/0.000 ms
0
lakris@landet:~/projekt$ ping -c 1 unknownhost; echo $?
ping: unknown host unknownhost
2
lakris@landet:~/projekt$ 

Regards,
/Lakris

And by the way,
why the hangup about GOTO?
It is of course up to the programmer to construct legible and functional code, and for a small program or asm it is very useful. AND can be very readable too. Of course it is silly to use GOTO in a large program where it's hard to keep track of the state of things. But everything's not black and white.

/Lakris

Here is the code snippet which i am using . actually wheen it tries to ping a node it pings it and then ig et a message sayng " Time exceeded" then it doesent come outside of the loop, which triggers more than 50 mails in 5 secs. Please advice:(

i=1
while [ $i -le 7 ]
do
     k=`cat iplist |head -$i|tail -1`
     ping $k
     if [$? -eq 0]
          then echo " The node $k is alive " |  mailx -s " Network connectivity " venki_dadad @in.com
          else echo " The node $k is down  " | mailx -s " Network connectivity " venki_dadad @in.com
          i=`expr $i + 1`
          echo $i
     fi
done

Hi, did You read my previous post about ping?
I can think of a few problems with Your code, but let's be systematic :slight_smile:

1) If You have a list of hosts in a file, if iplist is a list of k's, then use that as a basis for Your loop and don't construct some strange mechanism that force You to change Your code just because the list changes.

Example:

while read k ; do
... some action here based on value of k ...
done < iplist

2) If You are on a *nix system, ping by default will continue to ping until You tell it to stop, so You must give it a condition so it's behaviour is predictable. As I said before. That IS Your break.

Example:

ping -c 1 $k

3) You can't have a space in the e-mail address, is it a typo? Make a habit of copy/paste when You submit code examples, otherwise it's very easy to make spleling mistakes.

And if You really want to know the status of the nodes, what is most important about it? To know if it's down? Maybe it's not very interesting to know that things are working as they should? Because, well, they should. Then just send a mail when a node is unreachable.

And if it's really important, why not put that information in the header, so it stands out?

Example:

while read k ; do
ping -c 1 $k &> /dev/null || echo "$k is DOWN, I repeat, $k is DOWN" |mailx -s "$k is DOWN" me@host 
done < iplist

which means, "if the ping command isn't successful ( || is an "else" ), then send me a mail about it". Try && for a different outcome...

Best regards,
Lakris

Hi , Two things which i wanted clarify

The reason behind the implementation is that we have multiple connections to various nodes through which files move in and out. so we are monitoring the ip also we are trying to implement the telnet functionality in this.

I have sorted out the ping command functionality by using grep option whihc would get the particular pattern and then decide accordingly.

Now the next step which i would be following is telnet.

so here i telnet to the ip with the port no

say for example

telnet 112.123.123.121 xxxx where xxxx is the port no
now here after this command is fired the telnet is connected and while logging out i type logout there and comeout. This is in case of a manual connection.

Now if i am implementing this int he script

telnet 112.123.123.121 xxxx > file1

`sleep 15`

'logout`

cat file1 | grep " connected"

if [ $? -eq 0 ]

then

echo " the node is up " | mailx -s " node connecitivty " venkidhadha@in.com

The problem which happens here is that after loggin in the logout command doesnot work as now we are not in the original system where the scirpt is present.

How can do a remote logout of telnet so that the next step "LOGOUT" works?

Sorry if this seems to be a basic question.:slight_smile:

Thanks a lot actually i have sorted that issue out. i am using a grep option and from that i am retreiving the data. Also now i need to know how do i remotely logg off my telnet seession with a vendor node from my system. I am not bale to use this in my script because the logout command in my scirpt will not work after connecting the the vendor node and oly after i log out manually the logout and the next commands in,my script works. Hope you got my point. sorry if it is too basic for you.

Assuming you only want the first 7 IP's from this file, try this also:

ips=`head -n 7 iplist`
for myip in $ips
do
     ping -c 1 $myip
     if [$? -eq 0]
     then 
         echo " The node $myip is alive " |  mailx -s " Network connectivity " venki_dadad @in.com 
     else 
         echo " The node $myip is down  " | mailx -s " Network connectivity " venki_dadad @in.com 
      fi
done

Or you could batch this in one shot like this:

ips=`head -n 7 iplist`
RESULT=""
for myip in $ips
do
     # Make this post to the console (remove the '2>&1 >> /dev/null' at the end)  or leave it so that it is quiet
     ping -c 1 $myip 2>&1 >> /dev/null
     if [$? -eq 0]
     then 
         RESULT="${RESULT} \nThe node $myip is alive "  
     else 
         RESULT="${RESULT} \nThe node $myip is down  " 
      fi
done
echo -e "${RESULT}" |  mailx -s " Network connectivity " venki_dadad @in.com

One email sent with all test results.

Ok, for this I think You should have a look at the program expect. It will enable You to script and automate any console/text based connection by logging in, looking for expected output and sending a predefined response.

/Lakris

That was why I posted AFTER you said you sorted it all out...

Why use the un-needed grep?

Look at my previous post and you will see that grep is not needed.

ips=`head -n 7 iplist`

No grep used to get these ip's.