I have an Ubuntu machine and a Debian machine.
On both I want to be able to see for how long an network interface has been connected. (That is, connected to a network getting an IP etc. Not the physical state of a cabel). Uptime in seconds or date + time since last change or anything similar.
As of now I have written a little script to do the task but it seems there should be a more general way to check this. A program or something in /proc or such.
#!/bin/bash if [ -f /etc/os-release ]; then if TMP=$(grep -i 'ubuntu' /etc/os-release); then # we are on ubuntu for i in $(/bin/ls -1 /var/log/syslog* | sort -r); do TMP=$(zgrep '(eth0): device state change: ip-config -> activated' "$i" | tail -1 | sed "s/ "$(hostname)"/*/") done WHEN=$(echo "$TMP" | cut -f1 -d '*') SEC=$(echo "$(date +%s) - $(date -d "$WHEN" +%s)" | bc) echo "Last link up: $WHEN ($SEC seconds ago)." elif TMP=$(grep -i 'debian' /etc/os-release); then # we are on debian TMP=$(grep 'eth0: link up' /var/log/syslog* | tail -1 | cut -f2- -d':' | sed "s/ "$(uname -n)" kernel:/*/") WHEN=$(echo "$TMP" | cut -f1 -d '*') SEC=$(echo "$(date +%s) - $(date -d "$WHEN" +%s)" | bc) echo "Last link up: $WHEN ($SEC seconds ago)." fi else echo "File /etc/os-release not found." fi