我有一台 Ubuntu 机器和一台 Debian 机器。
在两者上,我都希望能够看到网络接口连接了多长时间。(也就是说,连接到网络获取 IP 等。不是电缆的物理状态)。以秒为单位的正常运行时间或自上次更改以来的日期 + 时间或任何类似内容。
到目前为止,我已经编写了一个小脚本来完成这项任务,但似乎应该有一种更通用的方法来检查这一点。/proc 中的程序或其他内容。
我的脚本:
#!/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 …Run Code Online (Sandbox Code Playgroud)