2017-10-31 21 views
0

pexpect를 사용하여 원격 시스템을 제어하는 ​​매우 간단한 프로그램을 작성하려고합니다. 그러나 원격 시스템은 전송 된 명령에 반응하지 않습니다. , 지금까지의 내가 이해, 명령이 성공적으로 실행되는시스템이 pexpect 명령에 응답하지 않음

0 
10 

그래서하지만 대상 시스템, 즉 foo는 아무런 결과가 없습니다 :

여기
#!/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) 

이 출력됩니다 : 여기

는 소스 코드 파일이 작성되지 않았습니다.

아무도 도와 줄 수 있습니까?

답변

1

pexpect.spawn() 뒤에 다음 행을 추가하면 아무 것도 표시되지 않습니다.

# for Python 2 
child.logfile_read = sys.stdout 

# for Python 3 
child.logfile_read = sys.stdout.buffer 
또한 (그렇지 않으면 스크립트가 즉시 종료 후 sendline('touch foo') 그래서 touch foo을 실행할 수있는 기회가없는 것) 끝에 다음 문을 필요

: 매뉴얼에 따르면

child.sendline('exit') 
child.expect(pexpect.EOF) 

을 :

The logfile_read and logfile_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 
+0

감사합니다! 'child.sendline ('exit')'과'child.expect (pexpect.EOF)'가 작동했습니다. 'child.logfile_read = sys.stdout'은 어떨까요? 'TypeError : write() 인수는 바이트가 아닌 str이어야합니다.'하지만 작동하더라도 아무런 이익도 보지 않습니다. 이를 무시하고 파일 끝과 관련된 권장 사항 만 사용할 수 있습니다. –

+1

이 이것을 발견했습니다 : https://stackoverflow.com/questions/35330424/expect-in-python3-is-throwing-error-as-must-be-in-str-not-bytes. 내 대답이 업데이트되었습니다. – pynexj