Questions tagged [pexpect]

Pexpect is a pure Python module that reads the output of a program and sends input to that program based on output patterns. It can be used to automate interactions with interactive programs. It is inspired by the classic TCL expect program.

Pexpect is a pure Python module for controlling and automating other programs. It

Pexpect is similar to the Don Libes Expect system in TCL, but with a different interface that is intended to be easier to understand.

Pexpect is basically a pattern matching system. It runs programs and watches output. When the output matches a given pattern Pexpect can respond by sending input to the program. Pexpect can be used for automation, testing, and screen scraping of interactive programs. From the project description:

Pexpect can be used for automating interactive console applications such as ssh, ftp, passwd, telnet, etc. It can also be used to control web applications via lynx, w3m, or some other text-based web browser. Pexpect is pure Python. Unlike other Expect-like modules for Python Pexpect does not require TCL or Expect nor does it require C extensions to be compiled. It should work on any platform that supports the standard Python pty module.

Resources

820 questions
36
votes
8 answers

Python how to read output from pexpect child?

child = pexpect.spawn ('/bin/bash') child.sendline('ls') print(child.readline()) print child.before, child.after All I get with this code in my output is ls ls But when my code is child = pexpect.spawn('ls') print(child.readline()) print…
user2579116
  • 473
  • 1
  • 4
  • 8
32
votes
4 answers

Multiple inputs and outputs in python subprocess communicate

I need to do something like this post, but I need to create a subprocess that can be given input and give output many times. The accepted answer of that post has good code... from subprocess import Popen, PIPE, STDOUT p = Popen(['grep', 'f'],…
reynoldsnlp
  • 1,072
  • 1
  • 18
  • 45
32
votes
4 answers

python libraries for ssh handling

I'm going to write first code for handling ssh commands on python and I did search over the stackoverflow and can see that there are several python libraries that can be used for handling commands passed through ssh, like paramiko, pexpect and…
yart
  • 7,515
  • 12
  • 37
  • 37
26
votes
3 answers

Run command and get its stdout, stderr separately in near real time like in a terminal

I am trying to find a way in Python to run other programs in such a way that: The stdout and stderr of the program being run can be logged separately. The stdout and stderr of the program being run can be viewed in near-real time, such that if the…
J.J
  • 3,459
  • 1
  • 29
  • 35
19
votes
3 answers

How to get Fabric to automatically (instead of user-interactively) interact with shell commands? Combine with pexpect?

Seeking means to get Fabric to automatically (instead of user-interactively) interact with shell commands (and not just requests for passwords, but also requested user input when no "stdin/interactive override" like apt-get install -y is…
Johnny Utahh
  • 2,389
  • 3
  • 25
  • 41
16
votes
3 answers

fabric vs pexpect

I've stumbled upon pexpect and my impression is that it looks roughly similar to fabric. I've tried to find some comparison, without success, so I'm asking here--in case someone has experience with both tools. Is my impression (that they are…
Tomasz Zieliński
  • 16,136
  • 7
  • 59
  • 83
15
votes
1 answer

Cannot import name 'spawn' for pexpect while using pxssh

This is the code I am trying to run: from pexpect import pxssh s = pxssh.pxssh() if not s.login ('myip', 'myusername', 'mypassword'): print ("SSH session failed on login.") print (str(s)) else: print ("SSH session login successful") …
Oliver Blue
  • 677
  • 2
  • 9
  • 22
13
votes
4 answers

How to check the status of docker-compose up -d command

When we run docker-compose up-d command to run dockers using docker-compose.yml file, it starts building images or pulling images from the registry. We can see each and every step of this command on the terminal. I am trying to run this command from…
S Andrew
  • 5,592
  • 27
  • 115
  • 237
13
votes
4 answers

pexpect.expect() in python3 is throwing error as "must be in str , not bytes"

I am migrating my code to python 3.4.3. This code works fine in python 2.4.3. but here it throws error in python 3.4.3. should I use anything different from expect ? Here is my code snippet which gets the error: telconn=pexpect.spawn('telnet…
Suma
  • 241
  • 1
  • 2
  • 12
13
votes
2 answers

python pexpect clearing or flushing the line

I am trying to clear any characters on the line after recovering from an unknown state, as in some cases they contain prompts and other keywords that I use in future expect method calls. I have tried multiple approaches with mixed results, as I…
user4061565
  • 545
  • 1
  • 5
  • 16
13
votes
4 answers

ImportError: No module named pexpect

I am using Fabric and would like to use fexpect. I have the following Python script: from ilogue.fexpect import expect, expecting, run (...) def install_postgresql(profile): print("!!! Installing PostgreSQL...") print(' -> Doing…
mitchkman
  • 6,201
  • 8
  • 39
  • 67
12
votes
3 answers

pexpect can't pass input over 1024 chars?

I'm currently passing some input to a process with pexpect with the following code: p = pexpect.spawn('cat', timeout=5.0 ) p.maxread = 5000 p.setecho(False) # prevent the process from echoing stdin back to us INPUT_LEN =…
tba
  • 6,229
  • 8
  • 43
  • 63
12
votes
2 answers

Not able to print pexpect response via python logger

I am trying to have pexepct stdout logs via logger that I have defined. Below is the code import logging import pexpect import re import time # this will be the method called by the pexpect object to log def _write(*args, **kwargs): content =…
Sumit
  • 1,953
  • 6
  • 32
  • 58
12
votes
3 answers

Windows alternative to pexpect

I'm trying to write a cross-platform tool that runs specific commands, expects certain output for verification, and sends certain output (like username/password) for authentication. On Unix, I have been successful in programming a Python tool that…
Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
11
votes
3 answers

Pexpect and PyCharm - Inappropriate ioctl for device

I'm trying to run a basic Pexpect script: import pexpect ftp_process = pexpect.spawn('ftp') ftp_process.interact() When the code is run directly from a terminal, the code works as expected. If I run the code using PyCharm's run/debug I get the…
golmschenk
  • 11,736
  • 20
  • 78
  • 137
1
2 3
54 55