1 # encoding: utf-8 2 3 import os 4 import sys 5 import time 6 7 8 def child(master, slave): 9 os.close(master)10 os.dup2(slave, 0)11 os.dup2(slave, 1)12 os.dup2(slave, 2)13 os.execvp("/bin/bash", ["bash", "-l", "-i"])14 15 16 def parent():17 master, slave = os.openpty()18 new_pid = os.fork()19 if new_pid == 0:20 child(master, slave)21 22 time.sleep(1)23 os.close(slave)24 25 os.write(master, "fg\n")26 time.sleep(1)27 _ = os.read(master, 1024)28 29 30 os.write(master, sys.argv[1] + "\n")31 time.sleep(1)32 lines = []33 while True:34 tmp = os.read(master, 1024)35 lines.append(tmp)36 if len(tmp) < 1024:37 break38 output = "".join(lines)39 output = "\n".join(output.splitlines()[1:])40 print output41 42 parent()