#!/bin/sh
# alpha-net-check - a network VERDICT about YOUR network, or about a
# server of YOURS that you name, with the reason in plain words.
# It looks only at the seat you are sitting in and the target YOU give.
# Usage: alpha-net-check [hostname-or-address-of-your-own-server]
TARGET="$1"
echo "NET CHECK - from this machine"
echo
if ! command -v ping >/dev/null 2>&1; then
  echo "VERDICT: CANNOT CHECK"
  echo "Reason: this shell has no ping tool, so nothing was tested."
  echo "Nothing was passed or failed - the tool simply is not here."
  exit 0
fi
# 1) The default gateway of THIS machine (your own router/hub).
GW=""
if command -v ip >/dev/null 2>&1; then
  GW=$(ip route 2>/dev/null | awk '/^default/{print $3; exit}')
fi
if [ -z "$GW" ] && [ -r /proc/net/route ]; then
  GW=$(awk '$2=="00000000"{print $3}' /proc/net/route 2>/dev/null | head -1)
fi
GW_OK="skip"
if [ -n "$GW" ]; then
  if ping -c 1 -W 2 "$GW" >/dev/null 2>&1; then
    echo "Your gateway ($GW): ANSWERS - your local network is alive."
    GW_OK="yes"
  else
    echo "Your gateway ($GW): NO ANSWER - your local network looks down."
    GW_OK="no"
  fi
else
  echo "Your gateway: could not be identified in this shell - skipped."
fi
echo
# 2) Optional: a server/box of YOURS that you name.
TGT_OK="skip"
if [ -n "$TARGET" ]; then
  if ping -c 1 -W 3 "$TARGET" >/dev/null 2>&1; then
    echo "Your server ($TARGET): ANSWERS - it is up and reachable from here."
    TGT_OK="yes"
  else
    echo "Your server ($TARGET): NO ANSWER from here."
    echo "That can mean it is off, asleep, or a firewall is not"
    echo "answering pings - it does not prove the machine is dead."
    TGT_OK="no"
  fi
  echo
fi
# Verdict, plain words.
if [ "$GW_OK" = "no" ]; then
  echo "VERDICT: WARNING"
  echo "Reason: your own gateway did not answer. Start with your"
  echo "router/hub: is it on, and is this machine connected to it?"
elif [ "$TGT_OK" = "no" ]; then
  echo "VERDICT: CAUTIOUS"
  echo "Reason: your local network answers, but your named server"
  echo "did not. Check that machine itself next."
elif [ "$GW_OK" = "yes" ] || [ "$TGT_OK" = "yes" ]; then
  echo "VERDICT: GOOD"
  echo "Reason: everything this check could reach, answered."
else
  echo "VERDICT: CAUTIOUS"
  echo "Reason: nothing could be confirmed either way from this seat."
fi
