Skip to content

Instantly share code, notes, and snippets.

@arbinish
Created April 19, 2015 18:01
Show Gist options
  • Save arbinish/37b60e56cadbef7a2ebb to your computer and use it in GitHub Desktop.
Save arbinish/37b60e56cadbef7a2ebb to your computer and use it in GitHub Desktop.
Password automation
# coding: utf-8
import pty
import os
import sys
commands = ["some-command", "--with", "options"]
pid, child_fd = pty.fork()
if not pid:
os.execv(commands[0], commands)
# you may use getpass here
password = "secRetPa33w0rd"
while True:
try:
output = os.read(child_fd, 1024).strip()
except OSError:
break
# the following two lines are not really necessary though
sys.stdout.write("output: %s\n" % output)
sys.stdout.flush()
lower = output.lower()
if lower.endswith("password:"):
os.write(child_fd, "%s\n" % password)
elif 'are you sure' in lower:
os.write(child_fd, "y\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment