Home » Uncategorized » Send password to terminal on Mac

Send password to terminal on Mac

From time to time I need to run some curl or wget call from terminal that requires authentication. Something like this:

curl --anyauth -u "username:pass" "url"

The problem obviously is that typing passwords in plaintext in terminal is not a very good idea. For once they’ll get stored in history.

The quick and crude solution I’ve came up with was to use OS X ability to run AppleScript directly from terminal using osascript -e 'script here' syntax. So why not run AppleScript that would display input dialog and return the text user has entered into it?

Here’s the simple way to get user input from call to osascript:
osascript -e 'return text returned of (display dialog "Input text" default answer "")'

Naturally, for passwords we’d like to hide the text we type in. And also we need to put the result of the command into curl parameters. The final result is this:

curl --anyauth -u "username:$(osascript -e 'return text returned of (display dialog "Input text" default answer "" with hidden answer)')" "url"

Final note: naturally, you can remove “username:” part from command and type it into the dialog instead if you’re also concerned about usernames ending up in terminal in plaintext.

That’s it. Hope that helps someone like it helped me.

P.S. Couple of useful aliases for the command

alias inputp='osascript -e "return text returned of (display dialog \"Input text\" default answer \"\" with hidden answer)"'
alias inputt='osascript -e "return text returned of (display dialog \"Input text\" default answer \"\")"'
Advertisements

One thought on “Send password to terminal on Mac

  1. Thanks for sharing interesting experience! Under Ubuntu you can just type space before command in terminal in order not to store it in history (Tried on Mac – it does not work :( ).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s