Raf
Raf

Reputation: 1757

How to set a password variable via a script for mounting a cifs share

I have the following bash script to mount a couple of shared directories in a NAS drive:

sudo mount -t cifs //server/dir1 /mnt/nas/dir1 -o username=raf
sudo mount -t cifs //server/dir2 /mnt/nas/dir2 -o username=raf
sudo mount -t cifs //server/dir3 /mnt/nas/dir3 -o username=raf

Each of these mount commands will ask for a password. I want to avoid having to enter the same password multiple times.

From mount.cifs manual, it says that it can use the variable PASSWD for the password.

That's where my bash skills fail me: how can I update the script to ask and set the PASSWD variable, call the mount commands, and finally unset the PASSWD variable?

So far I could go as far as reading something without echoing back to screen using

read -s PASSWD

But I'm not sure how to incorporate that into the script.

Note 1: The mount argument -o password=password is a no go for me. I don't want hard coded passwords in a text file.

Note 2: Similarly, I'd rather not go for the mount argument credentials=filename

Upvotes: 4

Views: 5570

Answers (2)

user2519486
user2519486

Reputation: 53

I wanted something similar and ended up writing a python utility called 'cifscloak'

With cifscloak, the password is stored encrypted in a sqlite database.

Really simple to use:

sudo cifsfs addmount -n films -s myfilms -m /mnt/films -i myfileserver -o "ro" -u cifsuser

sudo cifsfs addmount -n games -s mygames -m /mnt/games -i myfileserver -u cifsuser

sudo cifsfs mount -a

To mount the filesystem at boot, cifscloak can be used to create a systemd file:

cifscloak systemdfile -a

Take a look, see if it works for you:

https://github.com/sudoofus/cifscloak

pip install cifscloak

Upvotes: 1

apatniv
apatniv

Reputation: 1856

Most likely the variable is not seen by child process. You can export the PASSWD using export

read -s PASSWD
export PASSWD

Note: This makes it available to all the child process. For your purposes, what you need is simply export the variable only to concerned child process.

sudo PASSWD="$PASSWD" mount -t cifs //server/dir3 /mnt/nas/dir3 -o username=raf

Upvotes: 3

Related Questions