1

My work requires me to connect to several network drives over two different protocols, SMB and SSHFS. I got tired of typing in the commands to connect to them individually and being prompted for my password every time, so I wrote this script:

#!/bin/sh

# SSHFS shares

local_paths=("/Users/$USER/mnt/share_1" "/Users/$USER/mnt/share_2" "/Users/$USER/mnt/share_3")
remote_paths=("$USER@server.university.edu:/home/$USER" "$USER@server.university.edu:/some/path" "$USER@server.university.edu:/another/path")

echo "Enter password:"
read -s password
for i in "${!local_paths[@]}"; do
    diskutil unmount ${local_paths[$i]}
    echo "Mounting ${remote_paths[$i]} to ${local_paths[$i]}"
    mkdir -p ${local_paths[$i]}
    sshfs -o password_stdin ${remote_paths[$i]} ${local_paths[$i]} -o volname=$(basename ${local_paths[$i]}) <<< $password
    echo
done

# SMB shares

local_paths=("/Users/$USER/mnt/share_4" "/Users/$USER/mnt/share_4")
remote_paths=("//$USER@different.server.university.edu:/home/$USER" "//$USER@different.server.university.edu:/some/path")

for i in "${!local_paths[@]}"; do
    diskutil unmount ${local_paths[$i]}
    echo "Mounting ${remote_paths[$i]} to ${local_paths[$i]}"
    mkdir -p ${local_paths[$i]}
    mount_smbfs ${remote_paths[$i]} ${local_paths[$i]}
done

It just loops through every path and disconnects/reconnects. It mostly works. After running it, I gain access to four of the five drives. For some reason, the last SSHFS in the array will mount, but I get a "permission denied" error message when I try to open the folder where it is mounted. If I re-order the array, it is always the last path that will error out like this. I have no such issue with the SMB shares.

Once this error happens, my computer is bugged out. Trying to forcibly unmount the share will just freeze my terminal. I lose all ability to access websites or do anything else that uses a network connection. I can't even restart the computer without holding down the power button for a hard reset.

Technical Specs:
Intel MacBook Pro
MacOS Big Sur
zsh, but I've tried this script in bash and sh with the same result.

Notes:
I tested this on a colleague's laptop and got the same results.

  • 1
    Just `sshfs $USER@server.university.edu:/` why do you want so many connections, just do one. – KamilCuk Jan 19 '22 at 18:16
  • @KamilCuk Because sometimes I want that many connections. If you aren't going to help, then don't bother commenting. – Johnny Rocketfingers Jan 19 '22 at 18:18
  • @JohnnyRocketfingers Welcome on SO, I think KamilCuk's question is pertinent! Having only one connectopm couiöd do the jpb. Il no see `ControlMaster` in ssh doc. – F. Hauri - Give Up GitHub Jan 19 '22 at 18:25
  • Not having the `$` expansions in the sshfs command in double quotes looks weird and might have strange effects depending on what your real paths are. To see what is really going on, add `set -x` before the first loop and see which sshfs commands fall out. – Harald Jan 19 '22 at 18:33
  • @Harald I was able to confirm that the paths are being constructed corrected. I even tried it with hard-coded paths and saw the same result. I think it has something do with how I am passing in the password. According to [the manual](https://man.archlinux.org/man/sshfs.1),the password_stdin option is "only for pam_mount", whatever that means – Johnny Rocketfingers Jan 19 '22 at 20:02

0 Answers0