#!/bin/sh # Script to get and/or check the Ethernet speed. Useful to be run by cron. # Copyright (C) 2013-2020 Vincent Lefevre # # It needs the "ethtool" and "ip" utilities. # # Use the -q option to avoid the error message when no UP Ethernet devices # are found and messages for devices with no Ethernet cable. Useful if the # machine is not always connected by Ethernet, to avoid spurious messages. # # You can provide an optional argument, such as 1000Mb/s (expected speed). # If the speed has this value, nothing will be output. This is useful to # check by cron that the Ethernet speed hasn't been reduced (e.g. because # of dust or a bad cable). # # All UP Ethernet devices (whose name matching the regexp in the code below) # are checked. # # Note: Monitoring tools can do a better job, noticing changes immediately. # But this utility is simpler to use and may still be useful. It should # also be less sensitive to temporary issues. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see http://www.gnu.org/licenses/ # or write to the Free Software Foundation, Inc., 51 Franklin St, # Fifth Floor, Boston, MA 02110-1301, USA. unset quiet if [ "x$1" = "x-q" ]; then quiet=1 shift fi if [ $# -gt 1 ]; then echo "Usage: $0 [ -q ] [ ]" >&2 exit 1 fi # Get the Ethernet devices that are up. # Historical names start with "eth". # New systemd/udev-based names start with "en": # https://www.freedesktop.org/software/systemd/man/systemd.net-naming-scheme.html devices=$(ip link | \ sed -n 's/.* \(\(en\|eth\)[-0-9a-z]*\):.*[,<]UP[,>].*/\1/p') if [ -z "$devices" ]; then [ -z "$quiet" ] || exit 0 echo "$0: can't find an Ethernet device" >&2 exit 2 fi et() { # In not run as root, let's hide the ethtool spurious warnings # by redirecting stderr to /dev/null; but this will also hide # any error message (the user would then run ethtool manually). if [ "$(id -u)" -eq 0 ]; then ethtool $1 else ethtool $1 2> /dev/null fi } for dev in $devices do speed=$(et $dev | sed -n 's/^[[:space:]]*Speed:[[:space:]]*//p s/^[[:space:]]*Link detected: no/NOLINK/p') case $speed in *NOLINK*) # This happens when the machine boots without an Ethernet cable and # netplug is used, because its probe rule sets all the eth* devices # up by default (see netplugd(8) man page). This can also happen on # machines with multiple Ethernet interfaces, but where one of them # is not used (I'm wondering why such an interface is up, though). [ -n "$quiet" ] || echo "$0: no link on $dev" ;; *) if [ -z "$speed" ]; then echo "$0: can't get $dev speed" elif [ "x$speed" != "x$1" ]; then echo "$0: $dev speed is $speed" fi ;; esac done # Exit with no errors (any output will be shown by cron). exit 0 # $Id: check-eth-speed 127096 2020-04-29 15:06:28Z vinc17/zira $