Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way for me to call a shell script once I connect to a specific wireless network? Reason I want to do this is I have to login to the network before I can start using it and I would like to automate this if possible.

I read this question: Is there a way to run a script every time I connect to a specifc wireless network?

But I'm really unsure as to how to use upstart to do it.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

Apologies for my previous answer which was the way I would have done it years ago. Seems things have changed.

It turns out that Network Manager runs ALL of the scripts in the /etc/NetworkManager/dispatcher.d/ directory (those owned by root, that are executable, that are not readable by other users, and not setuid), when a connection changes (up, down, preup, predown).

Environment variables are set and passed to this script by Network manager. You'll be interested in the CONNECTION_UUID environment variable (contains a unique string).

So, to solve your problem (execute a script when a particular wireless network is connected to):

1) find out the uuid of the wireless connection you're interested in (by looking inside the appropriate connection file in the /etc/NetworkManager/system-connections/ directory).

2) write a bash (or perl, or python, or whatever) script that does what you want if the environment variable CONNECTION_UUID is a match to the uuid of the wireless network in (1) above.

3) put this script into /etc/NetworkManager/dispatcher.d/ and set the owner and permissions appropriately.

further reading: man networkmanager ( and a litte poking around the scripts in the directories mentioned above).

An example script:

#!/bin/bash
#####################################
# MounterBeast Script
# /etc/NetworkManager/dispatcher.d/02remotemount
# Copyright 2011 Nathan E. Williams
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Usage:
# This script must be customized for your configuration.
# By default, the script will attempt to mount a CIFS share
# when a specified MAC address is found at the network gateway,
# or over sshfs if the MAC address of the gateway is not the specified MAC.
# e.g. I mount over CIFS to the servers internal IP when at home, and
# over sshfs when away from home.
#
# id gateway mac without physically checking the sticker:
# $ arp -n -a $(ip route show 0.0.0.0/0 | awk '{print $3}') | awk '{print $4}'
#
# Testing:
# up) sudo /etc/NetworkManager/dispatcher.d/02remotemount wlan0 up
# down) sudo /etc/NetworkManager/dispatcher.d/02remotemount wlan0 down
#####################################
#
# Configuration:
#
targetmac='xx:xx:xx:xx:xx:xx'
mount_user='$USER'
mount_pass='pass'
internal_server_name='192.168.1.102'
external_server_name='my.dyndns.com'
share_name="music"
mount_point='/mnt/remote'
ssh_port='22'
#
# Should not need to edit below
#
gateway=$(ip route show 0.0.0.0/0 | awk '{print $3}')
mactest=$(arp -n -a $gateway | awk '{print $4}')

if [[ "$mactest" == "$targetmac" ]]
then
  case "$2" in
          up)
          sleep 5
          mount -t cifs -o username=$mount_user,password=$mount_pass //$internal_server_name/$share_name $mount_point
          ;;
          down)
          umount -l $mount_point
          ;;
  esac
else
  case "$2" in
      up)
          sleep 5
          sshfs -p $ssh_port $external_server_name:$share_name $mount_point
      ;;
      down)
          umount -l $mount_point
      ;;
  esac
fi

exit $?
share|improve this answer
    
Hey, there is nothing in /etc/NetworkManager/system-connections. Apparently, the UUID is only stored there if the connection is new (never used before). Nevertheless, I got it to work by creating a bash script in /etc/network/if-up.d. It does run the script in any wireless network I connect to though but I worked around it by doing iwconfig | grep -q "some SSID". Thanks for the bump towards the right direction though, wouldn't have figured it out without your help! –  Brock Dute Nov 20 '10 at 19:16
    
works for my gentoo installation too, thanks for the tip :) –  Jeffrey04 Jun 13 '13 at 9:57

Yes, Shell scripts in /etc/NetworkManager/dispatcher.d/ for NetworkManager is a very good idea.

There is also a Dbus method with NetworkManager, more fun, more complicated: man nm-settings.

A resume of shells arguments from Man page of NetworkManager about dispatcher:

Each script receives two arguments, the first being the interface name of the device just activated, and second an action.

Action can be: up, down, vpn-up, vpn-down, hostname, dhcp4-change, dhcp6-change . (Release of the man page: 17 January 2012)

Here is a very simple script to restart OpenVPN after an network interface was up:

if [ "$2" = "up" ]; then  
       /etc/init.d/openvpn restart  
fi  
exit $? 
share|improve this answer

I don't know if there is a way to do it with Network Manager, there is one probably but I have another solution for you. You can install Wicd :

sudo apt-get install wicd

Wicd have support directly on the gtk interface to add pre-script and post-script support to every network you can connect. Take care that Wicd will desinstall Network-Manager to work (they are both in conflict) so if something goes wrong you should download the .deb of Network-Manager or have a Live-CD/Live-USB with you.

Wicd is easy to use and quicker to connect but lack some advanced features of Network-Manager (like VPN). Here is a screenshot :

Wicd

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.