[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Network connection test

Posted on 2004-11-30
15
Medium Priority
?
276 Views
Last Modified: 2010-04-20
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 ..

0
Comment
Question by:pixitron
  • 9
  • 6
15 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 12710644
Basic conectivity?

ping -c3 some-ip >/dev/null
if [ $? -ne 0 ]; then
  echo "no network"
  exit 1
fi
0
 

Author Comment

by:pixitron
ID: 12710710
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
 

Author Comment

by:pixitron
ID: 12710760
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:pixitron
ID: 12710773
a further update - the equivalent option in cygwin is

ping -n 1 ip_addr
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12710778
Dunno about what options the Cygwin ping might take...
0
 

Author Comment

by:pixitron
ID: 12710945
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
 
LVL 40

Expert Comment

by:jlevie
ID: 12711360
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
 

Author Comment

by:pixitron
ID: 12711418
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
 
LVL 40

Expert Comment

by:jlevie
ID: 12711547
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
 

Author Comment

by:pixitron
ID: 12713887
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
 

Author Comment

by:pixitron
ID: 12714030
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
 
LVL 40

Expert Comment

by:jlevie
ID: 12717181
There's another way to solve this. What does a successful ping display?
0
 

Author Comment

by:pixitron
ID: 12717380
Thats where its really strange - both successful AND unsucessful pings are returning 0.  (echo $? to check status of last  command - correct?)
0
 
LVL 40

Accepted Solution

by:
jlevie earned 200 total points
ID: 12719768
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
 

Author Comment

by:pixitron
ID: 12723835
aha! very clever - thanks very much. I think now I''ll close this question, you've more than answered my initial question.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question