문제

I am super new to python and i have a simple question I have this function

def testU(self):
        b = pexpect.spawn('users')
        b.expect('root user2')

and it works fine.. and this function

def testW(self):
    b = pexpect.spawn('whoami')
    b.expect('root')

and it is also fine.

but when i try to have both of them together, i get an error, I think it is because of the way i wrote the new line

def testZ(self):
    b = pexpect.spawn('whoami; users')
    b.expect('root\nroot user2')

so, how should i represent the newline in my string?

thanks in advance

도움이 되었습니까?

해결책

You can not put two commands in a single spawn. You can use a trick:

b = pexpect.spawn("bash -c 'whoami;users'")

To match newlines, you should use:

b.expect('root\r\nroot user2')

Terminal TTY device drivers (dumb, vt100, ANSI, xterm, etc.) all use the CR/LF combination to mark the end of line. UNIX uses just linefeeds to end lines in files, but not when it comes to TTY devices.

You can read more about it from here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top