[Solved] NetworkManager/dispatcher.d scripts not being executed for wlan up/down

Linux Kernel, Network, and Services configuration.
Post Reply
Message
Author
seberoon
Posts: 4
Joined: 2024-09-26 20:37

[Solved] NetworkManager/dispatcher.d scripts not being executed for wlan up/down

#1 Post by seberoon »

[I first posted essentially this question in the RPi Troubleshooting forum, but no takers. I have now tried exactly the same thing in a fresh install of stock Debian 12 on a separate machine. Same behaviour, so this doesn't seem to be RPi-specific. I'm hoping that one of the Debian network gurus will be able to set me straight. For context, I have not made any changes to the network configuration beyond plugging in a wifi dongle and using the Gnome widget to connect to my home wifi. The wlan interface name below is the one that was chosen automagically.]

I have a script that I want to be executed when the wireless lan interface is taken down. I have tried both the following: a) place a script in /etc/network/if-down.d; b) place a script in /etc/NetworkManager/dispatcher.d. As far as I can tell, a) has no effect (the script is never run). For b) the script is run sometimes, but not when the wlan interface is taken down. For debugging purposes I have this:

Code: Select all

> cat /etc/NetworkManager/dispatcher.d/00-log-iface-events.sh
#!/usr/bin/env bash
interface=$1
event=$2
echo "===========" >> /tmp/Hello-there.txt
echo "$interface received $event" >> /tmp/Hello-there.txt
Having checked that wlx7cdd905b9c3d is up, I do

Code: Select all

sudo ip link set wlx7cdd905b9c3d down
Nothing is written to the tmp file at all.
I then do

Code: Select all

sudo ip link set wlx7cdd905b9c3d up
The following is written:

Code: Select all

===========
wlx7cdd905b9c3d received dhcp4-change
===========
wlx7cdd905b9c3d received dhcp6-change
Note that only the dhcp events are logged, no "up" event.

One additional data point: if I reboot with that script in place in /etc/NetworkManager/dispatcher.d, an "up" event is logged in the tmp file, so it seems that things do behave the way I was expecting when the wlan is first brought up.

Any ideas?
Last edited by seberoon on 2024-09-27 14:11, edited 2 times in total.

Aki
Global Moderator
Global Moderator
Posts: 3928
Joined: 2014-07-20 18:12
Location: Europe
Has thanked: 108 times
Been thanked: 517 times

Re: NetworkManager/dispatcher.d scripts not being executed for wlan up/down

#2 Post by Aki »

Hello,

What is the content of the file named /etc/network/interfaces ? You can use the following command:

Code: Select all

cat /etc/network/interfaces
Just a guess. Try the following command:

Code: Select all

sudo ifdown wlan0
Then check the output of tmp file generated by the script in /etc/network/if-down.d
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
⠈⠳⣄⠀

seberoon
Posts: 4
Joined: 2024-09-26 20:37

Re: NetworkManager/dispatcher.d scripts not being executed for wlan up/down

#3 Post by seberoon »

[Note: the actual wlan interface name is wlx7cdd905b9c3d not wlan0. Ugh. Where I wrote wlan0 in my original post it was a copy-paste error from my earlier RPi forum post.]

@Aki It looks like ifupdown cannot be used to manage interfaces used in the default Debian 12 network set up:

Code: Select all

> cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback
(/etc/network/interfaces.d is empty).
So:

Code: Select all

> ifdown wlx7cdd905b9c3d
ifdown: unknown interface wlx7cdd905b9c3d
From a bit of Googling it seems that I need to use the ip command and NetworkManager is (mostly?) in charge of things.

Aki
Global Moderator
Global Moderator
Posts: 3928
Joined: 2014-07-20 18:12
Location: Europe
Has thanked: 108 times
Been thanked: 517 times

Re: NetworkManager/dispatcher.d scripts not being executed for wlan up/down

#4 Post by Aki »

Hello,

Can you please check if the network manager is running ? You can use the following commands:

Code: Select all

ps aux | grep /usr/sbin/NetworkManager
nmcli networking
nmcli device
You can check the permission for your script with the command:

Code: Select all

ls -la /etc/NetworkManager/dispatcher.d/*
You can disable and re-enable your network interface to verify if the script in /etc/NetworkManager/dispatcher.d/ is triggered:

Code: Select all

sudo nmcli device down wlx7cdd905b9c3d
sudo nmcli device upwlx7cdd905b9c3d
⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
⠈⠳⣄⠀

seberoon
Posts: 4
Joined: 2024-09-26 20:37

Re: NetworkManager/dispatcher.d scripts not being executed for wlan up/down

#5 Post by seberoon »

@Aki Hi, yes, it is running. Note from my original post that the script in /etc/NetworkManager/dispatcher.d is being executed for dhcp status changes (those that are triggered after the wlan interface comes up). My issue is specifically that the script doesn't get called in response to the wlan going up or down.

PS I think I have found a workaround/solution: use wpa_cli with an action script. Will post a follow-up once I am sure.

lindi
Debian Developer
Debian Developer
Posts: 556
Joined: 2022-07-12 14:10
Has thanked: 2 times
Been thanked: 110 times

Re: NetworkManager/dispatcher.d scripts not being executed for wlan up/down

#6 Post by lindi »

Your service might have a private /tmp, try writing to some other location or strace the nm execution

seberoon
Posts: 4
Joined: 2024-09-26 20:37

Re: NetworkManager/dispatcher.d scripts not being executed for wlan up/down

#7 Post by seberoon »

I found a way using wpa_cli, as described in this old stack exchange post, for example: https://unix.stackexchange.com/question ... aemon-mode. To test, I placed a test script in /etc (it doesn't work if the script is in /root, as I eventually realised after much cursing that wasn't the issue: make sure to specify the full path for your action script file - a local path only works if you are not running wpa_cli as a daemon):

Code: Select all

> cat /etc/wpa-action.sh 
#!/usr/bin/bash

interface=$1
event=$2

echo "=========== wpa_cli action =====" >> /tmp/Hello-there.txt
echo "$interface received $event" >> /tmp/Hello-there.txt
echo "WPA_CTRL_DIR: $WPA_CTRL_DIR" >> /tmp/Hello-there.txt
echo "WPA_ID: $WPA_ID" >> /tmp/Hello-there.txt
echo "WPA_ID_STR: $WPA_ID_STR" >> /tmp/Hello-there.txt
Then execute:

Code: Select all

wpa_cli -a /etc/wpa-action.sh -B
Now when I bring the wlan up and down, the script gets run and I see the following kind of thing in the tmp file:

Code: Select all

=========== wpa_cli action =====
wlx7cdd905b9c3d received DISCONNECTED
WPA_CTRL_DIR: /var/run/wpa_supplicant
WPA_ID: 0 
WPA_ID_STR:  
=========== wpa_cli action =====
wlx7cdd905b9c3d received CONNECTED
WPA_CTRL_DIR: /var/run/wpa_supplicant
WPA_ID: 0
WPA_ID_STR: 
Last edited by seberoon on 2024-09-27 15:12, edited 1 time in total.

seberoon
Posts: 4
Joined: 2024-09-26 20:37

Re: NetworkManager/dispatcher.d scripts not being executed for wlan up/down

#8 Post by seberoon »

PS How do I mark as solved? Sorry, figured it out.
Last edited by seberoon on 2024-09-27 14:11, edited 1 time in total.

Post Reply