Detect if an ethernet port is unplugged - linux

I want to trap the event of unplugging an ethernet port on a Linux Ubuntu 14.04.
I want to create a script that detect whenever an ethernet port is unplugged and write it to a log.
Which is the best way to trap such an event

Just put an executable script inside /etc/network/if-post-down.d.
#!/bin/bash
set -e
if [[ "$IFACE" == "wlan0" ]]; then
logger "The wlan0 interface is down!"
# Do whatever you want here.
fi
Make sure to chmod +x it, also.
Read more about these events/scripts here on the Ubuntu Wiki.

Related

How to set up a udev rule for ETH "link down"/"link up"?

I like to switch on a green LED(connected through GPIOs), when eth0 is connected. When disconnected I like to switch the green LED of and a red one on.
Thought that udev is maybe the right place for it. I created the simple demo rule:
KERNEL=="eth0", SUBSYSTEM=="net", ACTION=="add", RUN+="/sbin/set_BSPleds eth0 on"
This rule should call a script when the eth0 is added. It was never executed.
After I was looking to the udev monitor by entering "udevadm monitor -k -u" at the shell. There were no events coming when I unplug/plug the lan cable.
root#sama5d3xek:/etc/udev/rules.d# udevadm monitor -k -uh0
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
root#sama5d3xek:/etc/udev/rules.d#
Seems that there are no uevents for eth0. The ethernet driver is provided my ATMEL. I am building a custom Linux by the help of Yocto.
My question is, how to get the "link down"/"link up" events to udev?
If it does not works with udev, what alternative way is the best?
As others have already mentioned, it seems one can't use udev for this.
Ubuntu: wait for network link up and execute a bash command suggests
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'
(Currently, / is on nfs for my box, so I can't really say if it will work.)
In other posts, there are some concerns about using the inotify API on /sys: inotify_add_watch fails on /sys/class/net/eth0/operstate .
I think the Right Way(TM) do do this would be to use the netlink(7) API, preferably through a daemon such as netplugd.
Hope that helps :)
Ethernet devices are devices, but connections are not.
You could trace connection through /dev/xconsole, dmesg or /var/log/kern.log.
Sample, using rsyslog:
You could (as root):
echo ':msg, contains, "Link is" |/run/netlink' >/etc/rsyslog.d/netlinkstate.conf
mkfifo /run/netlink
chgrp user /run/netlink
chmod 640 /run/netlink
service rsyslog force-reload
Then, logged as user, simply:
read line < /run/netlink
will wait for input from fifo, then hold until something happen.
state=${line#*Link is } eventtime=${line%% $HOSTNAME*}
echo $eventtime $state
2016-11-21T17:40:50.956181+01:00 Down
or
echo $eventtime $state
2016-11-21T17:40:50.956181+01:00 Up 100 Mbps Full Duplex, Flow Control: Rx/Tx
echo $eventtime ${state%% *}
2016-11-21T17:40:50.956181+01:00 Up
Under bash, you could use timeout for emptying fifo and read only last entry:
while read -t .01 entry </run/netlink;do line="$entry";done
state=${line#*Link is }
eventtime=${line%% $HOSTNAME*}
shortstate=${state%% *}
Nota: I've used /run to store fifo. This could be not the better place as this won't exist on next reboot.

Linux: execute a command when network connection is restored

I have a linux box connected to a router via an ethernet cable.
The cable is never unplugged and the linux box is always on.
My goal is to have a command executed on the linux box every time the router is rebooted.
This question is quite similar to this question but the suggested solution doesn't seem to work.
More specifically:
the command
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected' does never detect a change (even in the case the cable is unplugged from the pc), because according to this answer the command inotifywait does not work in /sys
the command (suggested in a comment) inotifywait -e modify /etc/network/if-up.d/avahi-daemon does not detect any change when the router is rebooted
You can start a script after the linux box gets connected using up
(requires ifplugd to be installed )
#/etc/network/interfaces
auto eth0
iface eth0 inet dhcp
up /etc/network/yourscript.sh
However, there keep in mind that if you disconnect the cable (and plug in in after a while), the script also starts even though the router might not have been restarted.
--edit--
alternatively , place your script in
/etc/network/if-up.d/
(make sure it is executable and restart networking after changes.)
Depending on the Linux distribution on that router the correct way of running a command at startup/reboot is to create a startup script, or add the command to /etc/rc.local

How to initialize Bluetooth in a startup script with Yocto Poky Linux

I have a script that initializes my bluetooth setup on an Intel Edison. It allows pairing and connecting to this headless machine running Yocto Poky Linux. It was suggested that I put the startup script in /etc/init.d and run update-rc.d myscript.sh defaults. The script ran but it didn't work (generated boot errors saying bluetooth device not found) because Bluetooth had not started yet. I did some reasearch and after removing my links I did update-rc.d myscript.sh defaults 99 which was claimed to run the script last but it did't make any differrence -- it still ran in the same place in the boot sequence. I verified that the links had S99 on them so it seemed like they were set up correctly. There is another post on SO asking a similar question but that was a Ubuntu system where mine is Poky Linux. That solution suggested putting the startup script in a directory that does not exist on my system. There were other suggestions, putting it in rc.local, which I did and got the same result, it runs before Bluetooth is initialized.
Here is my script. My program is called nmea_thread and is run last. Everything else is initializing Bluetooth.
#!/bin/sh
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread
Often bluetooth is initialized asynchronously, so you can't be sure that your script will be run after hci0 is added. Good solution is to wait for BT initialization in background:
#!/bin/bash
if [ "$1" != "background" ]; then
$0 background &
else
#Wait until BT is initialized
for ((i = 0; i <= 100; i++)) do
hciconfig hci0 && break
usleep 100000
done
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread
fi
hciattach is the correct way.
syntax
hciattach /dev/ttyAMA0 bcm43xx 3000000
you need to flash the driver first before initializing it. Currently i don't remember how, but thats how i made it with raspberry pi and yocto.
Note if you use systemV, you can do call it from a script and it will work
Using SystemD, you need to make it in a service and wait. Falshing should be done in the two cases.

How to disable a specific usb port permanently in linux?

Is it possible to permanently disable a usb port in linux?
I have already figured out how to disable it:
echo -n "0000:00:12.0" > /sys/bus/pci/drivers/ohci_hcd/unbind
BUT after restart it is enabled.
I have placed this script :
#!/bin/sh
case "$1" in
resume|thaw)
echo -n "0000:00:12.0" > /sys/bus/pci/drivers/ohci_hcd/unbind
;;
esac
in /etc/pm/sleep.d/0_disable_usb2
But again without success. I also thought that i could disable it through bios but as i could see i can disable the whole pci.
Is there any way of doing this?
.. My Operating system is Debian 7.7 64bit. The reason i want to do this is I am trying to configure my system for realtime capabilities and my usb soundcard sharing the same IRQ with this port.
For me usb mounting is handle by a service udisk2.service if you would like to stop usb mounting then stop below service
root#mahasan-Inspiron-5537:~# systemctl start udisks2.service
root#mahasan-Inspiron-5537:~# systemctl disable udisks2.service
First of all you should find your USB number of your device, Simply by using lsusb.
And in Linux everything is a file, So you can manage all of your hardware using the files.
As it describes Here if your kernel is > 2.6.38 you should use this keywords:
echo "0" > "/sys/bus/usb/devices/usb2/power/autosuspend_delay_ms"
Then:
echo "auto" > "/sys/bus/usb/devices/usb1/power/control"
echo "0" > /sys/bus/usb/devices/usb{x}/authorized

"killall wpa_supplicant" affects "ip route add ..." in a strange way

Not sure if the title explains my situation correctly, but in details it looks like this:
I'm writing a simple bash script to set up a wireless network, using wlp2s0 interface.
ip route flush dev wlp2s0
ip addr flush dev wlp2s0
ip link set wlp2s0 down
killall wpa_supplicant
ip link set wlp2s0 up
ip addr add 192.168.1.200/24 dev wlp2s0
ip route add default via 192.168.1.1
wpa_supplicant -B -D wext -i wlp2s0 -c wireless.conf
It kills all previously started wpa_supplicants and then starts a new one.
Now, the problem is that the killall call causes ip route add to cry:
RTNETLINK answers: Network is unreachable
no matter if wpa_supplicant was actually started before.
It can be "fixed" by adding a sleep 1 call after killall, but of course I'd like to avoid this. It can also be fixed by removing the killall command and calling it manually before the script.
So the question is - how can I work around this strange behaviour of killall? Maybe someone has any idea why are these strange things happening.
EDIT: ip route add does not give that error if killall isn't called before it.
Why did you think it was strange? Successful return of killall doesn't necessarily mean wpa_supplicant has finished processing the incoming SIGTERM signal. It's only that the signal was delivered to the wpa_supplicant process, at best. Pehaps wpa_supplicant needed some more time (such as sleep 1) to finish execution of its clean-up handler (wpa_supplicant_terminate_proc() in wpa_supplicant.c)
http://hostap.epitest.fi/cgit/hostap/tree/src/utils/eloop.c#n753
http://hostap.epitest.fi/cgit/hostap/tree/wpa_supplicant/wpa_supplicant.c#n4033
So, I think you really need sleep 1.
Update
I always rely on polling method like this
TIMEO=5
for ((i=0; i<TIMEO; ++i)); do
if pidof -s wpa_supplicant > /dev/null; then
sleep 1
else
break
fi
done
if ((TIMEO==i)); then
echo "timeout"
else
echo "it's gone"
fi
assuming there will not be multiple instances of wpa_supplicant.

Resources