我想从每个网络适配器获取 RX 和 TX,因此要从一个网络适配器获取 RX 和 TX,我使用了以下命令:
cat /proc/net/dev | grep 'wlan0' |awk '{if ($2) print $2}' | awk -v var="$(date +"%Y-%m-%d %r")" '{print $1"&",var";"}' >> /opt/baran/log/baran_RX_usage_$(date +"%Y-%m-%d");
cat /proc/net/dev | grep 'wlan0' |awk '{if ($10) print $10}' | awk -v var="$(date +"%Y-%m-%d %r")" '{print $1"&",var";"}' >> /opt/baran/log/baran_TX_usage_$(date +"%Y-%m-%d");
如您所见,我将结果放入带有日期(每日)的文件中。我希望在 while 循环中获取每个网络适配器而不仅仅是 wlan0,因此我们必须有一些日志文件,例如:
opt/baran/log/baran_TX_usage_wlan0_2014-01-01
opt/baran/log/baran_RX_usage_wlan0_2014-01-01
opt/baran/log/baran_TX_usage_eth0_2014-01-01
opt/baran/log/baran_RX_usage_eth0_2014-01-01
opt/baran/log/baran_TX_usage_eth1_2014-01-01
opt/baran/log/baran_RX_usage_eth1_2014-01-01
opt/baran/log/baran_TX_usage_eth2_2014-01-01
opt/baran/log/baran_RX_usage_eth2_2014-01-01
opt/baran/log/baran_TX_usage_eth3_2014-01-01
opt/baran/log/baran_RX_usage_eth3_2014-01-01
如您所见,我有一个无线适配器和 4 个 eth,并且我希望每天为它们每个生成日志文件。
谢谢
答案1
使用更简单的内核 API 怎么样?
for iface in /sys/class/net/*; do
# might want to filter out the 'lo' interface
read rx_bytes < "$iface"/statistics/rx_bytes
read tx_bytes < "$iface"/statistics/tx_bytes
day="$(date +"%Y-%m-%d")"
time="$(date +"%r")"
printf "%s& %s %s;" "$rx_bytes" "$day" "$time" >> /opt/baran/log/baran_RX_usage_"$day"
printf "%s& %s %s;" "$tx_bytes" "$day" "$time" >> /opt/baran/log/baran_TX_usage_"$day"
done