Wednesday, September 28, 2016

Autorun windows share mount script after login as root


I have looked everywhere for a solution that lets me run CIFS automount script after login with around 20 second delay.
What I have tried.
Added the script to visudo:


rait ALL=(ALL) NOPASSWD: /home/rait/Downloads/Testing/mount.sh, /bin/mount

After that added the script to Startup applications, but it does not work.
Also I tried adding it to .bash_profile and rc.d. In rc.d it works but that does not run it after login. I need it after login because it will ask people for their AD user and password via GUI.
The script itself looks like this:


    #!/bin/bash
MOUNTDIR=Public
DIRNAME=Avalik
DOMAIN=Domain
SERVER=server_adr
SHARE=shares
if [ ! -d ${HOME}/Documents/${DIRNAME} ]; then
mkdir ${HOME}/Documents/${DIRNAME}
fi
# get the windows username
wUsername=`zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Kasutaja
nimi:"`
if [ $? -ne 0 ]; then
exit 1
fi
# get the windows password
wPassword=`zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Parool:"
--hide-text`
if [ $? -ne 0 ]; then
exit 1
fi
mount -t cifs //$SERVER/$SHARE ${HOME}/Documents/${DIRNAME} -o username=${wUsern
ame},password=${wPassword},domain=${DOMAIN}
if [ $? -eq 0 ]; then
zenity --info --title="Mount OK!" --text="Mount OK!"
else
zenity --error --title="Mount Failed!" --text="Mount Failed!"
fi

I hope that someone knows how to do it.


PS: After I edited the visudo now it asks for root password after I open terminal. So terminal asks me for sudo password when ever I open it after login. Once I remove the line that should run the script in visudo it does not ask for password anymore.


Please help :)



Fixed it by adding this to sudoers:


%sudo   ALL=(ALL) NOPASSWD: /bin/mount, /bin/umount

And the Startup Applications command was edited so it was /home/user/script/path/script.sh and it started to work. Also added sudo infront of the mount command in the script as well.


Thanks for you help guys :)


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...