import pty
import os
import sys
import time
pid, fd = os.forkpty()
if pid == 0:
# Slave
os.execlp("su","su","MYUSERNAME","-c","id")
# Master
print os.read(fd, 1000)
os.write(fd,"MYPASSWORD\n")
time.sleep(1)
print os.read(fd, 1000)
os.waitpid(pid,0)
print "Why have I not seen any output from id?"
1 个解决方案
#1
You are sleeping for too long. Your best bet is to start reading as soon as you can one byte at a time.
你睡得太久了。您最好的选择是尽快开始阅读一个字节。
#!/usr/bin/env python
import os
import sys
pid, fd = os.forkpty()
if pid == 0:
# child
os.execlp("ssh","ssh","hostname","uname")
else:
# parent
print os.read(fd, 1000)
os.write(fd,"password\n")
c = os.read(fd, 1)
while c:
c = os.read(fd, 1)
sys.stdout.write(c)
#1
You are sleeping for too long. Your best bet is to start reading as soon as you can one byte at a time.
你睡得太久了。您最好的选择是尽快开始阅读一个字节。
#!/usr/bin/env python
import os
import sys
pid, fd = os.forkpty()
if pid == 0:
# child
os.execlp("ssh","ssh","hostname","uname")
else:
# parent
print os.read(fd, 1000)
os.write(fd,"password\n")
c = os.read(fd, 1)
while c:
c = os.read(fd, 1)
sys.stdout.write(c)