如何正确统计流量?

sh/bash/dash/ksh/zsh等Shell脚本
回复
whaha
帖子: 104
注册时间: 2016-07-08 17:43
系统: debian8

如何正确统计流量?

#1

帖子 whaha » 2017-02-05 20:20

我想在关机前,将本次上网的流量统计数据写入一个log 文件。
iptables已经设置好。

sudo iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:http


sudo vim /etc/systemd/system/graphical.target.wants/Ktraffic.service
[Unit]Description=Record net traffic
[Service]Type=oneshot
ExecStart=/bin/bash /etc/init.d/K01trafficLog.sh
[Install]WantedBy=poweroff.targetsudo

vim /etc/init.d/K011trafficLog.sh
#!/bin/bash
trafficlog="/var/log/traffic.log
"echo `date "+%Y-%m-%d %H:%M:%S "` >> $trafficlog
iptables -v -L INPUT |grep Chain >> $trafficlog


systemctl enable ktraffic.service

启动系统测试一下。
sudo cat /var/log/traffic.log2017-02-05 10:49:31Chain INPUT (policy ACCEPT 0 packets, 0 bytes)2017-02-05 11:40:25Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

流量的值是0 。
trafficlog="/var/log/traffic.log
"echo `date "+%Y-%m-%d %H:%M:%S "` >> $trafficlog
iptables -v -L INPUT |grep Chain >> $trafficlog
这三条语句在root权限下,执行没有任何问题。

请问,怎么修改?
回复