Start shell script on Network Manager successful connection

The other day I was writing a script that needed to do its job only when specific network interface is triggered (wireless broadband ppp0 in my case). Pinging Google every 10 seconds to detect Internet access was out of the question. There is a more elegant way to do this. If you are interested please proceed.

Do you know that authors of Network Manager built option to trigger scripts right into this great application. To use this option you need to write bash script with some specific bash variables and put it to "/etc/NetworkManager/dispatcher.d/" directory. Specific variables are necessary to receive instructions from Network Manager about network interface that triggers execution of your script and should it be executed on "up" or "down" operation on that interface.

The following example script starts "command1" after ppp0 goes "up", "command2" just before ppp0 goes "down", "command3" just before ppp0 is "up", and "command4" just after ppp0 goes "down". Replace commands with what you want to accomplish, you can leave some of the command "fields" blank if you're not interested in some use cases like "pre-up" or "post-down".

#!/bin/bash
 
IF=$1
STATUS=$2
 
if [ "$IF" == "ppp0" ]
then
    case "$2" in
        up)
        logger -s "NM Script up triggered"
        command1
        ;;
        down)
        logger -s "NM Script down triggered"
        command2
        ;;
        pre-up)
        logger -s "NM Script pre-up triggered"
        command3
        ;;
        post-down)
        logger -s "NM Script post-down triggered"
        command4
        ;;
        *)
        ;;
    esac
fi

HERE you can download tarball with bash script used as an example (you need to modify it for whatever you want to trigger using Network Manager connections). Save it to your desktop and use this command to extract it:

tar -xjvf  /home/$USER/Desktop/90myscriptname.sh.tar.bz2 -C /home/$USER/Desktop/

This "90" in the name of the script means that this script will be executed in the last 10% of all scripts if you have a bunch of scripts to execute when your interface starts. You probably don't have any other scrips in "/etc/NetworkManager/dispatcher.d/" directory but this option is here if you need it. Now we should give permission to execute by doing "chmod +x" on our script and copy it in place.

chmod +x /home/$USER/Desktop/90myscriptname.sh
sudo cp /home/$USER/Desktop/90myscriptname.sh /etc/NetworkManager/dispatcher.d/90myscriptname.sh

Finally we will monitor "/var/log/syslog" as it changes to make sure that everything is in order:

sudo tail -f /var/log/syslog

If everything is OK you will see "NM Script action triggered" when you connect or disconnect network interface in question. If not, retrace your steps and double check everything. You can also ask here for my assistance and I will do my best to help you. That's it. Enjoy!

DevGenii

A quality focused Magento specialized web development agency. Get in touch!

