Linux : NetworkManager's /etc/NetworkManager/dispatcher.d/ test script does not work. code included [closed]

StackOverflow https://stackoverflow.com/questions/16848021

  •  30-05-2022
  •  | 
  •  

Question

Based on information, any file inside of

/etc/NetworkManager/dispatcher.d/

is suppose to receive 2 possible commands from

NetworkManager

to find out what these commands might be we simply
look at the source of one of the scripts that is already in

/etc/NetworkManager/dispatcher.d/

so let's do that.

cd     /etc/NetworkManager/dispatcher.d/
ls
00-netreport  05-netfs  10-dhclient
vi 00*

nothing here.

gedit 05*

excellent.

#!/bin/sh

export LC_ALL=C

if [ "$2" = "down" ]; then
    /sbin/ip route ls | grep -q ^default || {
        [ -f /var/lock/subsys/netfs ] && /etc/rc.d/init.d/netfs stop || :
    } && { :; }
fi

if [ "$2" = "up" ]; then
    /sbin/ip -o route show dev "$1" | grep -q '^default' && {
        /sbin/chkconfig netfs && /etc/rc.d/init.d/netfs start || :
    } || { :; }
fi

let's copy this and create a file named

 test

let's only use what we need which is "up".

file: test


#!/bin/sh

export LC_ALL=C

if [ "$2" = "up" ]; then
    /sbin/ip -o route show dev "$1" | grep -q '^default' && {
        /sbin/chkconfig netfs && /etc/rc.d/init.d/netfs start || :
    } || { :; }
fi

let's modify it so that it can execute something "visually" that we can test it with.

#!/bin/sh

export LC_ALL=C

if [ "$2" = "up" ]; then
    gedit test.txt
fi

Result:

Not Working.

let's modify it in a way so that it may understand.

#!/bin/sh

export LC_ALL=C

if [ "$2" = "up" ]; then
    /sbin/ip -o route show dev "$1" | grep -q '^default' && {
        gedit test.txt || :
    } || { :; }
fi

rebooting..

result:

nothing

SOLVED:

here's the smallest possible code from the chosen answer:
( that i was able to create )

case "$2" in
    up)
touch /root/Desktop/ooo
    ;;
esac

i needed to make it smaller because i reformat my machine often and then run a script to automatically set these things up. so in the future i simply will run

cat >> /etc/NetworkManager/dispatcher.d/test << EOF
case "\$2" in
    up)
touch /root/Desktop/ooo
    ;;
esac
EOF
chmod +x /etc/NetworkManager/dispatcher.d/test

in other words the $ has to be escaped with "cat>>" thing.

Était-ce utile?

La solution

Here's a working example of a script that runs when NetworkManager connects:

#!/bin/sh -e
# Script to dispatch NetworkManager events
#
# Runs ifupdown scripts when NetworkManager fiddles with interfaces.
# See NetworkManager(8) for further documentation of the dispatcher events.

if [ -z "$1" ]; then
    echo "$0: called with no interface" 1>&2
    exit 1;
fi

# Run the right scripts
case "$2" in
    up|vpn-up)
    logger -s "HELLO THIS IS YOUR SCRIPT"
    cd /home && touch "HELLOFILE.txt"
    ;;
    down|vpn-down)
    ;;
    hostname|dhcp4-change|dhcp6-change)
    # Do nothing
    ;;
    *)
    echo "$0: called with unknown action \`$2'" 1>&2
    exit 1
    ;;
esac

Name the script above anything you like, make it executable, and put it into the /etc/NetworkManager/dispatcher.d/ directory.

You can verify that this works by opening a terminal and running:

sudo tail -f /var/log/syslog //print system log until cancelled

Then open another terminal, and run:

sudo service network-manager restart //restart NetworkManager

You should see the words "HELLO THIS IS YOUR SCRIPT" appear in the log, with a lot of other info. And the file should be added to the /home directory.

I'm not sure why your specific case of opening gedit isn't working, but this is the way to run a script when network manager starts. My guess is that the rest of the problem is related to the fact that the script runs as root but you're wanting gedit to open in the desktop environment of a specific user (you). Good luck!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top