Created
October 29, 2013 07:14
-
-
Save petri/7210268 to your computer and use it in GitHub Desktop.
Some programs such as ssh ask for password and key passphrase via tty, which is not supported by subprocess.Popen. This example illustrates use of pty.fork() to support communications with a child process via a tty (a pty, actually).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Run git clone using pki key with passphrase." | |
import pty, os, sys, select | |
def waitfor(fd, str): | |
"poll the child for input" | |
poll = select.poll() | |
poll.register(fd, select.POLLIN) | |
while True: | |
evt = poll.poll() | |
if str in os.read(fd, 1024): | |
return | |
if __name__=="__main__": | |
# run this from shell with git url,target path & key passphrase | |
giturl, path, passphrase = sys.argv[1:4] | |
pid, fd = pty.fork() | |
# executed in child | |
if pid == 0: | |
os.execvp("git", ["git", "clone", giturl, path]) | |
# executed in parent | |
elif pid > 0: | |
waitfor(fd, "Enter passphrase") | |
os.write(fd, passphrase + "\n") | |
waitfor(fd, "Resolving deltas: 100%") | |
os.wait() | |
os.close(fd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment