Bash Script to Test Internet Connectivity Quickly
Use a Bash script with
ping -c 1 google.com >/dev/null 2>&1 and check $? to test internet connectivity.Examples
Inputping -c 1 google.com
Output1 packets transmitted, 1 received, 0% packet loss, time 0ms
Inputping -c 1 invalid.domain
Outputping: unknown host invalid.domain
Inputping -c 1 8.8.8.8
Output1 packets transmitted, 1 received, 0% packet loss, time 0ms
How to Think About It
To test internet connectivity, the script tries to send one network packet to a reliable website like google.com using
ping. If the ping succeeds, it means the internet is reachable; if it fails, the internet is not connected.Algorithm
1
Send one ping packet to a known website.2
Check the exit status of the ping command.3
If the exit status is zero, print 'Internet is connected'.4
Otherwise, print 'Internet is not connected'.Code
bash
#!/bin/bash ping -c 1 google.com >/dev/null 2>&1 if [ $? -eq 0 ]; then echo "Internet is connected" else echo "Internet is not connected" fi
Output
Internet is connected
Dry Run
Let's trace the script when internet is connected.
1
Run ping command
ping -c 1 google.com runs and returns exit status 0
2
Check exit status
$? equals 0, meaning success
3
Print result
Print 'Internet is connected'
| Step | Command | Exit Status | Output |
|---|---|---|---|
| 1 | ping -c 1 google.com | 0 | No output (redirected) |
| 2 | Check $? == 0 | true | N/A |
| 3 | echo result | N/A | Internet is connected |
Why This Works
Step 1: Ping command tests connectivity
The ping command sends a small packet to google.com to check if it is reachable.
Step 2: Exit status shows success or failure
The shell variable $? holds the exit status of the last command; 0 means success.
Step 3: Conditional prints result
The script uses if to print a message based on whether the ping succeeded.
Alternative Approaches
Using curl to check connectivity
bash
#!/bin/bash curl -s --head http://google.com | head -n 1 | grep "HTTP/" >/dev/null if [ $? -eq 0 ]; then echo "Internet is connected" else echo "Internet is not connected" fi
Uses HTTP request instead of ping; useful if ping is blocked but HTTP works.
Using timeout with ping
bash
#!/bin/bash if ping -c 1 -W 2 google.com >/dev/null 2>&1; then echo "Internet is connected" else echo "Internet is not connected" fi
Adds a 2-second timeout to avoid long waits if network is down.
Complexity: O(1) time, O(1) space
Time Complexity
The script runs a single ping command which takes constant time regardless of network size.
Space Complexity
The script uses minimal memory, only storing the exit status and printing a message.
Which Approach is Fastest?
Using ping with one packet is fastest; curl adds HTTP overhead but can work when ping is blocked.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Ping single packet | O(1) | O(1) | Quick basic connectivity check |
| Curl HTTP head | O(1) | O(1) | When ping is blocked but HTTP works |
| Ping with timeout | O(1) | O(1) | Faster failure detection on no network |
Use
ping -c 1 to send only one packet and keep the test fast.Beginners often forget to redirect ping output, causing cluttered terminal output.