Use wlan0 or eth0 in different scripts / terminal - linux

I have a debian installation using eth0 and wlan0.
For all applications I want to use eth0 except for one single script I want to use wlan0.
Is there any possibility to force for example in a terminal session or in e.g. a shell script the usage of wlan0 for this specific script?
Thanks a lot for your help

Yes, it is quite easy to use eth0 by default and then if you need to use wlan0 for one run simply pass wlan0 as a command-line argument. You should also check that the value you are using is either eth0 or wlan0, or you should consider the argument invalid.
A short script that implements that logic would be:
#!/bin/sh
iface=${1:-eth0} ## use eth0 by default or use the first argument
## if the iface entered is not eth0 or wlan0, handle error
if [ "$iface" != "eth0" ] && [ "$iface" != "wlan0" ]
then
printf "error: invalid interface '%s'\n" "$iface"
exit 1
fi
printf "using: %s\n" "$iface" ## output interface being used
Example Use/Output
$ sh useiface.sh
using: eth0
$ sh useiface.sh wlan0
using: wlan0
$ sh useiface.sh eth1
error: invalid interface 'eth1'
You can see above when the user attempts to call the script with eth1 that is not considered valid and the script provides an error and exits. You can adjust to fit your needs.

Related

identify correct interface in Linux among others

I have some physical servers with multiple interface, each interface is assigned to a separate vlan. The correct mapping of interface number to vlan from network admins usually differs from what I see inside my console and the common procedure is to check MAC of interface he has configured. I tried to find a solution to resolve this. The first solution I tried was to get tcpdump from the interface to see what type of ip address is broadcasting which it is cumbersome. The second solution was to arping the gw to see if this is true interface with a command like:
arping -c 1 -D -q -I $i $SGW1
if [ $? == 1 ]; then echo "$i $SGW1" ;SIF+=("$i"); fi
like
arping -c 1 -D -q -I ens160 172.20.29.158
but it take time and sometimes with wrong answers. Could you please suggest a better way?
(we are not running DHCP by the way)

CentOS Network Interface Post-Up Script Not Executing

