Skip to content

Instantly share code, notes, and snippets.

@mtik00
Last active June 13, 2023 18:21
Show Gist options
  • Save mtik00/96f614b09344de5831cd0ebfcdc6dfb4 to your computer and use it in GitHub Desktop.
Save mtik00/96f614b09344de5831cd0ebfcdc6dfb4 to your computer and use it in GitHub Desktop.
read password from stdin
# You can check to see if someone ran your script with `echo -n test | myscript.py`.
# In that case, there's no tty attached. If there _is_ an attached tty, then you
# can just prompt the user like normal.
import sys
def read_secret_key():
if sys.stdin.isatty():
return getpass("Enter the secret key: ")
else:
return sys.stdin.readline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment