How to automate typing password on a shell script

Hi All,
I am logged on my Mac as User1.
I open Terminal and I type:
su User2 -c “java -jar myapp.jar”

Terminal window rightly ask me the password for user User2.

I want to avoid to type the password directly into Terminal windows but I’d like that my Apple Script
show me a small dialog (done with Apple Script) where I can type my password as like as into Terminal
window.

Is it possible ?

Thanks in advance to all!!!

G.

If I understand well your question, the Standard Additions command display dialog may be your friend .
It’s described as :

display dialog‚v : Display a dialog box, optionally requesting user input
display dialog text : the text to display in the dialog box
[default answer text] : the default editable text
[hidden answer boolean] : Should editable text be displayed as bullets? (default is false)
[buttons list of text] : a list of up to three button names
[default button text or integer] : the name or number of the default button
[cancel button text or integer] : the name or number of the cancel button
[with title text] : the dialog window title
[with icon text or integer] : the resource name or ID of the icon to display.
[with icon stop/Œnote/Œcaution] : .or one of these system icons.
[with icon file] : .or an alias or file reference to a ˜.icns’ file
[giving up after integer] : number of seconds to wait before automatically dismissing the dialog
→ dialog reply : a record containing the button clicked and text entered (if any)

Yvan KOENIG (VALLAURIS, France) mercredi 15 octobre 2014 17:43:23

What kind of system are you running, my macs (10.8 and 10.9) don’t support the option -c.

Assuming that -c stands for executing a single command as another user and then stop. Normally when you have no tty (like in a do shell script) but the command requires it you can use a expect script. This way you still can use an shell without a terminal and anticipate to it’s values. I use expect command line utility to automate the ssh login stepsm which always prompts for a password as well.

Hello DJ.

I’m running 10.9.5.
Altough the optional parameter -c is not listed among accepted ones, in the man page of the su command I may see :

EXAMPLES
su man -c catman
Runs the command catman as user man. You will be asked for man’s
password unless your real UID is 0.
su man -c ‘catman /usr/share/man /usr/local/man’
Same as above, but the target command consists of more than a sin-
gle word and hence is quoted for use with the -c option being
passed to the shell. (Most shells expect the argument to -c to be
a single word).

su -l foo
Simulate a login for user foo.
su - foo
Same as above.
su - Simulate a login for root.

Maybe there is just an omitted line in the list of parameters.

Yvan KOENIG (VALLAURIS, France) jeudi 16 octobre 2014 09:41:04

Thanks Yvan, I was typing it wrong and the command said “-- c illegal option”, assuming that -c was actually an illegal option.

Here is the expect script what I meant:

set shellUser to "User2"
set shellPasswd to "password4User2"
set shellCommand to quoted form of "java -jar myapp.jar"

set expectScript to "set timeout 30
spawn su " & shellUser & " -c " & shellCommand & "
expect {
   \"Password:\" {
       send \"" & shellPasswd & "\\r\"
       exp_continue
   }
}"

set theResult to do shell script "expect <<<" & quoted form of expectScript

Hello DJ.

Nice script you got there! I wonder where I find the spawn command, and what exactly it is it does. :slight_smile:

Thanks.

Expect is a scripting language and spawn is one of its commands. Be warned, man expect gives you almost an entire hour to read but expect is very useful for opening and automating a terminal session.

Thank you.

What a nice way to say rtfm. :smiley:

I didn’t realize it was one of expects sub commands, I am sorry about that. I know some about expect, I don’t need it, I think you can describe it as “ui scripting for interactive unix commands”. :slight_smile: I’ll read up on the manual page.

Oh no, not at all. Was just giving friendly pointers. I can’t explain it better than the manual itself :cool:

I think that my post was not clear enough.
I’d like to create a small form with a textbox and an Ok button.
In the textbox I type my password and when I click on the button
I need to transfer my password on a Terminal window waiting for
this string.
Obviuosly all done with apple script…
I hope to be more clear…

Thank you for the patience…

G.

Hello.
I think you’d have a hard time making it work with just applescript. Here is some bits and pieces you may use as a vantage point. You’ll need to fill in the user account, maybe password, and the shell command.

set shellUser to "admin"
set shellPasswd to text returned of (display dialog "Enter password for user admin" default answer "password" with hidden answer)

set shellCommand to quoted form of "whoami"

set expectScript to "set timeout 30
spawn su " & shellUser & " -c " & shellCommand & "
expect {
\"Password:\" {
send \"" & shellPasswd & "\\r\"
exp_continue
}
}"
tell application "Terminal" to do script "expect <<<" & quoted form of expectScript in window 1

Your solution work almost completely…at 99%!!!

I replaced the generic variables with my variables and I obtained a script like this:

#############################################################################

set shellUser to “myUser”
set shellPasswd to text returned of (display dialog “Enter password for user admin” default answer “password” with hidden answer)

set shellCommand to quoted form of “./pathToScript/script.sh”

set expectScript to “set timeout 30
spawn su " & shellUser & " -c " & shellCommand & "
expect {
"Password:" {
send "” & shellPasswd & “\r"
exp_continue
}
}”
tell application “Terminal” to do script “expect <<<” & quoted form of expectScript in window 1

#############################################################################

Inside my shell bash “script.sh” there is a string (java -jar myJavaApp.jar) to launch a java application as user “myUser”

My java application starts correctly but after 10 seconds it closes!!!

I dont know why…

Gianni

Hello.

I admit to not having read the expect manual page (man expect), but the first thing to test for is if it is the time out parameter of expect that kicks in; I think that is a good place to start, and I advice you to change “set timeout 30” to set “timeout 28000” which will prevent the expect command from time out in around 8 hours. :slight_smile:

This may not be the cure, but it is a good place to start looking.

Please enclose scripts with the Applescript tags you get by clicking the Applescript button above the edit box. :wink:

Your post was clear, was just showing how to handle it. Using a dialog and running it in terminal instead of do shell are just minor differences.

That is expect’s default timeout, timeout doesn’t work.

You can turn the timeout off by setting it to -1 for testing. Since the script stops at 10 seconds (default timeout) it seems that the timeout setter doesn’t work at all in kasketto’s case.

If the command needs to be running for a long(er) time, i wouldn’t increase the timeout. I would rather run the command in the background then. You can redirect the output to a file and open it with the console.app or using tail -f if you would like to see live the output of the script.

Hello.

I can think of a couple of reasons why the timeout doesn’t work in this case: the assumption is that timeout doesn’t work, as long as expect didn’t find a process to control in the first place. Here we use su, which means that expect is bereft of any control what so ever, of the process that it initiates (my idea for setting a high value), disabling timeout all together seems to be a much better idea anyways. :slight_smile:

Expect doesn’t control a process, what it does (in our example) is opening a shell read out the stdout and when a certain match is made it will send data to stdin of that shell (which will be send to the process eventually). When using su or any other subshell doesn’t really matter, I don’t have the problem kasketto mention on my own machine so therefor it’s hard to understand what goes wrong.

Hello DJ.

I thought the timeout parameter specified the amount of idleness exepect would allow the calling process to have. I now understand that timeout is to make a process die after a specified amount of time.

This is getting over my head right now, but the manual, at least is well written. :slight_smile: