How to record net traffic before net closed when to poweroff? - iptables

I want to record net traffic everytime on debian8.
Here is my way.
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
To check the traffic log file when to reboot.
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)
no packets number recorded in /var/log/traffic.log.
Network connection was closed before execution of /etc/init.d/K011trafficLog.sh,how to make it execute before network connection colsed?
Which service closed network connection during poweroff?
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
Is the network.service to close my network when poweroff?
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
To reboot to test it, my issue remains.
I set a flag in /var/log/syslog.
echo "it is a flag here to get output info of poweroff " | sudo tee -a /var/log/syslog
To reboot and get syslog as following.
[![enter image description here][1]][1]
Which service result in info Starting Synchronise Hardware Clock to System Clock?

Thank to Ferenc Wágner .
solution to Execute simple script before shutdown and reboot
It is right fromat to record network traffic on my 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

Related

Unable to set correctly a firewall in mininet with sdn and opeflow ovs (UDP Version )

I'm experimenting with mininet in ubuntu 14 in order to create a basic firewall which blocks the udp packets from one host ( h1= 10.0.0.1 ) to another ( h4= 10.0.0.4 ).
Those hosts are in the same vlan and in different switchs (if that can be of any help). Also I would like to block it the udp packets which the destination port as 5001.
To do it so, i have launch two xterm in h1(in mininet) in order to check the ping is working correctly and also launch the packets to h4. xterm h1: "iperf -u -c10.0.0.4 -p 5001 -i 5 -b 200K -t 360".
In mininet I also have open a xterm h4 to set it up as a server listening in the port 5001. xterm h4: "iperf -s -u -p 5001 -i 5​".
When I guess the rule I have to introduce is this one "sh ovs-ofctl add-flow s1 udp_dst=5001,nw_proto=17,actions=drop"
But, it doesnt work due to the packets are still arriving. The ping works fine, but ( and here comes the main problem) the packet arrives at the server and it shouldn't.
Any help please?
Thank you very much
Here I leave you the screenshots of the network topology and also what I appear in the xterm windows.

Force docker-machine to specific IP using Hyper-V, network unreachable

I have found a partial answer to this question, and it is successfully setting the machine at the desired IP address. But the network is unreachable from inside a docker-machine created with the Hyper-V driver.
The TLDR on the answer above is to create a script, /var/lib/boot2docker/bootsync.sh:
sudo cat /var/run/udhcpc.eth0.pid | xargs sudo kill
sudo ifconfig eth0 192.168.XXX.YYY netmask 255.255.255.0 broadcast 192.168.XXX.255 up
Once I make the script, I restart the machine.
When I restart the machine, the IP is set to my desired address (expected). I can remote in at the address, so it is at least available through the host. But when I test for connections, there is no connection to the internet (unexpected).
Boot2Docker version 17.05.0-ce, build HEAD : 5ed2840 - Fri May 5 21:04:09 UTC 2017
Docker version 17.05.0-ce, build 89658be
docker#machine:~$ docker pull ubuntu
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on [::1]:53: read udp [::1]:48331->[::1]:53: read: connection refused
docker#machine:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
ping: sendto: Network is unreachable
If I remove the script and restart again, I am reassigned a new/random IP address (expected), remote in at that new IP address, and can do network connections (expected):
docker#pm:~$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
aafe6b5e13de: Pull complete
0a2b43a72660: Pull complete
18bdd1e546d2: Pull complete
8198342c3e05: Pull complete
f56970a44fd4: Pull complete
Digest: sha256:f3a61450ae43896c4332bda5e78b453f4a93179045f20c8181043b26b5e79028
Status: Downloaded newer image for ubuntu:latest
docker#pm:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=43 time=18.424 ms
64 bytes from 8.8.8.8: seq=1 ttl=43 time=27.638 ms
The accepted answer has several up votes, but it reads like this is a confirmed work around on VirtualBox. Not sure what about Hyper-V would be causing the IP assignment to cut off internet access.
I had the same problem, and I solved it by adding the following to the end of bootsync.sh:
route add default gw <address>
There was no default route to the gateway or the internet, so it must be set manually.

UbuntuServer 16.04 VirtualBox Machine - Emergency Mode for Network Interfaces

