Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top