DeweyOx
DeweyOx

Reputation: 719

Automate a Mount of a Remote Directory in Bash Using sshfs and expect

I am able to mount a remote SFTP directory using the root user, however when I try to run a bash script to automate the same, the script runs with no errors, however the mount is never created.

I'm using:

#!/bin/bash

_SSHHOST="sub.domain.com"
_SSHUSER="10001"
_SSHPASS="somepass"
_DATADIR="/mnt/data"

# Create user if doesn't exist
id -u $_SSHUSER &>/dev/null || useradd $_SSHUSER 
# Create directory if doesn't exist
mkdir -p $_DATADIR/$_SSHUSER
# Change ownership of dir to new user
chown $_SSHUSER:root $_DATADIR/$_SSHUSER

/usr/bin/expect <<EOD

spawn sshfs $_SSHUSER@$_SSHHOST:/remote/dir $_DATADIR/$_SSHUSER
expect "Password: "
send "$_SSHPASS\r"
expect "#"

EOD

No mount is created and there are no files listed in the created directory. I appreciate that some people will say to avoid using Bash and create this directly in Expect, however this is just a very small portion of a larger script that performs numerous bash functions. What am I doing incorrectly?

Thanks for your help.

UPDATE I fixed this by passing -o password_stdin into the sshfs command. Thanks everyone for looking.

Upvotes: 2

Views: 5076

Answers (2)

DeweyOx
DeweyOx

Reputation: 719

I fixed this by passing the -o password_stdin flag into the sshfs command. Thanks everyone for looking.

Upvotes: 0

mechinn
mechinn

Reputation: 46

So I think the problem you're running into is expect only works on the output from the program you're using it on

In this case sshfs prompts for:

$_SSHUSER@$_SSHHOST's password:

Once you normally would type in the password it would fork into the background and there would not be a # to expect to get.

What I think you're seeing is the # on the next line of your terminal after the sshfs tool forks so you can't expect that

As an alternative to an expect script sshfs accepts the password via stdin with

echo $_SSHPASS | sshfs -o password_stdin $_SSHUSER@$_SSHHOST:/remote/dir $_DATADIR/$_SSHUSER

Upvotes: 2

Related Questions