简体   繁体   English

从 pexpect sendline 读取输出

[英]reading output from pexpect sendline

I have pexpect working, but I am having problems printing the output back from it.我有 pexpect 工作,但我在打印输出时遇到问题。 In my test script below, it creates the ssh connection, and then sends a sudo su -, then my password, and then sends a line that would require sudo access to do (I have also added p.interact() a few times to make sure it is at root).在我下面的测试脚本中,它创建 ssh 连接,然后发送一个 sudo su -,然后是我的密码,然后发送需要 sudo 访问权限的行(我还添加了几次 p.interact() 以确保它在根目录下)。 The problem I am having, is with returning the output of the commands I run.我遇到的问题是返回我运行的命令的输出。 In the end I am wanting to run some top commands, and some du -h, and other(much more complex) space commands.最后,我想运行一些顶级命令、一些 du -h 和其他(更复杂的)空间命令。 But currently when it tries to print p.before, I get:但是目前当它尝试打印 p.before 时,我得到:

Traceback (most recent call last):
File "./ssh.py", line 37, in <module>
print p.before()
TypeError: 'str' object is not callable

Here is the script I am working from(edited to remove my pass and such)这是我正在使用的脚本(编辑以删除我的通行证等)

#!/usr/bin/env python

import pexpect
import struct, fcntl, os, sys, signal

def sigwinch_passthrough (sig, data):
    # Check for buggy platforms (see pexpect.setwinsize()).
    if 'TIOCGWINSZ' in dir(termios):
        TIOCGWINSZ = termios.TIOCGWINSZ
    else:
        TIOCGWINSZ = 1074295912 # assume
    s = struct.pack ("HHHH", 0, 0, 0, 0)
    a = struct.unpack ('HHHH', fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ , s))
    global global_pexpect_instance
    global_pexpect_instance.setwinsize(a[0],a[1])

ssh_newkey = 'Are you sure you want to continue connecting'
p=pexpect.spawn('ssh user@localhost')
i=p.expect([ssh_newkey,'password:',pexpect.EOF,pexpect.TIMEOUT],1)
if i==0:
    print "I say yes"
    p.sendline('yes')
    i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==1:
    print "I give password",
    p.sendline("mypassword")
elif i==2:
    print "I either got key or connection timeout"
    pass
elif i==3: #timeout
    pass
global global_pexpect_instance
global_pexpect_instance = p
p.sendline("sudo su -")
p.sendline("mypasswd")
p.sendline("mkdir /home/user/test")
print p.before

I am working off of this link: http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/我正在使用此链接: http : //linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/

Any help is much appreciated.任何帮助深表感谢。

EDIT: As Armin Rigo pointed out below.编辑:正如 Armin Rigo 在下面指出的那样。 I was calling to p.before as a function like p.before().我正在调用 p.before 作为像 p.before() 这样的函数。 Stupid mistake on my part, as this explains why I was getting this error today, and not yesterday when I was trying this.我犯了一个愚蠢的错误,因为这解释了为什么我今天遇到这个错误,而不是昨天我尝试这个时。 After making that change to my script, and modifying the command being sent, print p.before, and no output is returned.在对我的脚本进行更改并修改正在发送的命令后,打印 p.before,并且不会返回任何输出。 Any other ways to return output from a sendline() command?还有其他方法可以从 sendline() 命令返回输出吗?

Use logfile, that logfile is store all output in terminal.use that example code:-使用日志文件,该日志文件将所有输出存储在终端中。使用该示例代码:-

child = pexpect.spawn("ssh user@localhost")
child.logfile = open("/tmp/mylog", "w")
child.expect(".*assword:")
child.send("guest\r")
child.expect(".*\$ ")
child.sendline("python -V\r")

open the log file and see everything in terminals event打开日志文件并查看终端事件中的所有内容

To fetch the complete output after sendline use child.read()要在发送后使用child.read()获取完整的输出

eg例如

cmd_resp = pexpect.spawnu(cmd)    # for execution of the command
str_to_search = 'Please Enter The Password'
cmd_resp.sendline('yes')       # for sending the input 'yes'
resp = cmd_resp.expect([str_to_search, 'password:', EOF], timeout=30) # fetch the output status
if resp == 1:
   cmd_resp.sendline(password) 
   resp = cmd_resp.expect([str_to_search, 'outputString:', EOF], timeout=30)
   print(cmd_resp.read()) # to fetch the complete output log

p.before is a string - not a function. p.before是一个字符串 - 不是一个函数。 To see the output you have to write print p.before .要查看输出,您必须编写print p.before Hope this might help you希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM