Bash Script to Mount NAS - BASH Help Required!

Discussion in 'Linux' started by scuzzphut, Nov 5, 2008.

  1. scuzzphut

    scuzzphut

    Joined:
    Oct 30, 2008
    Messages:
    10
    Likes Received:
    0
    Hello all.

    Instead of having to do ...
    Code:
    sudo mount -t cifs //x.x.x.x/public  NetworkFolder
    ...every time I boot , I wanted to create a bash script which I can double click to run.

    So I created a file , thusly .

    Code:
    #!/bin/bash
    sudo mount -t cifs //x,x,x,x/public NetworkFolder
    echo "Network Drive Connected"
    I saved this a NetConnect.sh and did a

    Code:
    chmod +x NetConnect.sh
    NOW

    If I double click the file in Thunar, it doesn't work

    I discovered why, when I opened a terminal and did this
    Code:
    bash NetConnect.sh
    Because I have to sudo to use mount, the bash script asks for a password when run from the terminal. I am guessing that when I double click to run, the lack of password causes a fail.


    Does anyone know a work-around to this ? Can I pass the password to sudo somehow in the bash script ?

    As you can see, I've made a fair bit of headway through google and these forums, but the last bit is evading me.

    Please help!

    Thanks in advance.

    Mark.
     
    scuzzphut, Nov 5, 2008
    #1
  2. scuzzphut

    scottro

    Joined:
    Aug 31, 2008
    Messages:
    347
    Likes Received:
    0
    A few different ways.
    If you want to run each time it boots, put the commands (without sudo) in /etc/rc.local.

    Otherwise, add a line to /etc/sudoers. Are you running this on Linpus? Usually it doesn't need a password.
    However, assuming the user name is user, add this to the END of /etc/sudoers. (With sudo, last command wins, so if you have something like wheel group can do everything, and add a no password thing above it, it won't work--it processes in order, so that the order is important.

    Check where mount is--it's probably bin.

    Do
    which mount
    You should get back /bin/mount
    If it comes back with something else, like /usr/bin/mount, use that.
    Anway, at the end of sudoers
    user ALL= NOPASSWD: /bin/mount
     
    scottro, Nov 5, 2008
    #2
  3. scuzzphut

    rbil

    Joined:
    Aug 14, 2008
    Messages:
    730
    Likes Received:
    0
    Location:
    The Wet Coast, Canada
    scuzzphut,

    Consider using SUID. Read about it here ...

    http://www.newlinuxuser.com/explain-wha ... nd-setgid/

    Then you shouldn't need to use "sudo" within the script.

    As to running it in rc.local, I doubt it would work as network connections are made way later in the process.

    Cheers.
     
    rbil, Nov 5, 2008
    #3
  4. scuzzphut

    scottro

    Joined:
    Aug 31, 2008
    Messages:
    347
    Likes Received:
    0
    That's odd--Linpus runs rc.local before the network?

    Fedora runs rc.local after everything else, however, if what you say is the case for the OP's situation, then, OP, forget that suggestion of mine. :)
     
    scottro, Nov 5, 2008
    #4
  5. scuzzphut

    cpchan

    Joined:
    Sep 22, 2008
    Messages:
    201
    Likes Received:
    0
    This is because Linpus doesn't really use the standard SysV init system (most of the services starts after login. To to OP: the correct place to put it in is /etc/rc.d/slim/nowait.sh after the line that starts the network.
     
    cpchan, Nov 5, 2008
    #5
  6. scuzzphut

    scuzzphut

    Joined:
    Oct 30, 2008
    Messages:
    10
    Likes Received:
    0
    thanks for the info and suggestions, guys - I'll report back once I get it working :)
     
    scuzzphut, Nov 6, 2008
    #6
  7. scuzzphut

    scuzzphut

    Joined:
    Oct 30, 2008
    Messages:
    10
    Likes Received:
    0
    ok - SUID didn't seem to work.

    I removed sudo from the "sudo mount..." line and did the chmod to set the SUID on my script.
    Unfortunately, running the script returns a "mount.... only su can do that" type message.

    So I added sudo back into the script and it still asked me for a password when I ran it, eve with the SUID set.



    So... on to option 2
     
    scuzzphut, Nov 6, 2008
    #7
  8. scuzzphut

    rbil

    Joined:
    Aug 14, 2008
    Messages:
    730
    Likes Received:
    0
    Location:
    The Wet Coast, Canada
    Just curious, after you did the chmod to set the SUID bit, was the script owned by root? One way I can think of doing this is to turn the terminal over to root, by: sudo su -
    and then running chmod on the file.

    You'll probably have success autostarting the commandline itself in nowait.sh after it has started the networking.

    Cheers.
     
    rbil, Nov 6, 2008
    #8
  9. scuzzphut

    scuzzphut

    Joined:
    Oct 30, 2008
    Messages:
    10
    Likes Received:
    0
    No joy with either, unfortunately

    No matter what I do with the shell script, sudo wants a password when run.


    As for putting the line in nowait.sh - this works when I'm connected via ethernet, but not wi-fi. I realise that this is because the wifi connection isn't started until the UI is up, so not much I can do.

    It would just be nice to have an icon I could click to connect me to my NAS :cry:

    Will keep pluggin away = any more ideas gratefully received and thanks so much to you guys who have pitched in so far.
     
    scuzzphut, Nov 6, 2008
    #9
  10. scuzzphut

    rbil

    Joined:
    Aug 14, 2008
    Messages:
    730
    Likes Received:
    0
    Location:
    The Wet Coast, Canada
    Well, you could create a .desktop file which contains your exec= command, I'd think. If you want it on your Linpus desktop, you'll need to assign some sort of icon to it. You can use an existing .desktop file to get an idea how to write a new one for your command.

    Cheers.
     
    rbil, Nov 6, 2008
    #10
  11. scuzzphut

    cpchan

    Joined:
    Sep 22, 2008
    Messages:
    201
    Likes Received:
    0
    OK, I forgot about wireless networking. Try putting the command at the end of /etc/sysconfig/networking-scripts/ifup-wireless
     
    cpchan, Nov 6, 2008
    #11
  12. scuzzphut

    pierreforget

    Joined:
    Dec 21, 2008
    Messages:
    4
    Likes Received:
    0
    Hi,

    I use a bash script (called mountdirectory):

    #!/bin/bash
    mount -t cifs //ipaddressofshare/directorytobeshared /mnt/directory -ousername=yourusername,password=yourpassword

    Note the -o before username, it has to be there.

    I have to be root to use it, so, in terminal I get to be root with:

    su -

    then type my password

    You could use sudo, but what the heck: if your are to be root, let it be root all the way! But be careful, root means you are God! So make no mistake...

    I am quite sure that calling this script from rc.local should work, because rc.local work as root.

    Don't forget to make your script executable with CHMOD 755

    Have fun,

    Pierre Forget
     
    pierreforget, Dec 28, 2008
    #12
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.