Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This is related to question 19245, in that one of the responders answered the question in an awesome way, and very VERY clear to any newbie. Now here is a question that i can't seem to figure.

i wrote a script for starting the vmware firefox plugin (don't worry. i gave that up and now run vBox VERY happily. i left vmware for my servers :) )

I needed to start the plugin as sudo, but i also needed to pass an argument (password) to it, that happen to be the same.

So, if my password was Hello123, the command would be: sudo ./myscript.sh hi other Hello123

running from command line, the script would ask for my sudo password and then run. i wanted to capture THAT password and pass it as well. i also wanted to run graphically, so i tried gksudo, and there is an option -p that returns the password for variable assignment.

well, that was a nightmare, because i would still get prompted for the original sudo: see below

Find UserName

vUser=$USER
Find password (and hopefully enable sudo)

vP=gksudo -p -D somedescriptiontext echo
Execute command

gksudo ./myscript.sh hi $vUser $vP

and i still get prompted twice.

so my question is tri-fold:

  1. is there a variable i can use for the password, just like there is one for user, $USER?

  2. is there a different way i should be assigning the value resulting of the command i have in $vP? i am wondering if executing the way i have it, does it in an uninitiated session and not the current one, since i am getting some addtl warning type errors on some variables blah blah

  3. i tried using Zenity to just capture the text, but then of course, i couldn't pass that value to sudo, so i could only use as a parameter, which puts me back in 2 prompts.

Thanks!

share|improve this question

3 Answers 3

You can assign certain commands to run without having to provide a password with sudo (man visudo). For example, you might have a line like this, if your username is psusi:

%psusi ALL = NOPASSWD: /usr/local/bin/myscript.sh

Use extreme caution with granting all-trusting sudo access.

share|improve this answer
    
That sudoers line will give all members of the group psusi allowance to run that script without typing the password... –  geirha Feb 2 '11 at 8:40

The program you're looking for is called expect. You write a script wherein you tell the program to expect certain prompts (such as password prompts) and then supply some response via stdin, the same as with keyboard input. I've used it in the past to get around annoying password prompts that I couldn't (easily) get rid of.

sudo aptitude install expect
man expect
share|improve this answer

1) No, since that would give away your password to anyone on your system

2) The return value from gksudo is an integer indicating whether it had an error or not. The -p switch has it PRINT the password to its stdout.

3) Why are you even trying to get the password? Skip the first call to gksudo and just use the second.

share|improve this answer
    
Thanks for answering. The reason i need the pwd is because i need it as a param for what i am passing. So let me give you the actual scnario. vmware server in ubuntu, comes with a "viewer" for firefox, that you can start separately (given that vmware doesn't have a client). When you open it, you need to put in your sudo user and pwd, and then you pick the machine to connect. however it also allows you to pass this info from command line, to skip the prompt. So i need to launch it with sudo AND need to pass the pwd as a parameter. That's why i capture it via stdout. makes sense? –  semi-newbie Jan 12 '11 at 17:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.