- In this example I implied that we work as root. If you don't, apply
sudo su or
sudo when needed.
- Your system may use different init system than Systemd, but Cron is pretty universal.
You can simply use
/etc/fstab to pre-define your mount options and whatnot.
USERNAME@HOSTNAME_OR_IP:/REMOTE/DIRECTORY /LOCAL/MOUNTPOINT fuse.sshfs defaults,password_stdin,_netdev 0 0
Keep in mind default mount options are far from perfect.
For example: reconnect is important. see: https://github.com/libfuse/sshfs/issues/101
An example with these options (taken from the Github issue):
email@example.com:/content/ /mnt/srv fuse password_stdin,defaults,user,allow_other,reconnect,delay_connect,ConnectTimeout=5,ServerAliveInterval=5,IdentityFile=/root/.ssh/id_rsa_storage 0 0
Once that's done, you need a simple script with this sole content, such as:
echo "passwordgoeshere" | mount /mnt/srv
Let's save it under your root user, so an example:
Now you need to make it executable:
chown +x /root/mount_sshfs.sh
Now all you need is just
systemd to execute this on mount.
With cron, a simple entry like this works:
1) You have to create the script. See just above.
2) You have to create a new Systemd service script.
An example would be:
3) The contents of the file:
- I did not test the Systemd method as I try not to rely on it as much as possible. It's personal dislike/dislike/hate. :)
- With Cron, you may need to add a "sleep" to the script, so it doesn't try to run the script "too early", ie.: before internet/network comes up.
Find the UUID of your connection using
Note that this lists not just physical connections but also the Wireless connections defined (SSIDs).
Put some simple script like this in your
Don't forget to set the right permissions to make it excutable (i.e.
chmod +x trigger-sshfs-on-vpn.sh). It can be any type of script, a Bash script is probably sufficient for your purpose.
NetworkManager just executes all the scripts in this directory providing some environment variables you can use for scripting. In this case you probably just need