shell脚本 监控网络流量
![shell脚本 监控网络流量](https://cdn.mongona.com/upload/image/article/2019/01/深度截图_选择区域_20190131205129.png?imageView2/0/q/75|watermark/2/text/bW9uZ29uYS5jb20=/font/5b6u6L2v6ZuF6buR/fontsize/280/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10|imageslim)
实时监控网络出入流量
#!/bin/bash
if [ -n "${1}" ];
then
NetDevice=${1};
else
NetDevice=eth0;
fi
printf "|------ Time ------|---- NetIn ----|--- NetOUt ----|\n"
while true
do time=$(date +%F" "%T)
rx_before=$(ifconfig ${NetDevice}|awk -F' *|:' '{if($2=="RX"&&$3=="bytes") print $4}')
tx_before=$(ifconfig ${NetDevice}|awk -F' *|:' '{if($2=="RX"&&$3=="bytes") print $9}')
sleep 1
rx_after=$(ifconfig ${NetDevice}|awk -F' *|:' '{if($2=="RX"&&$3=="bytes") print $4}')
tx_after=$(ifconfig ${NetDevice}|awk -F' *|:' '{if($2=="RX"&&$3=="bytes") print $9}')
rx_result=$(((rx_after-rx_before)/1024))
tx_result=$(((tx_after-tx_before)/1024))
printf "|%19s|%10s kbps|%10s kbps|\n" "${time}" "${rx_result}" "${tx_result}"
# sleep 1
done