Pexpect makes Python a better tool for controlling other applications. Pexpect is a pure Python module for spawning child applications; controlling them; and responding to expected patterns in their output. Pexpect works like Don Libes' Expect. Pexpect allows your script to spawn a child application and control it as if a human were typing commands. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing. Pexpect is in the spirit of Don Libes' Expect, but 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. The Pexpect interface was designed to be easy to use.
2025-09-14 16:26:38 147KB pexpect
1
Python的pexpect模块是一个强大的自动化工具,主要用于控制和自动化交互式应用程序,比如telnet、SSH、ftp等。它的工作原理是模拟一个终端会话,能够发送输入、接收输出,并根据预期的输出进行响应,因此得名"pexpect"。这个模块在系统自动化测试、脚本编写以及需要与命令行程序交互的场景中非常有用。 pexpect模块的核心功能包括: 1. **启动进程**:可以启动一个新的进程,比如执行一个命令行程序,并接管其标准输入、输出和错误流。 2. **等待预期输出**:pexpect能够等待并识别进程输出的特定字符串或模式,当匹配到时,它会返回该输出的索引或对象。 3. **发送输入**:在接收到预期输出后,可以向进程发送新的输入,继续控制进程的执行流程。 4. **异常处理**:如果进程的输出不符合预期,pexpect会抛出异常,便于捕获并处理错误情况。 5. **非阻塞IO**:pexpect支持非阻塞IO,这意味着在等待进程输出的同时,Python脚本可以执行其他任务,提高了程序效率。 在安装pexpect之前,确保已经安装了Python环境。pexpect通常不是Python标准库的一部分,需要通过pip来安装。在命令行中输入以下命令: ``` pip install pexpect ``` 如果你遇到了版本问题或者网络问题导致无法通过pip安装,可以从官方网站或者其他可靠的源下载pexpect的源码包(如你提供的`pexpect-4.6.0`),然后手动编译安装。步骤如下: 1. 解压下载的压缩包,例如: ``` tar -zxvf pexpect-4.6.0.tar.gz ``` 2. 进入解压后的目录: ``` cd pexpect-4.6.0 ``` 3. 使用Python的setuptools来安装: ``` python setup.py install ``` 一旦安装成功,你就可以在Python脚本中导入并使用pexpect模块了。下面是一个简单的示例,展示了如何使用pexpect启动一个telnet会话并发送一些命令: ```python import pexpect child = pexpect.spawn('telnet localhost 23') # 启动telnet并连接到本地主机的23端口 child.expect('Username:') # 等待并匹配'Username:'字符串 child.sendline('myusername') # 发送用户名 child.expect('Password:') # 等待并匹配'Password:'字符串 child.sendline('mypassword') # 发送密码 child.expect('Prompt:') # 假设登录成功后出现'Prompt:'提示符 child.sendline('ls') # 发送ls命令 print(child.read()) # 打印输出 child.close() # 关闭会话 ``` 这个例子展示了pexpect的基本用法,实际上它还支持更复杂的模式匹配(如正则表达式)、子进程管理、超时处理等功能。通过深入学习和实践,你可以充分利用pexpect模块来简化那些需要手动交互的自动化任务。
2025-09-14 16:25:53 145KB python
1
python expect工具。 自动交互完成: 1. 批量并行scp/rsync 2. 批量并行ssh并执行命令 3. 挨个自动ssh,退出(ctrl-D、exit)后,自动登入下一台机器
2023-10-25 06:03:41 92KB python expect pexpect
1
pexpect_deploy_tools 批量远程操作脚本-- scp 和 ssh 无环境依赖,解压压缩包即可使用,较原生 expect 慢,可定制性高。 使用方法: 当前目录创建 ip list: 192.168.0.100 192.168.0.101 pscp.py 拷贝本地文件到远程机器(iplist.txt)列表的制定目录 pscp.py iplist.txt source_localfile destpath 说明: iplist.txt: 远程机器列表 source_localfile: 需要拷贝的文件 destpath: 远程目的路径 rshell.py 远程机器上执行 test.sh rshell.py iplist.txt < test.sh
2023-03-15 10:09:33 133KB Python
1
python自动化运维框架,同时封装paramiko和pexpect
2022-07-26 09:04:59 27KB paramiko pexpect
1
最近发现Python课器做很多事情,在监控服务器有其独特的优势,耗费资源少,开发周期短。 首先我们做一个定时或者实时脚本timedtask.py,让其定时监控目标服务器,两种方式: 第一种: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017/11/27 15:59 # @Desc : 定时任务,以需要的时间间隔执行某个命令 # @File : timedtask.py # @Software: PyCharm import time, os from monitorserver import alltask def
2021-09-22 19:40:44 269KB cpu时间 info pexpect
1
期待 Wexpect是一个Windows变种。 Pexpect是一个Python模块,用于生成子应用程序并自动对其进行控制。 您是否需要wexpect,如果... 您想通过python脚本控制任何Windows控制台应用程序。 您要为Windows控制台应用程序编写测试自动化脚本。 您想通过同步并行控制多个应用程序来自动化您的工作。 安装 pip install wexpect 用法 要与子进程进行交互,请使用spawn方法: import wexpect prompt = '[A-Z]\:.+>' child = wexpect . spawn ( 'cmd.exe' ) child . expect ( prompt ) # Wait for startup prompt child . sendline ( 'dir' ) # List the curren
2021-07-28 10:49:49 102KB python windows test-automation redirect
1
python开发
2021-05-11 16:00:44 9KB python pexpect ssh telnet
1
比使用pip insatll安装的版本1.5,修改了许多Bug。 譬如self.child.termiate(),就不再报错了。
2019-12-21 19:44:14 38KB pexpect winpexpect
1