如何在关闭电源之前记录净流量?

时间:2017-02-05 04:01:20

标签: iptables systemd

我想每次在debian8上记录净流量 这是我的方式。

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.target

sudo 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.log
2017-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)

/var/log/traffic.log中没有记录数据包编号 网络连接在执行/etc/init.d/K011trafficLog.sh之前关闭,以便在网络连接被检测之前执行它?

哪些服务在断电期间关闭网络连接?

systemctl |grep net - vim
(standard input):sys-devices-pci0000:00-0000:00:1c.2-0000:03:00.0-net-eth0.device                         loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
(standard input):sys-subsystem-net-devices-eth0.device                                                    loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
(standard input):netfilter-persistent.service                                                             loaded active exited    netfilter persistent configuration
(standard input):network-online.target                                                                    loaded active active    Network is Online
(standard input):network.target                                                                           loaded active active    Network
vim:sys-devices-pci0000:00-0000:00:1c.2-0000:03:00.0-net-eth0.device                         loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
vim:sys-subsystem-net-devices-eth0.device                                                    loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller
vim:netfilter-persistent.service                                                             loaded active exited    netfilter persistent configuration
vim:network-online.target                                                                    loaded active active    Network is Online
vim:network.target                                                                           loaded active active    Network

network.service是否在断电时关闭我的网络?

sudo vim /etc/systemd/system/graphical.target.wants/Ktraffic.service

[Unit]
Description=Record net traffic
Before=networking.service

[Service]
Type=oneshot
ExecStart=/bin/bash   /etc/init.d/K01trafficLog.sh

[Install]
WantedBy=poweroff.target

要重启以测试它,我的问题仍然存在 我在/ var / log / syslog中设置了一个标志。

echo  "it is a flag here to get output info of poweroff " | sudo tee -a  /var/log/syslog 

重新启动并获取syslog如下。

[![enter image description here][1]][1]

信息中哪些服务结果开始将硬件时钟同步到系统时钟

1 个答案:

答案 0 :(得分:0)

感谢FerencWágner。

solution to Execute simple script before shutdown and reboot

在我的eth0上记录网络流量是正确的。

[Unit]
Description=Record net traffic
Before=shutdown.target reboot.target halt.target poweroff.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/bash
ExecStop=/bin/bash   /etc/init.d/K01trafficLog.sh

[Install]
WantedBy=poweroff.target shutdown.target reboot.target halt.target