#! /bin/ksh
# netgrep version 0.0.6 April 4, 2004
# shell script to analyse potential NIC / network problems.
# not clever or efficient, but quick, dirty and effective.
# Sam Nelson <sam.nelson@sun.com> 
# I've moved companies. All updates will be available at http://www.unix.ms
# cheers, sam@unix.ms

TMPFILE=netgrep.txt
NDD=/usr/sbin/ndd
NETSTAT=/usr/bin/netstat
IFC=/sbin/ifconfig

typeset -R5 LINK
typeset -R9 AUTOSPEED
typeset -R6 STATUS
typeset -R6 SPEED
typeset -R5 MODE
typeset -R18 ETHER

echo "Running netgrep v0.0.6, Sam Nelson <sam@unix.ms>.."
echo "netgrep v0.0.6 output - for latest version see http://www.unix.ms" >$TMPFILE 2>&1
echo "netgrep diagnostic script " >> $TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "ifconfig output:" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/sbin/ifconfig -a >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

# Get basic TCP / UDP info
echo "netstat -s -P tcp" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
netstat -s -P tcp >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "netstat -s -P udp" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
netstat -s -P udp >>$TMPFILE 2>&1

# Check the speed and settings of interfaces.
# Thanks and regards to Paul Bates and James Council
# This section http://www.sun.com/bigadmin/scripts/submittedScripts/nicstatus.txt

echo "Interface Settings:"  >> $TMPFILE

#---- Function to test a Cassini Gigabit-Ethernet port (i.e. ce_).
#
Check_CE()
{
autospeed=`${NETSTAT} -k $LINK| awk '/xcvr_id/ {print $4}'`
case $autospeed in
   1) AUTOSPEED=on	;;
   0) AUTOSPEED=off	;;
   *) AUTOSPEED=Error	;;
esac

status=`${NETSTAT} -k $LINK| awk '/link_up/ {print $2}'`
case $status in
   1) STATUS=Up		;; 
   0) STATUS=DOWN	;;
   *) STATUS=Error	;;
esac

SPEED=`${NETSTAT} -k $LINK| grep link_speed| awk '{print $2}'`

mode=`${NETSTAT} -k $LINK| grep link_speed| awk '{print $4}'`
case $mode in
   2) MODE=FDX		;;
   1) MODE=HDX		;;
   0) MODE=---		;;
   *) MODE=Error	;;
esac
}

# Function to test Quad Fast-Ethernet, Fast-Ethernet, and
# GEM Gigabit-Ethernet (i.e. qfe_, hme_, ge_)
#
Check_NIC()
{
${NDD} -set /dev/${1} instance ${2}

if [ $type = "ge" ];then
	autospeed=`${NDD} -get /dev/${1} adv_1000autoneg_cap`
else	autospeed=`${NDD} -get /dev/${1} adv_autoneg_cap`
fi
case $autospeed in
   1) AUTOSPEED=on	;;
   0) AUTOSPEED=off	;;
   *) AUTOSPEED=Error	;;
esac

status=`${NDD} -get /dev/${1} link_status`
case $status in
   1) STATUS=Up		;;
   0) STATUS=DOWN	;;
   *) STATUS=Error	;;
esac

speed=`${NDD} -get /dev/${1} link_speed`
case $speed in
   1) SPEED=100MB	;;
   0) SPEED=10MB	;;
   *) SPEED=Error	;;
esac

mode=`${NDD} -get /dev/${1} link_mode`
case $mode in
1) MODE=FDX		;;
   0) MODE=HDX		;;
   *) MODE=Error	;;
esac
}


# Output header.
echo
echo " NIC Autospeed Status  Speed  Mode   MAC Address" >>$TMPFILE 2>&1
echo "----- --------- ------ ------ ----- ------------------">>$TMPFILE 2>&1

# make a list of active i/f.
# i.e multi qfe!
#
for LINK in `${IFC} -a| egrep -v "lo|be|dman|lpfc"| egrep "^[a-z,A-z]"| cut -f1 -d :| sort| uniq`
do
 type=$(echo $LINK | sed 's/[0-9]//g')
 num=$(echo $LINK | sed 's/[a-z,A-Z]//g')

# Set variables / reference functions.
#
 if [ $type = ce ]; then
	Check_CE $type $num
 else	Check_NIC $type $num
 fi
# Set ethernet variable  / dump it all.
#
 ETHER=`${IFC} ${LINK}| awk '/ether/ {print $2}'`
 echo "$LINK $AUTOSPEED $STATUS $SPEED $MODE $ETHER" >>$TMPFILE 2>&1
done

set +x
echo "" >>$TMPFILE 2>&1

# rudimentary check for collisions / ierrors / oerrors.
echo "Check for ierrors/oerrors/collisions......" >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/netstat -i >>$TMPFILE 2>&1


#routing info
echo "Routing table info.">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/netstat -rvn >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#defaultrouter?
echo "checking for existence of /etc/defaultrouter">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/ls -al /etc/defaultrouter>>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "Checking contents of /etc/defaultrouter....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/cat /etc/defaultrouter>>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#check nsswitch.conf
echo "Checking permissions of /etc/nsswitch.conf....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/ls -al /etc/nsswitch.conf >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "Checking contents of /etc/nsswitch.conf....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/cat /etc/nsswitch.conf >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#check resolv.conf
echo "Checking permissions of /etc/resolv.conf....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/ls -al /etc/resolv.conf >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "Checking contents of /etc/resolv.conf....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/cat /etc/resolv.conf >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#check inetd.conf
echo "Checking permissions of /etc/inetd.conf....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/ls -al /etc/inetd.conf >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "Checking contents of /etc/inetd.conf....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/cat /etc/inetd.conf >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#check hosts file
echo "Checking permissions of /etc/inet/hosts....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/ls -al /etc/inet/hosts >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "Checking contents of /etc/inet/hosts....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/cat /etc/inet/hosts >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#check /etc/gateways if it exists
echo "Checking permissions of /etc/gateways....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/ls -al /etc/gateways >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "Checking contents of /etc/gateways....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/cat /etc/gateways >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#check nodename file
echo "Checking permissions of /etc/nodename....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/ls -al /etc/nodename >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "Checking contents of /etc/nodename....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/cat /etc/nodename >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#check hostname.*** file(s)
echo "Checking permissions of /etc/hostname.*....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/ls -al /etc/hostname* >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
echo "Checking contents of /etc/hostname.*....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/cat /etc/hostname*>>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#what's open / connected?
echo "Open connections...." >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/bin/netstat -an >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

# ARP you being served?
echo "ARP cache details">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/sbin/arp -a >>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1

#check other hosts in the vicinity
echo "Ping responses from this subnet....">>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
/usr/sbin/ping -sn 255.255.255.255 1 255>>$TMPFILE 2>&1
echo "" >>$TMPFILE 2>&1
chmod 777 $TMPFILE

echo "OK script finished" >>$TMPFILE 2>&1

echo Check current directory for netgrep.txt file!