Passing a password to an Automator application
I have written an Automator application with the following shell script to open an FTP connection on my system:
sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
osascript -e 'tell app "Finder" to display alert "SFTP Opened"'
This works exactly as expected except that the sudo command needs a password. When the same command is run on Terminal, it prompts me for a password but when run as an Automator app, doesn't prompt me for any input. Is there anyway to programmatically pass the password to this sudo within the Automator script? I know this is not a safe practice but I still want to do it as my system is reasonably secure and untouched by anyone other than me.
A "do shell script...with administrator privileges" method is giving the following error:
Solution 1:
AppleScript should let you do this by using with administrator privileges
:
osascript -e 'do shell script "sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist" with administrator privileges'
This doesn't pass the password, but prompts for it with a standard OS X prompt:
You can use this in your Automator workflow in one of the two following ways:
Solution 2:
expect
You could instead use a tool like expect to script the interaction with sudo
. expect
is designed for this situation.
expect - programmed dialogue with interactive programs
The following shell script may be enough for your two commands:
#!/usr/bin/expect -f
spawn sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
send "my-secret-password\n"
AppleScript and with administrator privileges
An alternative approach is to use AppleScript's do shell script command. The command offers a with administrator privileges option. Apple's Technical Note 2065 covers do shell script in detail.
echo
Other common approaches to this problem involve using echo
to pass the password to sudo
with a pipe. This StackOverflow answer discusses this and other related approaches, How to provide password to a command that prompts for one in bash?