Cao Yi

Testing Network Accessibility in an Intranet with Bash Script


there are two hosts in the same intranet:

Here is an example to test network accessibility in bash script. It runs on host A. If A can access B, it returns “OK”, or returns “Failed”


#!/usr/bin/env bash

ping -c 1 > /dev/null
if [ $? -eq 0 ]; then
  echo "OK"
  echo "Failed"


ping -c

-c count
      Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.


What is /dev/null?

It is a virtual device, which has a special property: Any data written to /dev/null vanishes or disappears. Because of this characteristic, it is also called bitbucket or blackhole.

Quoted from


This is the exit status of the last executed command.

For example the command true always returns a status of 0 and false always returns a status of 1:

echo $? # echoes 0
echo $? # echoes 1

Quoted from