Explanation:
Shebang:
#!/bin/bash
specifies that this script should be interpreted using the Bash shell.check_internet Function: Uses
wget --spider
to check connectivity to a known URL (http://example.com
in this case). If the command returns 0 ($?
holds the exit status of the last command), it indicates internet connectivity.check_host Function: Uses
ping
to check connectivity to a specified host.-c 1
instructsping
to send only one ICMP echo request. If the command returns 0, connectivity to the host is confirmed.Execution: The script sequentially checks internet connectivity and connectivity to specific hosts (
google.com
,github.com
in this example). You can add more checks by callingcheck_host
with different hostnames.
Usage:
- Save this script to a file, e.g.,
check_network.sh
. - Make it executable with
chmod +x check_network.sh
. - Run it with
./check_network.sh
.
Notes:
Customization: Modify the URLs or hostnames in the
check_internet
andcheck_host
functions to suit your environment or specific requirements.Error Handling: The script assumes basic error handling. You may want to enhance it with more robust error checking and reporting based on your use case.
Security: Avoid relying solely on external URLs or hostnames that may change. Use reliable endpoints or local network resources for more accurate checks.
This script provides a foundation for checking basic network connectivity in a Linux environment. Depending on your needs, you can expand it to include more sophisticated checks, logging, or integration with other monitoring tools.
0 Comments