Set up SSHFS automated mounting only when user SSH into remote server?

3

i want to set up SSHFS such that whenever I SSH into remote server, it mounts the file transfer folder. Is it possible to set it up in such way?


making script after @Alex's comment.

my script is very simple.

#! /bin/bash
#login in the server
ssh -X user@0.0.0.0
#establish the automated mounting
sshfs user@0.0.0.0:/home/usr/data /usr/some/place/mountedfolder/ -ovolname=mounted-folder

but now, if i execute this file. first the ssh is established and then only after i exit ssh the folder is mounted with sshfs.

What am i doing wrong here?


okay for now, i just did simple fix :-> switched the order of commands. first the sshfs and then ssh so that : first it mounts and second it establishes ssh connection.

#! /bin/bash
#establish the automated mounting
sshfs user@0.0.0.0:/home/usr/data /usr/some/place/mountedfolder/ -ovolname=mounted-folder
#login in the server
ssh -X user@0.0.0.0

seems like it works for now.

hadi k

Posted 2017-02-15T09:55:44.477

Reputation: 203

Just run SSHFS then ssh in the same shell script. – Alex – 2017-02-15T12:02:07.527

this could be.. i am right now just using the single line on the terminal to ssh. may be yes make a script to do both. thanks for the idea. i feel dumb now. – hadi k – 2017-02-15T12:53:37.673

@Alex : i nearly went full retard, just re-read your comment again and you do point out the order of execution for the commands -.- – hadi k – 2017-02-15T13:39:01.667

2No, you good. As old wisdom says: "The only those who doing nothing never makes mistakes" – Alex – 2017-02-15T17:37:56.730

I think you have to move the corrected(working) part of the script to the answer and accept your own answer, it absolutely Ok with policy of this site and those who would look for the similar solution may find it useful – Alex – 2017-02-16T04:40:18.510

1No problem, I would advise you to add one more line of code - run SSHFS unmount command after ssh. This way if you logout from remote machine, it will automatically unmount SSHFS too, releasing mount point for something else – Alex – 2017-02-16T12:15:21.713

Answers

1

I wrote a simple script following @Alex's advice that first mounts and then ssh to the remote server. This works as wanted.

#! /bin/bash
#establish the automated mounting
sshfs user@0.0.0.0:/home/usr/data /usr/some/place/mountedfolder/ -ovolname=mounted-folder
#login in the server
ssh -X user@0.0.0.0
#unmounting the directory after logout
umount user@0.0.0.0:/home/usr/data  

To make it easier to run the script i just made it executable by following the two steps below :

  1. I wrote a simple script named sshautomatic.sh in /usr/local/bin (I use OSX)
  2. then in /usr/local/bin establish a symlink by : sudo ln -s sshautomatic.sh sshautomatic
  3. finally, make sshautomatic.sh executable by : chmod +x sshautomatic.sh Now, you can establish the fusemounting and ssh connection from terminal by just typing the command sshautomatic

hadi k

Posted 2017-02-15T09:55:44.477

Reputation: 203