Network connection test

Hi,

should be a simple one....

How would i test to see if a network connection is present from within a bash script? If not exit ..

pixitronAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jlevieCommented:
Basic conectivity?

ping -c3 some-ip >/dev/null
if [ $? -ne 0 ]; then
  echo "no network"
  exit 1
fi
0
pixitronAuthor Commented:
yeah this looks exactly like what i need!

However when i try to run it under cygwin the "no network" always shows up. (yip have set the ip address and can reach it via normal cygwin command line)

has it anything to do with the dev/null or -c3 ?
0
pixitronAuthor Commented:
hi again

Just check man ping on cygwin - looks like you cant use the -c option. I removed that from your above snippet and everything worked fine. ALthough it defaults to four packets, so its not exactly a fast check. Nevertheless thanks very much for the excellent very prompt answer!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pixitronAuthor Commented:
a further update - the equivalent option in cygwin is

ping -n 1 ip_addr
0
jlevieCommented:
Dunno about what options the Cygwin ping might take...
0
pixitronAuthor Commented:
this is very weird- its stopped working ????? And I'm using the following code as you suggested...

ping $backup_server >/dev/null
if [ $? -ne 0 ]; then
  echo "Sorry no route to the Backup Server Exists"
  echo "This is probably due to lack of network connection"
  exit 1       
fi

Even with the network cable disconnected it never exits or does the echo commands

I fear it might be a cygwin issue?
0
jlevieCommented:
I don't know what the behaviour would be with no network connection on a windows box. But from what you describe it seems that the ping command hangs instead of returning a 'destination unreachable' or 'no route to host' like it would on a Linux or Unix box. That would either be a problem in the Cygwin implenetation of ping or in the implementation of the TCP/IP stack in windows.
0
pixitronAuthor Commented:
well actually its odd cos when i i doi the folowing with a network connection

ping ip_address

Pinging X.X.X.X with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Destination host unreachable.

Ping statistics for X.X.X.X:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum =  0ms, Average =  0ms


So i would have thought it would be ok?
0
jlevieCommented:
You might get a different behaviour with a connection up and pinging an unreachable IP than when you disconnect the cable. And that probably is as a result of the windows TCP/IP implementation.
0
pixitronAuthor Commented:
yeah its weird, if i type:

echo $?

After a "failed" ping, it returns a 0.

And have tried different network type commands, such as nslookup from Cygwin, and even if they fail they return 0

0
pixitronAuthor Commented:
just tried ssh without a network connection, and that thankfully returned a 1. But I'm not sure if i could use that from within a script, i dont think i can
0
jlevieCommented:
There's another way to solve this. What does a successful ping display?
0
pixitronAuthor Commented:
Thats where its really strange - both successful AND unsucessful pings are returning 0.  (echo $? to check status of last  command - correct?)
0
jlevieCommented:
So it seems that at least some of the tools in the Cygwin package don't work as expected. Another way of solving the problem is to check the results of a 'ping -n1 some-ip' for somthing that is only there if the ping succeeds. On a Linux box I'd see:

wilowisp> ping -c 1 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=0 ttl=64 time=0.400 ms
 
--- 192.168.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.400/0.400/0.400/0.000 ms, pipe 2

So if I couldn't get error status out of $? I could do:

ip=some-ip
ping -c1 $ip | grep "64 bytes from $ip" >/dev/null
if [ $? ne 1 ]; then
  echo "no network"
  exit
fi

That, of course, assumes that grep will return valid status in $?. And even if you can't get valid status from grep there's yet another way:

res=`ping -c 1 192.168.0.1 | grep "64 bytes from 192.168.0.2"`
if [ -z "$res" ]; then
  echo "no network"
  exit
fi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pixitronAuthor Commented:
aha! very clever - thanks very much. I think now I''ll close this question, you've more than answered my initial question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.