Reputation: 395
I'm trying to write very simple program which controls remote machine using pexpect. But remote system does not react to sent commands.
Here is source code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pexpect
import sys
child = pexpect.spawn('telnet 192.168.2.81 24')
res = child.expect('/ # ')
print(res)
res = child.sendline('touch foo')
print(res)
Here is output:
0
10
So, as far as I understand, commands are executed successfully but there is no result on target system, i.e. foo file is not created.
Could anybody help me?
Upvotes: 2
Views: 1399
Reputation: 20797
Add the following line after pexpect.spawn()
or you would see nothing.
# for Python 2
child.logfile_read = sys.stdout
# for Python 3
child.logfile_read = sys.stdout.buffer
You also need the following statements at the end (otherwise the script would immediately exit after sendline('touch foo')
so touch foo
does not have a chance to run):
child.sendline('exit')
child.expect(pexpect.EOF)
According to the manual:
The
logfile_read
andlogfile_send
members can be used to separately log the input from the child and output sent to the child. Sometimes you don’t want to see everything you write to the child. You only want to log what the child sends back. For example:child = pexpect.spawn('some_command') child.logfile_read = sys.stdout
Upvotes: 4