24 thoughts on “Start shell script on Network Manager successful connection

  1. ignacio

    Hi,

    I want to use you script to mount and unmount a network drive.

    The unmount part works perfectly but the mount does not work 🙁

    This is my script
    #!/bin/bash

    IF=$1
    STATUS=$2

    if [ “$IF” == “eth1” ]
    then
    case “$2” in
    up)
    logger -s “NM Script up triggered”
    mount /home/ignacio/homedir
    ;;
    down)
    logger -s “NM Script down triggered”
    fusermount -u /home/ignacio/homedir/
    ;;
    esac
    fi

    And this is the line of the network drive in my fstab
    sshfs#im5j@blue.unix.virginia.edu:/home/im5j /home/ignacio/homedir fuse fsname=sshfs#im5j@blue.unix.virginia.edu:/home/im5j,user,noauto,reconnect,transform_symlinks,BatchMode=yes 0 0

    Any idea of what I’m doing wrong?

    Thanks!

    -Ignacio

    Reply
  2. ignacio

    @ignacio
    I figured out the problem and i have an idea of how to solve it.

    The script is trying to mount as root. I need to tell it to do it as the user ignacio.
    Any idea how to do that

    Best,

    -Ignacio

    Reply
  3. ignacio

    @Marko Martinović
    🙁
    Nope, that is not the solution to my problem…
    When i try to do it from the terminal (as root) it does not work

    root@ignacio-Laptop:/home/ignacio# sudo -u ignacio /home/ignacio/scripts/homedir.sh
    read: Connection reset by peer

    Reply
  4. ignacio

    @Marko Martinović
    I can mount it as root but if I do that I can not read it as Ignacio :_(

    root@ignacio-Laptop:/home/ignacio# mount /home/ignacio/homedir/
    root@ignacio-Laptop:/home/ignacio# cd homedir/
    root@ignacio-Laptop:/home/ignacio/homedir# exit
    ignacio@ignacio-Laptop:~$ ls
    ls: cannot access homedir: Permission denied

    any idea on how to fix that?

    Thanks a lot!

    Reply
  5. Marko Martinović Author

    @ignacio

    You need to give “user” param inside fstab like in the example.

    “user – Permit any user to mount the filesystem. This automatically implies noexec, nosuid,nodev unless overridden.”

    “Any” should include root and any other user. Did you try that? 🙂

    Reply
  6. ignacio

    @Marko Martinović
    I had user in my fstab from the begining 🙁

    sshfs#im5j@blue.unix.virginia.edu:/home/im5j /home/ignacio/homedir fuse fsname=sshfs#im5j@blue.unix.virginia.edu:/home/im5j,user,uid=1000,gid=1000,umask=007,noauto,reconnect,transform_symlinks,BatchMode=yes 0 0

    I can mount as root or ignacio but if i mount as root i can not access the folder as ignacio. Your script mount as root 🙁

    Reply
  7. Marko Martinović Author

    Ignacio found solution to this problem but at the time comments weren’t working because of TechyTalk.info hoster security settings (problem is resolved). Here’s the mail from Ignacio with the solution to his problem:

    I have it working.
    The script that is going to mount and umount is the following

    #!/bin/bash
    #/etc/NetworkManager/dispatcher.d/homedir

    IF=$1
    STATUS=$2

    if [ “$IF” == “eth1” ]
    then
    case “$2” in
    up)
    logger -s “NM Script up triggered”
    mount /home/ignacio/homedir/
    ;;
    down)
    logger -s “NM Script down triggered”
    fusermount -u /home/ignacio/homedir/
    ;;
    *)
    ;;
    esac
    fi

    In /etc/fstab i have

    sshfs#im5j@blue.unix.virginia.edu:/home/im5j /home/ignacio/homedir
    fuse
    fsname=sshfs#im5j@blue.unix.virginia.edu:/home/im5j,user,uid=1000,gid=1000,allow_other,umask=007,comment=sshfs,noauto,reconnect,BatchMode=yes
    0 0

    and finally in /etc/fuse.conf
    user_allow_other

    Thanks a lot for the help!!!

    -Ignacio

    Reply
  8. nicola

    Thank you very much Marko!
    I’ve tried your instruction and all seems work.
    I’ve a question: how can I run a script, not when interface is up, but when certain connection of an interface is up? (e.g. not when wlan0 is up, but when wireless connection “pippo-wifi”, that it is one of many connection that use wlan0, is up?)
    Another time,
    thanks,
    Nicola

    Reply
    1. Dana

      Hello. man NetworkManager says that “Each script receives two arguments, the first being the interface name of the device just activated, and second an action.” So I guess no you can’t use this method to detect connection only interfaces, I might be wrong.

      Reply
  9. Ball

    Unfortunately, NetworkManager seems forever crippled as pre-up isn’t implemented and seems will never be. It’s been an issue raised since 2007, so it’s a safe bet it’ll never be fixed because no-one can agree how to implement it. What a mess.

    That means anyone who needs pre-up can’t use NetworkManager and must instead manually set up everything like a slave. Of course, wifi devices have to be setup for each situation, which is why NetworkManager was created, which I can’t use, ’cause it’s forever broken…..ahh linux

    Reply
    1. Marko Author

      True what you say but alternatives to Linux have even more “sell your soul to the devil” type of downsides so I guess we will stick to Linux 🙂 Also we can implement the functionality our selves it looks nice on the job application to say you’ve contributed to such major Linux app 😉

      Reply
  10. leili

    dear marko
    I follow all your instruction but not anything happens!
    It doesn’t work
    why?
    I just want to add sum route when an interface become up
    please help me..

    Reply
    1. Mike

      Seconding this… for me, at least, not only SHOULD you change it, but you MUST change it to vpn-up / vpn-down.  It took me a while to figure this out.

      Reply
  11. Roxo

    This post is old but still holds.

    I was using it without any problem in [Xu|U]buntu 14.04. In 16.04 the “if” clause never got a True, so the “case” were never executed. The problem seems to be in the “if” clause. In order to check for equality one must use “=” and not “==” as published. Curiously the script worked until the upgrade.

    HTH

    Reply
  12. Tony

    Thanks for this. Solved my problem nicely.

    Trivial query on the script:
    you collect the value of $2:
    STATUS=$2

    but in the ‘case’ statement you use $2:
    case "$2" in
    rather than:
    case "$STATUS" in

    Is there a technical reason for this, or just a bad hair day? 🙂

    Reply
  13. smartmeister

    Hi guys.

    Network-manager seem to execute pre-up commands nicely in ubuntu-mate 17.04.

    I wanted to enable WOL support as a pre-up command on my enp1s0 network interface, so that the computer is in the correct wol awareness state when i shut it down later.

    All i had to do is create /etc/NetworkManager/dispatcher.d/pre-up.d/10wol.sh and fill it with the following command:


    #!/bin/bash
    /sbin/ethtool -s enp1s0 wol g

    and make it executable.

    sudo chmod +x /etc/NetworkManager/dispatcher.d/pre-up.d/10wol.sh

    i know it’s working as expected because ethtool show that the interface is set to “Wake-on: g” after a successfull pre-up command, otherwise it would stay as “Wake-on: d”, with is the default unconfigured state.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *