How to hide a password from a prompted osascript dialogue from a Mac/UNIX Shell Script
Asked Answered
N

1

6

I'm in the final phase of designing a script to automate my Active Directory binding that will be used by multiple people. Because of this, I need to prompt for a user name and password. I've successfully created the prompt, but want to find some way to prevent the password from showing up in the dialog box asking for the password (this will be done remotely, I don't want the password visible).

It can be turned into stars, dots, not show up at all, anything, I just need it NOT to show visually, but still be able to be passed down to the dsconfigad command.

I've tested the script itself and it works and this is the last piece that I need to make it live.

(Excuse any extra comments on here, I've pieced this together from a lot of different sources)

#!/bin/bash

while :; do # Loop until valid input is entered or Cancel is pressed.
    user=$(osascript -e 'Tell application "System Events" to display dialog "Enter the network user name:" default answer ""' -e 'text returned of result' 2>/dev/null)
    if (( $? )); then exit 1; fi  # Abort, if user pressed Cancel.
    user=$(echo -n "$user" | sed 's/^ *//' | sed 's/ *$//')  # Trim leading and trailing whitespace.
    if [[ -z "$user" ]]; then
        # The user left the project name blank.
        osascript -e 'Tell application "System Events" to display alert "You must enter a user name; please try again." as warning' >/dev/null
        # Continue loop to prompt again.
    else
        # Valid input: exit loop and continue.
        break
    fi
done

while :; do # Loop until valid input is entered or Cancel is pressed.
    netpass=$(osascript -e 'Tell application "System Events" to display dialog "Enter the network password:" default answer ""' -e 'text returned of result' 2>/dev/null)
    if (( $? )); then exit 1; fi  # Abort, if user pressed Cancel.
    netpass=$(echo -n "$netpass" | sed 's/^ *//' | sed 's/ *$//')  # Trim leading and trailing whitespace.
    if [[ -z "$netpass" ]]; then
        # The user left the project name blank.
        osascript -e 'Tell application "System Events" to display alert "You must enter a password; please try again." as warning' >/dev/null
        # Continue loop to prompt again.
    else
        # Valid input: exit loop and continue.
            break
        fi
    done

MACNAME=$(scutil --get ComputerName)

sudo dsconfigad -add DOMAIN \
-username $user \
-password $netpass \
-computer $MACNAME \
-mobile disable \
-mobileconfirm disable \
-localhome enable \
-useuncpath enable \
-shell /bin/bash \
-ou OU=Macs,CN=Computers,DC=corp,DC=DOMAIN,DC=net \
-force \
-localpassword LOCALPASS \
-groups "GROUPS"

#sudo rm -- "$0"
Newfashioned answered 29/8, 2014 at 20:20 Comment(0)
T
9

Use with hidden answer. Link:

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW12

osascript -e 'Tell application "System Events" to display dialog "Enter the network password:" with hidden answer default answer ""' -e 'text returned of result' 2>/dev/null
Talebearer answered 29/8, 2014 at 20:36 Comment(2)
I forgot to thank you for this comment. This was exactly what I needed.Newfashioned
I think you put stars in the string to highlight the key phrase **with hidden answer** but you must remove the stars or you get a syntax error.Hendershot

© 2022 - 2024 — McMap. All rights reserved.