I am running CentOS 7.2, and I'm struggling to get a simple script to execute on ifup of any interface.
My /sbin/ifup-local looks like this:
[root#oracle2 ~]# cat /sbin/ifup-local
#!/bin/bash
if [[ "$1" == "eth0" ]]
then
exec /vpnup
fi
[root#oracle2 ~]#
The referenced script /vpnup looks like this:
[root#oracle2 ~]# cat /vpnup
#!/bin/bash
#
# CompanyX Production L2TP VPN - UP
#
#
echo -e "\n"
echo -e "PLEASE WAIT\n"
echo -e "Dialling Production L2TP VPN... \n"
echo -e ".........................................\n"
ipsec auto --up L2TP-PSK && echo "c qvprodvpn" > /var/run/xl2tpd/l2tp-control
echo -e ".........................................\n"
echo "Connected..."
echo "Adding local static route to manage VPN bound traffic..."
sleep 6s
ip route add 10.10.24.0/24 via 10.10.24.51
echo "Route added..."
echo -e "...\n"
[root#oracle2 ~]#
Fairly simple, the script works fine when called at command line. It just dials into a L2TP VPN that I've setup, to get this box access to the production LAN of another segment of their network.
However, if I execute "service network restart" or indeed "systemctl restart network.service", the VPN interface does not come up, nor does the ip route get added. If I manually execute ifdown eth0, and then ifup eth0, it also does not run the script as intended.
If I execute "/sbin/ifup-local eth0" the script runs as expected, so I know my script is fine, and I know my ifup-local is fine.
Am I missing something obvious? I've never worked with pre/post up scripts before, but I always figured they were pretty simple... Was I wrong?
Ensure your ifcfg-eth0 script includes
NM_CONTROLLED=no
Otherwise, calling systemctl restart network or ifup eth0 will not execute ifup-pre-local, ifup-eth, ifup-post, ifup-local, etc. for eth0. They will still be called for lo, though.

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.

How to find a Linux namespace by network adapter?

I have a Linux server with multiple network namespaces defined:
# ip netns list
qdhcp-7dedbd4e-2265-4aa2-baac-add4e341dd18
qdhcp-851379ba-1d51-4e45-8e50-b756e81c0949
qdhcp-a19927c5-83b4-4bb4-a8b8-f21fdb5e004b
qdhcp-b94605ff-b0e2-4cfe-a95e-3dd10208a5fb
... ...
Each namespace contains one or more virtual network adapters - in this case, it's a TAP device:
# ip netns exec qdhcp-7dedbd4e-2265-4aa2-baac-add4e341dd18 ip route
192.168.168.0/24 dev tapda4018ab-b7 proto kernel scope link src 192.168.168.2
169.254.0.0/16 dev tapda4018ab-b7 proto kernel scope link src 169.254.169.254
default via 192.168.168.1 dev tapda4018ab-b7
Now let's say I know the name of the adapter - tapda4018ab-b7 - but I don't know the namespace it belongs to. Is there a way to look it up without checking namespaces one by one? Is there a generic Linux command to do this? Or at least OpenStack Neutron-specific command?
According to this man page http://man7.org/linux/man-pages/man8/ip-netns.8.html you could run the exec command on all namespaces but I tested it on an ubuntu trusty servers and it will not accept "-all" as an argument. So the only way I know to get such an information is via a small bash script. I made one that could certainly be improved as my scripting skills are rather basic, but it will do the work:
#!/bin/bash
i=$(ip netns list | wc -l)
counter=1
while [ $counter -le $i ]; do
ns=$(ip netns | head -$counter | tail -1)
ip netns exec $ns ip route | grep $1 | grep proto
let counter=counter+1
done
You can then launch the script using as sole argument your tap device as in the example bellow:
root#columbo:~# ./list_all_namespace tap8164117b-e3
5.5.5.0/25 dev tap8164117b-e3 proto kernel scope link src 5.5.5.3
If you do not provide an argument it will give you an error.
If I understand Neutron correctly (which is a big if - my only experience is with a fairly limited toy installation of Kilo/2015.1.2), you should be able to track through neutron's database to figure out the netns you're looking for
I believe your tap interface would be named using the first 5 octets (10 characters) of the port uuid that it's associated with, and the qdhcp netns uses the uuid of it's network, so you should be able to use the neutron CLI to track down the correct namespace.
You should be able to find the neutron port for your tap interface with:
$ neutron port-list | grep "da4018ab-b7"
| da4018ab-b7xx-xxxx-xxxx-xxxxxxxxxxxx | | fa:16:xx:xx:xx:xx | {"subnet_id": ...
where "da4018ab-b7" was pulled out of "tapda4018ab-b7". You can then use the full port uuid:
$ neutron port-show da4018ab-b7xx-xxxx-xxxx-xxxxxxxxxxxx
The network_id in the result from port-show should let you figure out the netns (qdhcp-network_id) containing tapda4018ab-b7.
You should be able to use similar logic to track down qg interfaces (which will probably show up on bridges in the default netns), but in that case it's the device_id that owns the port that gives you the qrouter-device_id netns you want.
You can use this script. Save this as get_dhcp_namespace.sh :-
ubuntu#ubuntu$ cat get_dhcp_namespace.sh
#!/bin/bash
interface=$1
id=${interface:3}
port_id=$(neutron port-list | grep $id | awk -F'|' '{print $2}' | tr -d ' ')
net_id=$(neutron port-show $port_id | grep network_id | awk -F'|' '{print $3}' | tr -d ' ')
echo "DHCP namespace is: qdhcp-$net_id"
Run this with the tap interface provided as argument. Don't forget to source the keystonerc/openstackrc/credentials file.
ubuntu#ubuntu$ ./get_dhcp_namespace.sh tapda4018ab-b7
qdhcp-bd39f45d-b45c-4e08-ab74-85c0b1180aea

"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