#!/bin/bash
#
# net_check 0.1.9
#
# Copyright (c) 2006,2008 Sergio Perticone <g4ll0ws@gmail.com>
# All rights reserved.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
## wireless tools and route(8) stay in sbin directories:
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin
## setting dummy variables:
C_RED="\e[31;1m"
C_GREEN="\e[32;1m"
C_YELLOW="\e[33;1m"
C_DEFAULT="\e[0m"
ALIGN="\t\t\t\t"
_putmsg() { ## _putmsg <color> <message> <exit status>
printf "\r${ALIGN}[ $1%s${C_DEFAULT} ]" "$2"
if test ! -z "$3"; then
printf "\n"
exit $3
fi
}
function _errx() { # _errx <exit status> <message>
printf "%s: %s" `basename $0` $1
if test x"$3" != x; then
printf "\n"
exit $3
fi
}
## interface to use (btw you can change it by -I flag):
IF="`route -n | grep -E '[ae]th|wlan' | head -1 | awk '{print $8}'`"
if test -z "$IF"; then
_errx 1 "No interface found: check your network settings."
fi
## by default, for internet checking, net_check uses google.com:
IP_WORLD=64.233.167.99
URL_WORLD=google.com
## primary and secondary dns:
DNS1="`cat /etc/resolv.conf | grep nameserver | grep -Ev ^# | \
awk '{print $2}' | head -1`"
DNS2="`cat /etc/resolv.conf | grep nameserver | grep -Ev ^# | \
awk '{print $2}' | head -2 | tail -1`"
## parser:
case $1 in
'--events'|'-e')
if ! type iwevent &> /dev/null; then
_errx 1 \
"You need Wireless-Tools installed on your system to do this"
fi
iwevent
exit 0
;;
'--scan'|'-S')
iwlist $IF scanning
exit $?
;;
'--state'|'-s')
if ! type watch &> /dev/null; then
echo "cat /proc/net/wireless" ## useless and ugly
exit $?
fi
watch -t -d -n 0.5 cat /proc/net/wireless ## useless but cool
exit 0
;;
'--help'|'-h')
echo ""
echo " -e, --events show wireless events"
echo " -S, --scan scanning"
echo " -s, --state show wireless status"
echo ""
echo " -g ip set gateway (only root)"
echo " -i ip set ip (only root)"
echo " -I if set interface to probe"
echo ""
echo " -n skip beacon test"
echo ""
echo " -h, --help what do you think you're reading?"
echo
exit 0
;;
esac
while getopts "g:I:i:n" option
do
case $option in
g)
if test x"$UID" = x"0"; then
if ! test -z "$GW"; then
route del default gw $GW $IF
fi
route add default gw $OPTARG
GW=$OPTARG
else
_errx 1 "Only root can do this."
fi
;;
I)
IF=$OPTARG
;;
i) if test x"$UID" = x"0"; then
ifconfig $FI $OPTARG up
IP_LAN=$OPTARG
else
_errx 1 "Only root can do this."
fi
;;
n) WIRELESS=0
;;
*)
_errx 1 "Try \`$0 --help' for more information."
;;
esac
done
if test -z "$IF"; then
_errx 1 "No network interface specified or detected."
fi
## other variables:
IP_LAN="`/sbin/ifconfig $IF 2> /dev/null | grep inet\ addr |\
cut -d : -f2 | awk '{print $1}'`"
if test -z "$GW"; then
GW="`route -n | grep $IF | grep G | head -1 | tail -1 | awk '{print $2}'`"
fi
if test -z "$GW"; then
_errx 1 "No gateway specified for $IF"
fi
if test -z "$IP_LAN"; then
_errx 1 "No IP detected for $IF."
fi
echo
printf " ${C_GREEN}*${C_DEFAULT} Interface..\r${ALIGN}$IF\n"
printf " ${C_GREEN}*${C_DEFAULT} Ip..\r${ALIGN}$IP_LAN\n"
printf " ${C_GREEN}*${C_DEFAULT} Gateway..\r${ALIGN}$GW\n"
printf " ${C_GREEN}*${C_DEFAULT} 1st DNS Server..\r${ALIGN}$DNS1"
if test -z "$DNS1"; then
printf "\r ${C_GREEN}*${C_DEFAULT} DNS Server.. \r${ALIGN}n/a\n\n"
elif test "$DNS2" != "$DNS1"; then
printf "\n ${C_GREEN}*${C_DEFAULT} 2nd DNS Server..\r${ALIGN}$DNS2\n\n"
else
printf "\r ${C_GREEN}*${C_DEFAULT} DNS Server.. \n\n"
fi
##########
# BEACON #
##########
if test ! "$WIRELESS"; then
if ! type iwconfig &> /dev/null; then
_errx 1 \
"You need Wireless-Tools installed on your system to do this" \
"Otherwise try \`net_check -n'"
fi
_putmsg ${C_YELLOW} wait
printf "\r ${C_GREEN}*${C_DEFAULT} Checking beacon.."
IWCONFIG=`iwconfig $IF 2> /dev/null`
if test -z "$IWCONFIG" ; then
_putmsg ${C_YELLOW} 'none'
echo " $IF: no wireless extensions."
elif iwconfig $IF | grep -q unassociated; then
_putmsg ${C_RED} fail 2 ## :(
else
_putmsg "${C_GREEN}" ' OK '
fi
echo
fi
###########
# GATEWAY #
###########
_putmsg ${C_YELLOW} wait
printf "\r ${C_GREEN}*${C_DEFAULT} Checking gateway.."
if ping -c1 -w2 $GW &> /dev/null; then
_putmsg ${C_GREEN} ' OK '
else
_putmsg ${C_RED} fail 3
fi
echo
############
# INTERNET #
############
_putmsg ${C_YELLOW} wait
echo -ne "\r ${C_GREEN}*${C_DEFAULT} Checking internet.."
if ping -c2 -w3 $IP_WORLD &> /dev/null; then
_putmsg ${C_GREEN} ' OK '
else
_putmsg ${C_RED} fail 4
fi
echo
#######
# DNS #
#######
_putmsg ${C_YELLOW} wait
printf "\r ${C_GREEN}*${C_DEFAULT} Checking DNS.."
if ping -c1 -w3 $URL_WORLD &> /dev/null; then
_putmsg "${C_GREEN}" ' OK '
else
_putmsg ${C_RED} fail 5
fi
##########
# IP-WAN #
##########
if type curl &> /dev/null; then
printf "\n\n ${C_GREEN}*${C_DEFAULT} Public Ip.."
IP_WAN=`curl -m 2 whatismyip.org 2> /dev/null`
if test -z "$IP_WAN"; then
printf "\r${ALIGN}n/a\n"
else
printf "\r${ALIGN}${IP_WAN}\n"
fi
fi
printf "\n\a"
## EOF