You are not logged in.

#1 2020-08-24 14:53:51

toothandnail
Member
From: Oxfordshire, UK
Registered: 2012-05-05
Posts: 88

Oddity with a NetworkManager dispatcher script

My laptop gets used in multiple places, on multiple networks. In a couple of instances, I normally use a wired connection, but occasionally need to use wifi. Annoyingly, when I plug a cable in, I get both a wired and a wireless connection. Its easy enough to disconnect the wifi, but if it is made, it seems to be the place where the laptop's name is registered with local DNS. When disconnected, I can only identify the laptop by IP. A bit of a pain...

I got annoyed enough a little while ago to see how I could set things up so that NetworkManager wouldn't connect to wifi if a cable was plugged in. Following the wiki, I set up a dispatcher script. Which works well, in most respects. If there is a cable plugged in, wifi is disabled. If I unplug the cable, a wifi connection will be made. Great...

Only problem is, if I shut the laptop down without unplugging the cable first, the next time I power the laptop up, wifi is disabled and I have to manually enable it from the command line.

The script I'm using is direct from the wiki:

#!/bin/sh

if [ "$1" = "enp0s25" ]; then
    case "$2" in
        up)
            nmcli radio wifi off
            ;;
        down)
            nmcli radio wifi on
            ;;
    esac
fi

Can anyone suggest how I can overcome this? I don't really understand why it works if the cable is unplugged when the system is running but doesn't if it is not disconnected before shut down.

Offline

#2 2020-09-04 12:27:11

fistrosan
Member
Registered: 2020-04-01
Posts: 171

Re: Oddity with a NetworkManager dispatcher script

Why don't you first detect whether the cable is plugged or not ? You can do it with something along these lines:

#!/bin/bash

dmesg | grep 'ethernet' > ~/tmp
ethernet=$(ls -la ~/tmp | awk '{print $5}')

if [ $ethernet -eq "0" ]; then
    echo "Starting WIFI connection"
    ip link set eth0 down
    ip link set wlan0 up 
    wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/whatever.conf
    dhcpcd wlan0
fi
if [ $ethernet -ne "0" ]; then
    echo "Staring Ethernet connection"
    ip link set wlan0 down
    ip link set eth0 up
    dhcpcd eth0
fi
rm ~/tmp

Offline

#3 2020-09-04 12:49:30

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,530
Website

Re: Oddity with a NetworkManager dispatcher script

That script doesn't make much sense at all.  You are grepping for a word and piping results to a file, then using ls on the file just to try to detect the file size, and later using that size in a conditional??  That's so very wrong on so many levels.  First, there are conditional checks to see if a file is empty without relying on ls and awk.  Second, ls piped to awk is a horrible way just to check the file size - that will totally break if ls format is different.  Third, there is absolutely no reason to create a temporary file when you could just pipe from grep into something like `wc` to see if there is any resulting content.  Fourth, there's no reason to test "-eq 0" then "-ne 0" in seperate if blocks when you should use if / else.  Fifth, there's no reason for any of that if you actually learn to use grep!

if dmesg | grep -q ethernet; then
   echo "Starting Ethernet ..."
   ...
else
   echo "Starting WIFI ... 
   ...
fi

But Sixth, and most important - grepping dmesg is not a remotely reliable way to even check what you want to check!

Last edited by Trilby (2020-09-04 12:50:21)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2020-09-04 13:21:16

fistrosan
Member
Registered: 2020-04-01
Posts: 171

Re: Oddity with a NetworkManager dispatcher script

Thanks for your comments Trilby. I am not well versed in bash scripting so I am willing to accept the criticism and learn. Having said that, FWI my script works for me. It was just an example. Toothandnail can adapt it to his/her needs. I provided some input to someone who has been waiting 10 days. When the gurus, such as yourself, do not answer, then the ball passes onto us laymen ... of course, the quality of the answer will not be the same right ?

Offline

#5 2020-09-04 22:18:50

bulletmark
Member
From: Brisbane, Australia
Registered: 2013-10-22
Posts: 653

Re: Oddity with a NetworkManager dispatcher script

I also want NetworkManager to switch off wifi when wired connected, and vice-versa. Here is my solution: https://gist.github.com/bulletmark/8e05 … 8c528e7764. There is nothing to configure.

Offline

Board footer

Powered by FluxBB