60

I often mount a remote drive using sshfs. I would love to have this mount automatically. However, if I mount it in my .bashrc or startup scripts, the mount often fails because my wireless internet connection has not yet been made.

Does anybody know of an easy way to execute a bash scrip every time the connection is made, or to auto-mount an sshfs drive?

Jacob Vlijm
  • 83,767

6 Answers6

70

Add your script to /etc/network/if-up.d/. Name your script without a '.sh' extension.

Also after adding script run chmod +x /etc/network/if-up.d/yourscriptname to give necessary permission.

Make sure the network interface (e.g. wlan0 or eth0) that should trigger your script when turned on is mentioned in the file /etc/network/interfaces.

dedunu
  • 9,186
  • 16
    inside scripts in /etc/network/*.d/ you can use the variable $IFACE to know which interface is getting a connection so you can skip the procedure if it is the wired connection for example. – Carlos Campderrós Feb 20 '13 at 09:22
  • Is there a way to specify which specific network you want to do this for? when it's my home network I want to do the script but when I'ts a network out and about I might not want to... – Joshua Robison Apr 28 '13 at 09:21
  • 4
    this actually doesn't seem to work at all. I know my scripts work because I can run them using ./ in the terminal and they work but when I toggle off and on the connection, the scripts are not called at all. Is there anything that I have to do to reference them in someway? dropping a script in if-up.d seems to be lacking a step?!?! – Joshua Robison Apr 28 '13 at 10:30
  • 1
    check whether they hace correct permissions please. – dedunu Jul 02 '13 at 05:47
  • 3
    Please add what exactly should go into interfaces when it is not there. Is it auto wlan0 and iface wlan0 inet dhcp or sth else? – Cookie Mar 25 '14 at 15:40
  • I want to know whether it's enough to just have auto lo iface lo inet loopback in my /etc/network/interfaces file. I want my script to be executed after connecting with eth0. – Exeleration-G Mar 26 '14 at 11:35
  • This link would be helpfull to you I think http://www.cyberciti.biz/tips/how-do-i-run-firewall-script-as-soon-as-eth0-interface-brings-up.html – dedunu Mar 27 '14 at 05:03
  • 2
    More on Ubuntu wiki: https://wiki.ubuntu.com/OnNetworkConnectionRunScript – Quentin Dec 21 '17 at 20:58
  • As far as I'm concerned, if I add to /etc/network/interfaces auto wlp58s0 iface wlp58s0 inet dhcp post-up /home/augustin/Config/auto-checkin.sh the wifi doesn't start after reboot... :( – Augustin Riedinger Feb 28 '18 at 11:20
  • From what I can tell this answer is no longer applicable as of Ubuntu 17.10 due to the deprecation of NETWORKING and it's replacement, NETPLAN. I wonder how this answer can be updated to work with Netplan? – hoatzin Aug 17 '22 at 14:57
25

Editor's note: Cuttlefish hasn't been updated since 2012 and is not in any current Ubuntu repositories

Ubuntu 12.04

  • Install Cuttlefish: A simple tool, which realises reflexes on your computer by executing actions when specific events are triggered.
  • Open Cuttlefish and click on New.

    Step-1

    • Give it a name, and change Activated by stimulus option to ON.
    • Click on Stimulus, select the category Network and type Connect to WLAN. Click OK.

    Step 2

    • You can also select the SSID of wireless network and you have an option of whether or not to check this on startup.

    Step 3

    • Now you will have to specify a reaction. Go to Reaction tab, and click add. Select Applications from the category and Start Application (in advanced mode) from type.

    Step 4

    • Now specify the script which you want to run in executable option. You can even pass parameters to the script.

    Step-5

    • And that's it! Also make sure that you add Cuttlefish in startup applications. Go to Edit > Preferences and enable the autostart option.

    Step-6

Zanna
  • 70,465
ignite
  • 8,936
  • 7
    cuttlefish is completely broken and hasn't seen an update for sometime. I wish it could get more development because we need something like Android's Tasker app for Linux. Nobody wants to mess with etc/network/interfaces and if-up folders and scripts. Just give me something I can click or toggle on and off. – Joshua Robison Jan 19 '14 at 04:17
4

As far as I'm concerned, the /etc/network/if-up.d/ solution did not work from me as when I added to /etc/network/interfaces the following:

auto wlp58s0
iface wlp58s0 inet dhcp
post-up /home/augustin/Config/myscript.sh

Wifi would start after reboot.

But after some struggle, from this link, what worked was to add the script to /etc/NetworkManager/dispatcher.d/ in the form 90myscript.sh where 90 is the priority level of the script and with the following form:

#!/bin/bash

IF=$1 STATUS=$2

if [ "$IF" == "wlp58s0" ] then case "$2" in up) # interface is up ;; down) # interface will be down ;; pre-up) # interface will be up ;; post-down) # interface is down ;; *) ;; esac fi

I guess there are issues between who controls the network config at system level and sometimes the default network behavior is left behind the dispatcher one.

Also, for those who would like - like me - to try to have a symbolic link to the script (to store the file in a better location), unfortunately it did not work for me.

Hope this helps!

4

I will answer this part of the OP question: "I would love to have this mount automatically"

My favorite tool for automounting network shares is autofs: https://help.ubuntu.com/community/Autofs

AutoFS is my tool of choice to mount my NAS - for all my laptops and workstations.

"autofs is a program for automatically mounting directories on an as-needed basis. Auto-mounts are mounted only as they are accessed, and are unmounted after a period of inactivity. Because of this, automounting NFS/Samba shares conserves bandwidth and offers better overall performance compared to static mounts via fstab."

RBell
  • 321
  • 3
  • 6
  • Your answer while possible may be a bit late as the OP already accepted the other answer. Your help is of course appreciated, however you might be better served by focusing your efforts on the unanswered questions – Wayne_Yux Jun 24 '16 at 07:51
  • Sorry Wayne, RBell got the last laugh here -- the best answer (about using Cuttlefish) is no longer relevant because that package is outdated -- but autofs still exists, and I hadn't seen it if not for this post. Thanks, RBell! – KlaymenDK Sep 09 '19 at 18:39
2

systemd seems to have taken over networking nowadays.

$ networkctl
WARNING: systemd-networkd is not running, output will be incomplete.

IDX LINK             TYPE               OPERATIONAL SETUP
  1 lo               loopback           n/a         unmanaged
  2 enp0s25          ether              n/a         unmanaged
  3 wlo1             wlan               n/a         unmanaged

3 links listed.

It also has units corresponding to the interfaces

$ systemctl list-units |grep wlo1
sys-devices-pci0000:00-0000:00:1c.3-0000:24:00.0-net-wlo1.device                                                        loaded active plugged   Centrino Ultimate-N 6300 (3x3 AGN)
sys-subsystem-net-devices-wlo1.device                                                                                   loaded active plugged   Centrino Ultimate-N 6300 (3x3 AGN)

Apparently you can use these in service unit files. So just create a service unit to run your script and use something like that inside to make it run conditionally on the network:

BindsTo=sys-subsystem-net-devices-wlo1.device
After=sys-subsystem-net-devices-wlo1.device

Good luck! I haven't tested this, so "your mileage may vary".

Sources:

Rolf
  • 2,031
1

The best, simple and easy way would be to add an up command to what ever interface on which you want work on.

Interfaces can be found in the "/etc/network/" directory under the file name "interfaces"

iface elan0 inet manual
    up filename.sh

This will add the up command to elan0. Simillarly give this to what ever interface you want to work on. Remember to indent one tab space.

Make sure you give the execute permissions to the file.

sudo chmod 755 filename.sh