「shell」ifconfigでネットワークトラフィックを監視するサンプルコード

サンプルコード:
#!/bin/bash
# Link: www.sample.com
###
while :
do
Time=date +%F” “%T.%N
rx_before=ifconfig eth0 |sed -n 8p |awk ‘{print $2}'| cut -c7-
tx_before=ifconfig eth0 |sed -n 8p |awk ‘{print $6}'| cut -c7-
sleep 2
rx_after=ifconfig eth0 |sed -n 8p |awk ‘{print $2}'| cut -c7-
tx_after=ifconfig eth0 |sed -n 8p |awk ‘{print $6}'| cut -c7-

rx_result=$[(rx_after – rx_before)/512]
tx_result=$[(tx_after – tx_before)/512]
echo -e “$Time nNow_In_Speed: ‘$rx_result’Kbps Now_OUt_Speed: ‘$tx_result’Kbpsn”
done

Comments are closed.