Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Odroid C2/N2, Raspberry Pi Zero - Network detection script
#!/bin/bash
# Author: Rajat Nair (aunlead.com)
# Description: This script will periodically check -
# - if device is on LAN
# - if remote share is online
# - Automount remote shares
# - Verify if DNS resolution is working
# - Verify if internet connectivity is working
# - Verify if server can be pinged
# - Verify if HTTP requests can be made
# - Automatic log cleanup when it reaches 100MB
# - Restart of device if it's not connected to LAN
#
# Usage -
# Edit Cron using
# crontab -e
# Copy script and give it execute permissions. To run every 30 minutes-
# */30 * * * * /opt/network/network-detection-script.sh
#
# Configurations
#
GATEWAY_CONFIGURED=$(/sbin/ip route | awk '/default/ { print $3 }' | awk 'NR == 1 {print; exit}')
GATEWAY_EXPECTED=192.168.2.1
VERIFY_DNS=$(cat /etc/resolv.conf | awk '/nameserver/ {print $2}' | awk 'NR == 1 {print; exit}')
VERIFY_DOMAIN=google.com
# Change your mountpoint according to your requirement
CUSTOM_MOUNTPOINT=/mnt
NETWORK_SHARE=192.168.2.3
#
# Logs
#
LOG_FILE=/var/log/network-script.log
TIMESTAMP=$(date +%s)
# 100 MB
LOG_SIZE=100000000
#
# When check fails do..
#
verificationFailure()
{
# Placeholder to implement your custom notification
# exit 1
echo "Verification failed."
}
#
# Netcat to domain on HTTPS port
#
netcatScan()
{
echo "Scan of $VERIFY_DOMAIN on port 443" >>${LOG_FILE}
if nc -zw1 $VERIFY_DOMAIN 443; then
echo "Success. $VERIFY_DOMAIN is available." >>${LOG_FILE}
else
echo "Failed. $VERIFY_DOMAIN is unreachable." >>${LOG_FILE}
verificationFailure
fi
}
#
# Ping domain
#
internetAccessVerification()
{
echo "Pinging $VERIFY_DOMAIN to check for internet connection." >>${LOG_FILE}
ping $VERIFY_DOMAIN -c 4
if [ $? -eq 0 ]; then
echo "$VERIFY_DOMAIN pingable. Internet up." >>${LOG_FILE}
else
echo "Internet down." >>${LOG_FILE}
verificationFailure
#exit 1
fi
}
#
# Verify DNS resolution
#
dnsVerification()
{
if [ "$VERIFY_DNS" != "run" ]; then
echo "Name resolution of $VERIFY_DNS using first DNS server in resolv.conf" >>${LOG_FILE}
ping "$VERIFY_DNS" -c 4
if [ $? -eq 0 ]; then
echo "DNS resolution successful for $VERIFY_DNS." >>${LOG_FILE}
else
echo "DNS resolution failed for $VERIFY_DNS." >>${LOG_FILE}
verificationFailure
#exit 1
fi
else
echo "DNS resolution skipped" >>${LOG_FILE}
fi
}
#
# Verify HTTP connection
#
httpVerification()
{
echo "Verify HTTP connectivity" >>${LOG_FILE}
case "$(curl -s --max-time 2 -I $VERIFY_DOMAIN | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in
[23]) echo "HTTP connectivity is up" >>${LOG_FILE} ;;
5)
echo "Proxy error" >>${LOG_FILE}
verificationFailure
;;
*)
echo "HTTP connectivity is down" >>${LOG_FILE}
verificationFailure
;;
esac
# exit 0
}
#
# Mount network shared from fstab
# if they are online
#
mountNetworkShares()
{
ISMOUNTED=$(df -k | grep $CUSTOM_MOUNTPOINT | wc -l)
if [ "$ISMOUNTED" -gt 0 ]; then
echo "$CUSTOM_MOUNTPOINT has been mounted." >>${LOG_FILE}
else
echo "$CUSTOM_MOUNTPOINT is not mounted. Attempting to mount." >>${LOG_FILE}
ping "$NETWORK_SHARE" -c 4
if [ $? -eq 0 ]; then
echo "$NETWORK_SHARE is available." >>${LOG_FILE}
# mount -a will mount drives from /etc/fstab
/bin/mount -a
else
echo "$NETWORK_SHARE is unreachable." >>${LOG_FILE}
#exit 1
fi
#exit 1
fi
}
#
# Clean up log
#
logCleanup()
{
size="$(wc -c <"$LOG_FILE")"
if [ "$size" -gt $LOG_SIZE ]; then
cat /dev/null >$LOG_FILE
fi
}
#
# Unrecoverable issue detected. Attempt reboot in 10 minutes.
# To prevent reboot loop, login within 10 minutes
# and run `/sbin/shutdown -c` to cancel it
#
catastrophicFailure()
{
#/bin/sleep 5m
/sbin/shutdown -r +10
}
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=" >>${LOG_FILE}
echo "Timestamp: $TIMESTAMP" >>${LOG_FILE}
echo "Pinging gateway $GATEWAY_CONFIGURED to verify LAN connectivity" >>${LOG_FILE}
if [ "$GATEWAY_CONFIGURED" = "" ]; then
echo "No gateway detected" >>${LOG_FILE}
catastrophicFailure
#exit 1
fi
if [ "$GATEWAY_CONFIGURED" != "$GATEWAY_EXPECTED" ]; then
echo "DHCP failed." >>${LOG_FILE}
catastrophicFailure
#exit 1
fi
ping "$GATEWAY_CONFIGURED" -c 4
if [ $? -eq 0 ]; then
echo "Gateway reachable. Proceeding with additional checks." >>${LOG_FILE}
mountNetworkShares
dnsVerification
internetAccessVerification
netcatScan
httpVerification
logCleanup
exit 0
else
echo "Gateway unreachable." >>${LOG_FILE}
catastrophicFailure
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment