import pty, os, select, time, subprocess
def run(argv, feed=None, timeout=10):
    pid, fd = pty.fork()
    if pid == 0:
        os.execvp(argv[0], argv)
    buf=b""; sent=False; start=time.time(); status=None
    while time.time()-start < timeout:
        r,_,_ = select.select([fd],[],[],0.4)
        if r:
            try: d=os.read(fd,1024)
            except OSError: break
            if not d: break
            buf+=d
            if feed and not sent and b"assword" in buf:
                os.write(fd,(feed+"\n").encode()); sent=True
        wpid,ws=os.waitpid(pid,os.WNOHANG)
        if wpid: status=ws; break
    if status is None:
        try: os.kill(pid,9); status=os.waitpid(pid,0)[1]
        except Exception: pass
    return buf.decode(errors="replace"), status

# Test 1: su ubuntu -c id, NO password fed (see if it prompts or succeeds)
out, st = run(["su","ubuntu","-c","id"], feed=None, timeout=8)
print("TEST1 su ubuntu -c id (no feed):", repr(out), "status", st)

# Test 2: id command directly for reference
out2, st2 = run(["id"], feed=None, timeout=4)
print("TEST2 id:", repr(out2))

# Test 3: su ubuntu, feed wrong pw
out3, st3 = run(["su","ubuntu","-c","id"], feed="WRONG", timeout=8)
print("TEST3 su ubuntu -c id (feed WRONG):", repr(out3), "status", st3)
