Very often I login on machines with older mii-tool that reports 1Gbit/s cards as if they are connected on 100Mbit/s. This is normal as mii-tool is depricated. But a lot of old timers like me are used to its output. So I wrote a very simple awk script which can convert the ethtool output to the one liner output of mii-tool

if [ -z "$1" ]; then
    echo "Usage: mii-tool DEV"
    exit
fi
ethtool $1 2>&1|awk '
/Settings/{d=$3}
/Speed/{
    s=$2
    if (s !~ /Unknown/) {
        gsub(/Mb.*/,"",s)
        s=s"baseTx-"
    }
}
/Duplex/{
    if ($2 == "Full")
        s=s"FD"
    else
        s=s"HD"
}
/Link/{
    if ($3 == "yes")
        l="link ok"
    else
        l="no link"
}
/No data available/{err=1}
END{
    if (err) {
        gsub(/:/,"",d)
        print "SIOCGMIIPHY on \""d"\" failed: No such device"
        exit
    }
    if (s!~ /Unknown/)
        print d," "s", "l
    else
        print d, l
}'

And a shorter version for your .bashrc:
function mii-tool { ethtool $1|awk '/Settings/{d=$3}/Link/{if($3=="yes")l="link ok";else l="no link"}/Speed/{s=$2}/Duplex/{u=$2}END{if(s!~/Unknown/)print d," "s", "u", "l;else print d,l}'}


Comments are closed.

Posted by HackMan
Dated: 7th December 2015
Filled Under: Linux General, Technology