Tuesday, April 27, 2010

solaris nic speed script

#!/bin/sh

# $Id: speed_duplex,v 1.4 2007/07/10 16:04:42 hutch Exp $

PATH=/bin:/usr/bin:/usr/sbin

# Print column header information
echo "Interface\tSpeed\t\tDuplex"
echo "---------\t-----\t\t------"

# Determine the speed and duplex for each live NIC on the system
for INTERFACE in `netstat -i | egrep -v "^Name|^lo0" \
| awk '{ print $1 }' | cut -d: -f1 | sort | uniq`
do
# Only gather information for active interfaces
# Note: "ce" interfaces can be "UP" in "ifconfig" but have link down
ifconfig $INTERFACE | grep "^$INTERFACE:.* /dev/null 2>&1 || continue
# Skip "cip" ATM interfaces
echo $INTERFACE | grep "^cip" > /dev/null 2>&1 && continue
# "ce" interfaces
if [ "`echo $INTERFACE | awk '/^ce[0-9]+/ { print }'`" ] ; then
kstat > /dev/null 2>&1
if [ $? -ne 0 ] ; then
echo "The \"kstat\" command failed for interface $INTERFACE."
continue
fi
# Determine the ce interface number
INSTANCE=`echo $INTERFACE | cut -c 3-`
DUPLEX=`kstat ce:$INSTANCE | grep link_duplex | awk '{ print $2 }'`
case "$DUPLEX" in
0) DUPLEX="link down" ;;
1) DUPLEX="half" ;;
2) DUPLEX="full" ;;
esac
SPEED=`kstat ce:$INSTANCE | grep link_speed | awk '{ print $2 }'`
case "$SPEED" in
0) SPEED="link down" ;;
10) SPEED="10 Mbit/s" ;;
100) SPEED="100 Mbit/s" ;;
1000) SPEED="1 Gbit/s" ;;
esac
# "dmfe" interfaces
elif [ "`echo $INTERFACE | awk '/^dmfe[0-9]+/ { print }'`" ] ; then
# Only the root user should run "ndd"
if [ "`id | cut -c1-5`" != "uid=0" ] ; then
echo "You must be the root user to determine \
${INTERFACE_TYPE}${INSTANCE} speed and duplex information."
continue
fi
DUPLEX=`ndd /dev/${INTERFACE} link_mode`
case "$DUPLEX" in
0) DUPLEX="half" ;;
1) DUPLEX="full" ;;
esac
SPEED=`ndd /dev/${INTERFACE} link_speed`
case "$SPEED" in
10) SPEED="10 Mbit/s" ;;
100) SPEED="100 Mbit/s" ;;
1000) SPEED="1 Gbit/s" ;;
esac
# "bge" and "iprb" interfaces
elif [ "`echo $INTERFACE | awk '/^iprb[0-9]+|bge[0-9]+/ { print }'`" ] ; then
kstat > /dev/null 2>&1
if [ $? -ne 0 ] ; then
DUPLEX="The \"kstat\" command failed for interface $INTERFACE."
continue
fi
# Determine the bge|iprb interface number
INSTANCE=`echo $INTERFACE | tr -d '[a-z]'`
INTERFACE=`echo $INTERFACE | tr -d '[0-9]'`
DUPLEX=`kstat $INTERFACE:$INSTANCE | grep duplex | awk '{ print $2 }'`
SPEED=`kstat $INTERFACE:$INSTANCE | grep ifspeed | awk '{ print $2 }'`
case "$SPEED" in
10000000) SPEED="10 Mbit/s" ;;
100000000) SPEED="100 Mbit/s" ;;
1000000000) SPEED="1 Gbit/s" ;;
esac
elif [ "`echo $INTERFACE | awk '/^e1000g[0-9]+/ { print }'`" ] ; then
INSTANCE=`echo $INTERFACE | cut -c7-`
# The duplex for e1000g devices can only be found with "dladm"
DUPLEX=`dladm show-dev $INTERFACE | awk '{ print $NF }'`
SPEED=`kstat e1000g:$INSTANCE | grep ifspeed | awk '{ print $2 }'`
case "$SPEED" in
10000000) SPEED="10 Mbit/s" ;;
100000000) SPEED="100 Mbit/s" ;;
1000000000) SPEED="1 Gbit/s" ;;
esac
# le interfaces are always 10 Mbit half-duplex
elif [ "`echo $INTERFACE | awk '/^le[0-9]+/ { print }'`" ] ; then
DUPLEX="half"
SPEED="10 Mbit/s"
# All other interfaces
else
INTERFACE_TYPE=`echo $INTERFACE | sed -e "s/[0-9]*$//"`
INSTANCE=`echo $INTERFACE | sed -e "s/^[a-z]*//"`
# Only the root user should run "ndd"
if [ "`id | cut -c1-5`" != "uid=0" ] ; then
echo "You must be the root user to determine \
${INTERFACE_TYPE}${INSTANCE} speed and duplex information."
continue
fi
ndd -set /dev/$INTERFACE_TYPE instance $INSTANCE
SPEED=`ndd -get /dev/$INTERFACE_TYPE link_speed`
case "$SPEED" in
0) SPEED="10 Mbit/s" ;;
1) SPEED="100 Mbit/s" ;;
1000) SPEED="1 Gbit/s" ;;
esac
DUPLEX=`ndd -get /dev/$INTERFACE_TYPE link_mode`
case "$DUPLEX" in
0) DUPLEX="half" ;;
1) DUPLEX="full" ;;
*) DUPLEX="" ;;
esac
fi
echo "$INTERFACE\t\t$SPEED\t$DUPLEX"
done

No comments: