pexpect获取远端命令执行结果
类比于shell的expect, python中使用pexpect模块来模拟用户和终端交互。有的时候使用pexpect.sendline发送命令后,在各种条件影响下, 可能并不能保证命令在远端服务器执行成功(例如sftp下执行远端rename/rm,实际文件可能并未成功改名/删除)。这个时候就可能需要获取命令执行结果,然后分析结果来对命令的执行状态进行最终确认!
pexpect模块中可以通过pexpect.before/pexpect.buffer获取命令执行结果:
pexpect.buffer -- 动态保存每一次expect后的所有内容. before/after都依赖此内容;
pexpect.before -- 匹配到的关键字之外的字符;expect后会设置before/after, 具体参考附录,摘录一段文字如下: There are two important methods in Pexpect – expect() and send() (or sendline() which is like send() with a linefeed). The expect() method waits for the child application to return a given string. The string you specify is a regular expression, so you can match complicated patterns. The send() method writes a string to the child application. From the child’s point of view it looks just like someone typed the text from a terminal. After each call to expect() the before and after properties will be set to the text printed by child application. The before property will contain all text up to the expected string pattern. The after string will contain the text that was matched by the expected pattern. The match property is set to the re match object.
测试 -- 模拟sftp登陆后然后执行ls命令查看远端路径
linux:~ # tree /root/sftp/
/root/sftp/
├── csv
│ ├── .csv
│ └── .csv
└── dat
├── .dat
└── .dat directories, files
linux:~ #
linux:~ # cat pexpect_test.py
import os
import sys
import pexpect def pexpect_get_command_result(command, process, prompt = 'sftp>'):
process.sendline('') # 发送空行, 初始环境
process.expect(prompt)
process.buffer = "" # 清空buffer, 防止互相影响
process.sendline(command)
process.expect(prompt) # expect后得到的expect.before就是命令和执行结果 return process.before.strip() # 返回结果 def pexpect_connect():
process = pexpect.spawn('sftp 127.0.0.1', timeout=30)
index = process.expect(["assword: ", "yes/no", pexpect.EOF, pexpect.TIMEOUT]) if index not in [0, 1]:
print "[-] sftp login failed, due to TIMEOUT or EOF"
return None if 1 == index:
process.sendline("yes")
process.expect("assword: ") process.sendline('passwd') return process if __name__ == '__main__':
process = pexpect_connect()
if process == None:
sys.exit(-1) allpath = ['/root/sftp/dat', '/root/sftp/csv']
for path in allpath:
print pexpect_get_command_result('ls ' + path, process) process.sendline("bye") process.close(force = True)
linux:~ #
linux:~ # python pexpect_test.py
ls /root/sftp/dat
/root/sftp/dat/1000.dat /root/sftp/dat/3000.dat
ls /root/sftp/csv
/root/sftp/csv/1000.csv /root/sftp/csv/3000.csv
linux:~ #
参考
https://pexpect.readthedocs.io/en/stable/
http://www.noah.org/python/pexpect/
https://www.cnblogs.com/zz27zz/p/7918717.html 引用出处:
https://pexpect.readthedocs.io/en/stable/overview.html?highlight=pexpect.buffer
pexpect获取远端命令执行结果的更多相关文章
- Go实现ssh执行远端命令及远程终端
什么是ssh? SSH是一种网络协议,用于计算机之间的加密登录. 如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会泄露. 互联 ...
- Python进阶----SOCKET套接字基础, 客户端与服务端通信, 执行远端命令.
Python进阶----SOCKET套接字基础, 客户端与服务端通信, 执行远端命令. 一丶socket套接字 什么是socket套接字: 专业理解: socket是应用层与TCP/IP ...
- git 学习笔记 —— 获取远端分支并修改后提交至远端仓库
笔者最近进行开发过程中,所有参与者的代码需要通过 git 上传到远端仓库中,不同的模块对应不同的 git 分支,不同模块的数据需要从远端仓库中获取.这里记录下笔者从远端仓库中获取分支数据,进行修改,最 ...
- saltstack命令执行过程
saltstack命令执行过程 具体步骤如下 Salt stack的Master与Minion之间通过ZeroMq进行消息传递,使用了ZeroMq的发布-订阅模式,连接方式包括tcp,ipc salt ...
- ping命令执行过程详解
[TOC] ping命令执行过程详解 机器A ping 机器B 同一网段 ping通知系统建立一个固定格式的ICMP请求数据包 ICMP协议打包这个数据包和机器B的IP地址转交给IP协议层(一组后台运 ...
- 【MongoDB】6.关于MongoDB存储文件的 命令执行+代码执行
参考:http://www.runoob.com/mongodb/mongodb-gridfs.html 1.命令执行 MongoDB GridFS GridFS 用于存储和恢复那些超过16M(BSO ...
- 图解“管道过滤器模式”应用实例:SOD框架的命令执行管道
管道和过滤器 管道和过滤器是八种体系结构模式之一,这八种体系结构模式是:层.管道和过滤器.黑板.代理者.模型-视图-控制器(MVC) 表示-抽象-控制(PAC).微核.映像. 管道和过滤器适用于需要渐 ...
- Linux:命令执行顺序控制与管道
命令执行顺序控制与管道 顺序执行 简单的顺序命令可以使用符号";"完成,如:sudo apt-get update;sudo apt-get install some-tool;s ...
- jenkins远程命令执行利用工具
昨天看小飞侠写的py的jenkins的脚本,昨天晚上在微信里评论今天写一个JAVA的GUI的tools. 早上花了点时间写一下: code: package com.tools; import jav ...
随机推荐
- HDU 1254 推箱子(BFS加优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others) Me ...
- VMware虚拟机修改BIOS启动项
vmware默认是硬盘启动,要进bios里面设置成开机的启动顺序,要将光盘设置成第一启动项.但vm的开机画面比笔记本的还要快很多,基本都在1s内的,想进入 bios里面也有难度.. 对于网上说的开vm ...
- ContentProvider 、 ContentResolver 、 ContentObserver
说说ContentProvider . ContentResolver . ContentObserver 之间的关系**a. ContentProvider 内容提供者,用于对外提供数据 b. Co ...
- 关于In-App Purchase(内购)的注意事项
前言:关于In-App Purchase(内购)的注意事项 一点注意事项: 内购应该有游客身份的购买选项,不然可能被拒,前一段时间我这边一直是因为没有游客身份购买被拒. 在验证内购结果的时候要注意使用 ...
- SpringBoot非官方教程 | 第十三篇:springboot集成spring cache
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot13-springcache/ 本文出自方志朋的博 ...
- linux各种抓包情况说明
大家都知道抓包指令:tcpdump 抓包的主要目的是测试端口.网络协议通不通,以及对抓取的数据包进行分析.测试,抓包对熟悉linux的大神都不陌生,网络对于我来说也是一窍不通,只是在这里记录一下 ...
- #leetcode刷题之路3-无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc" ...
- jquery和vue分别对input输入框手机号码格式化(344)
jQuery function fomatterTel(val, old) {//val: 当前input的值,old: input上次的值 var str = ""; var t ...
- C#中给WebClient添加代理Proxy
效果图: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...
- ECSHOP和SHOPEX快递单号查询申通插件V8.6专版
发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...