I'm using a virtual machine with Ubuntu16.04Server O.S. on a Windows host.
Everything was working perfectly but after switching off everything and then coming back to my virtual server, the machine doesn't start.
It shows:
[FAILED] Failed to start Raise network interfaces. See 'systemctl status networking.service' for details.
So the system goes in Emergency mode!
By typing journalctl -xb, I can see the following issue:
...
ifup[1987]: Cannot find device "ens33"
...
ifup[1987]: Failed to bring up ens33.
systemd[1]: netowrking.service: Main process exited, code=exited ....
So I find my failed to start Raise network interfaces
Subject: Unit networking.service has failed
Defined-By: systemd
By typing ifconfig I see only the lo interface. No ens33 is shown.
In my /etc/network/interfaces I have also the 2 rows:
auto ens33
iface ens33 inet dhcp
By typing lshw I have the following output related to the network:
*-network DISABLED
description: Ethernet interfaces
product: 82540EM Gigabit Ethernet Controller
vendor: Intel Corporation
physical id: 3
bus info: pci#0000:00:03.0
logical name: enp0s3
...
...
configuration: autonegotiation=on broadcast=yes driver=e1000 driverversion=7.3.21-k8-NAPI duplex=full.......
So if I run ifup ens33 I have the error
Cannot find device ens33
Error getting hardware address for "ens33": No such device
The virtual machine has the default ethernet card Intel PRO/1000 MT Desktop (82540EM) connected as NAT.
It is completly stuck and I can't go forward, only emergency mode.
Finally found a solution:
The issue should seems to be related to a mismatch of the ethernet card, and my solution was:
changing card on the virtual machine
entering in emergency mode
Editing the /etc/network/interfaces file, by changing the ens33 interface parameters and adding the new eth0 interface:
auto ens33
iface ens33 inet manual
auto eth0
iface eth' inet dhcp
At this point by restarting the network service I could see the interface up.
After it was necessary the editing of the /etc/default/grub file, changing the line from
GRUB_CMDLINE_LINUX=""
to
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"
Then I ran following commands:
sudo update-grub
sudo reboot

Raspberry Pi 2 GPSD Not Running

I am following this tutorial to hook up Adafruit's Ultimate GPS Breakout to my Raspberry Pi 2. I have carefully read each step.
When I run gpsmon /dev/ttyUSB0, it displays expected output with my location, speed, etc. When I run cgps -s, however, I get this message cgps: no gpsd running or network error: -4, can't create socket.
The really weird thing is that the GPSD seems to be failing. Here is me trying to start it:
pi#raspberrypi:~$ sudo service gpsd status
[FAIL] gpsd is not running ... failed!
pi#raspberrypi:~$ sudo service gpsd restart
[ ok ] Restarting GPS (Global Positioning System) daemon: gpsd.
pi#raspberrypi:~$ sudo service gpsd status
[FAIL] gpsd is not running ... failed!
pi#raspberrypi:~$ sudo service gpsd start
pi#raspberrypi:~$ sudo service gpsd status
[FAIL] gpsd is not running ... failed!
So, it is apparent that cgps is failing because GPSD is not starter. Any suggestions to get GPSD working?
EDIT
When I run sudo gpsd -N -D3 -F /var/run/gpsd.sock, I get this output:
gpsd:INFO: launching (Version 3.6)
gpsd:ERROR: can't bind to IPv4 port gpsd, Cannot assign requested address
gpsd:ERROR: can't create IPv6 socket
gpsd:DATA: command sockets creation failed, netlib errors -1, -1
Late answer. However I just bumped into this problem and found that my /etc/network/interfaces didn't exist. Thusly there was no loopback interface present. If interfaces doesn't exist, try creating it by running sudo nano /etc/network/interfaces and add:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
(Or as it suits your setup.)
Then run ifup -a and check if there's a new interface by running ifconfig. It should be somewhat like:
eth0 Link encap:Ethernet HWaddr b8:27:eb:16:bc:52
[...]
lo Link encap:Local Loopback
[...]
Hope it works for others as well.
For unrelated reasons, I reimaged my Pi, and the gps module is now working as expected. I don't know what was wrong last time, but it is working now.

GPSD not getting a good fix

