bashnascifs

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


Solution

  • 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