一个监控网卡流量的shell脚本
程序员文章站
2022-04-25 15:13:04
实现代码:
#!/bin/bash
eth_in_old=$(ifconfig eth0|grep "rx bytes"|sed 's/rx bytes:/...
实现代码:
#!/bin/bash eth_in_old=$(ifconfig eth0|grep "rx bytes"|sed 's/rx bytes://'|awk '{print $1}') eth_out_old=$(ifconfig eth0|grep "rx bytes"|sed 's/.*tx bytes://'|awk '{print $1}') sleep 1 eth_in_new=$(ifconfig eth0|grep "rx bytes"|sed 's/rx bytes://'|awk '{print $1}') eth_out_new=$(ifconfig eth0|grep "rx bytes"|sed 's/.*tx bytes://'|awk '{print $1}') eth_in=$(echo "scale=2;($eth_in_new - $eth_in_old)/1000.0"|bc) eth_out=$(echo "scale=2;($eth_out_new - $eth_out_old)/1000" | bc) echo "in: $eth_in kb" echo "out:$eth_out kb"
输出:
[root@localhost hbshell]# ./traffic.sh
in: 1.74 kb
out:1.17 kb