I have a GPS module connected to a Raspberry Pi via USB.
For some reason I can't seem to get a fix using:
cgps
it doesn't seem to get a fix and terminates.
I've also tried:
sudo killall gpsd
sudo gpsd /dev/ttyUSB0 -F /var/run/gpsd.sock
and then tried cgps again, but that doesn't seem to work either.
I even tried:
sudo nano /etc/default/gpsd
and changed the line:
GPSD_OPTIONS=""
to:
GPSD_OPTIONS="/dev/ttyACM0"
which apparently worked for others around the web, but after a reboot, that didn't work either.
But for some reason when I do:
gpsmon /dev/ttyUSB0
I get a full readout of data; lat, lon, Sats, altitude, ext.
Is there a way to output gpsmon /dev/ttyUSB0 sentences to a text file?
Also, why can't I obtain a fix using cgps or something similar?
gpsd is a beautiful application to simplify gps use, but it can be a little confusing.
If you're using a Rasbian, or some apt based package system it is best to configure it with sudo dpkg-reconfigure gpsd to avoid complications (tyqos), but isn't necessary. We have preferences for, but you may not,
autostart,
-n Don't wait for a client
-G to listen on all addresses,
-b Broken-device-safety mode, and
autofind
The resultant configuration file looks like
# Default settings for gpsd.
# Please do not edit this file directly - use `dpkg-reconfigure gpsd' to
# change the options.
START_DAEMON="true"
GPSD_OPTIONS="-n -G -b"
DEVICES=""
USBAUTO="true"
GPSD_SOCKET="/var/run/gpsd.sock"
The primary stumbling block with this approach while gpsd is running in this fashion is it will grab the gps before you can. Attempts to independently and directly access the device /dev/whatever will fail as busy.
If you wish to go that route, for whatever reason, before doing anything else, make sure gpsd is not running.
sudo killall gpsd
and remove any sockets gpsd might have left behind,
sudo rm /var/run/gpsd.sock
Check the location of your gps by attaching it and tracking where it went with dmesg | tail. It will look something like
[67338.935645] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[67338.935650] usb 1-1.2: Product: USB-Serial Controller
[67338.935653] usb 1-1.2: Manufacturer: Prolific Technology Inc.
[67338.936154] pl2303 1-1.2:1.0: pl2303 converter detected
[67338.937953] usb 1-1.2: pl2303 converter now attached to ttyUSB1
[67339.806917] pl2303 ttyUSB1: usb_serial_generic_read_bulk_callback - urb stopped: -32
[67339.807306] pl2303 ttyUSB1: usb_serial_generic_read_bulk_callback - urb stopped: -32
[67340.018016] pps_ldisc: PPS line discipline registered
[67340.018321] pps pps0: new PPS source usbserial1
[67340.018330] pps pps0: source "/dev/ttyUSB1" added
Then you can check for output with sudo cat /dev/ttyUSB1...or whatever, but you could do that with gpsd running. (You can also pump this into a text file sudo cat /dev/ttyUSB1 > gps_dump.txt, or your gpsmon /dev/ttyUSB0 >gps_dump.txt, but there are more elegant solutions.)
The flip side of the confusion is no gps output from the gpsd because it isn't running or configured 'properly'. (either turned off, not started, or pointing to the wrong device). A few application will tell you it's not running, many just sit in silence without any data. I know of none that will tell you gpsd has been manually set to the wrong device.
If you have killed gpsd, or do not have it automagically start, ensure that it is running with sudo /etc/init.d/gpsd restart
Unless you're doing something odd with odd hardware most cases will spit back data with gpsd in these settings, sparing the need for diagnostic settings.
However, bear in mind cgps will timeout if there is no fix. Check your skyview, and Time To First Fix. xgps, on the other hand, is more resilient for failures and provides clues for the absence, or quality of data. If you have an X server, xgps is actually my preferred test for "is it working". If you don't, but have your Pi on a network (xgps 192.168.0.6, or whatever, because the other machine has gpsd-client installed). Another option is to ssh -X user#192.168.0.6 and then execute xgps)
Telneting into gpsd, while interesting, is another tier diagnostic, as are others.
And finally, a shameless plug for a Python client for gpsd (gps3.py) as means to access the data from a gpsd. It still is alpha, but it doesn't import historical cruft.
You may be binding to the wrong serial. You discover the serial for your GPS device by navigating to the folder below without the device plugged in, then refreshing with the device plugged in.
cd /dev/
ls
My device is called "ttyACM0"
Install the requisite packages.
sudo apt-get install gpsd gpsd-clients python-gps
Stop Daemons
sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket
Expose Service to public ports and localhost
vim /lib/systemd/system/gpsd.socket
Change 127.0.0.1 to 0.0.0.0
Stop GPSD services
sudo killall gpsd
Bind service to serial device
sudo gpsd /dev/ttyACM0 -F /var/run/gpsd.sock
Show GPS Data
gpsmon
This video shows step by step how to do what you are looking for.
How to install GPS on RaspberryPi
https://www.youtube.com/watch?v=A1zmhxcUOxw