Programmatically reload network settings on Linux - linux

What is the best way to programmatically reload the network settings from the config file /etc/network/interfaces ?
At the moment I use the system command to execute ifdown and ifup on the interface, but i'm not happy with this solution.

being more specific would help ...
but,
maybe you could write some script passing the interfaces as parameters
and turning it down and up, the usual way to re-read the /etc/network/interfaces file is that (I think so).
for interface in $#
do
sudo ifdown ${interface} && sudo ifup ${interface}
done

Related

Use of alias in script

I have two scripts used to setup perf on an embedded linux system and they work fine except for the last line
#!/bin/bash
sudo route add -net 192.168.2.0 gw 192.168.42.1 netmask 255.255.255.0
scp -P 23 ~/perf_p7/perfSetup.sh 192.168.42.1:../tmp
ssh -p 23 192.168.42.1 bash ../tmp/perfSetup.sh
and
#!/bin/bash
mkdir /mnt/buildroot-target
IP=192.168.42.2
nfs.sh ${IP}
mount -o nolock,proto=tcp,addr=${IP} -t nfs ${IP}:/home/vclement/sfx1_build/Binaries/p7-arm/master/buildroot/target /mnt/buildroot-target/
alias perf='LD_LIBRARY_PATH=/mnt/buildroot-target/usr/lib /mnt/buildroot-target/usr/bin/perf'
alias perf='LD_LIBRARY_PATH=/mnt/buildroot-target/usr/lib /mnt/buildroot-target/usr/bin/perf' does not seem to work however when I run it manually in the console, it works just fine in order to let me use the "perf" command directly.
If anybody has any idea why that is or has a fix, thanks for letting me know
The problem is that alias is a built-in command for bash. Its effect is lost when the script ends.
If you want it to apply to your current shell as well you have to 'source' the second script, either:
source ./<scriptname>
or:
. ./<scriptname>
You can use functions. They are more appropriate for something more complex then just simple commands:
function perf() {
LD_LIBRARY_PATH=/mnt/buildroot-target/usr/lib /mnt/buildroot-target/usr/bin/perf
}

Detect if an ethernet port is unplugged

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.

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

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

Persist ifconfig lo multicast across reboots on Linux RHEL 6

Is there a way to persist the following command across reboots on Linux RHEL 6 (other than perhaps creating an init.d script):
ifconfig lo multicast
I have an application that requires multicast on 127.0.0.1, and I was wondering whether there's something in /etc/sysconfig/network-scripts that can help persist the ifconfig lo multicast across reboots.
Thanks in advance.
Thanks for the /etc/rc.local suggestion; it would work, and it's technically not an init.d script, so in my humble opinion, the -1 is a bit unfair. However, I'd like to avoid that route if possible in order to improve the maintenance of the file.
After some digging around, I couldn't find any files in the /etc/sysconfig/network-scripts/ that could help; however, I did find a potential solution; after tracing the ifup scripts, there's a hook to invoke an ifup-${DEVICE} file; since that doesn't exist for lo, I've created an ifup-lo file with 755 permissions as follows:
/etc/sysconfig/network-scripts/ifup-lo
# ifconfig is deprecated; use ip link instead...
/sbin/ip link set lo multicast on
/sbin/ip link set lo up
This will be invoked by the os when ifup lo is called, allowing any custom hooks to be added.
Any better solutions are appreciated.
The ideal solution would be for Redhat to add a MULTICAST=on or MULTICAST=off variable in the /etc/ifcfg-lo , but since that's not currently in place, ifup-lo will do for now...
append ifconfig lo multicast to the end of /etc/rc.local

Resources