Skip to content Skip to sidebar Skip to footer

Running An Interactive Shell Script In Python

I have a shell script which I execute manually like below First it prints some message and then at the end asks for username: >/x/y/somescript 'a b c' Please enter the credenti

Solution 1:

Your function may return prematurely before pexpect has read all the output from the child process. You need to add child.expect(pexpect.EOF) so that all the output (perhaps until some buffer is full) is read.

A simpler option is to use pexpect.run() function:

output, status = pexpect.runu(command, withexitstatus=1,
                              events={'Username:': 'administrator\n',
                                      'Password:': 'Testpassw0rd\n'})

There might be issues if the command generates large output or takes too long (timeout parameter).

Post a Comment for "Running An Interactive Shell Script In Python"