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.
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.
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:
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.
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
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...
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?
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
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.