Python Pexpect库的使用

简介

最近需要远程操作一个服务器并执行该服务器上的一个python脚本,查到可以使用Pexpect这个库。记录一下。

什么是Pexpect?Pexpect能够产生子应用程序,并控制他们,并能够通过期望模式对子应用的输出做出反应。Pexpect允许你的脚本产生子应用、控制他们像一个人类在输入命令一样。

Pexpect使用在自动交互的应用,例如SSH、SFTP、PASSWD、TELNET。它可以被应用在使用自动设置脚本为不同的服务器自动地重复的安装软件包。也可以被应用在自动的软件测试。

Pexpect的主要特点是需要Python的基本库pty,这个库只有在类Unix系统上才有

Pexpect关于SSH的使用

注:测试,我们直接用虚拟机本机ssh本机

  • 环境
1. win10 物理机
2. Vmware Centos 虚拟机
3. Xshell
4. 虚拟机python安装pexpect:pip install pexpect
  • 在虚拟机创建一个 python文件
#-*- coding:UTF-8 -*-
import pexpect
# 定义ssh连接
def ssh(user,host,password,command):
#创建子应用,命令是 ssh -l root 127.0.0.1 python /home/python/test.py
child = pexpect.spawn('ssh -l %s %s %s'%(user,host,command))
# 期待开启的子程序的显示,子程序的不同显示会匹配到不同key然后我们定义不同的操作
# 0 : 连接超时
# 1 :ssh有时候提示你是否确认连接
# 2 :提示输入密码
# 3 :匹配到#号,表示命令已经执行完毕。没用到
i = child.expect([pexpect.TIMEOUT, 'Are you sure you want to continue connecting','password:',r"([^-]>|#)"])
# 如果登录超时,renturn none
if i == 0: # Timeout
print "Timeout"
return None
# 提示是否确认连接
if i == 1:
child.sendline ('yes') # 我们输入yes
child.expect ('password: ')# 输入yes后 子程序应该提示输入密码,我们再次期待password
i = child.expect([pexpect.TIMEOUT, 'password: '])
#超时
if i == 0: # Timeout
return None
# 不考虑其他情况,走到此处时,要么timeout 已经return ,要么等待输入密码
#输入密码
child.sendline(password)
# 返回子程序
return child
if __name__ =='__main__':
try:
# 配置数据
host='127.0.0.1'
user="root"
password = '********'
command = 'python /home/python/test.py'
#command="ls -l"
child = ssh(user,host,password,command)
#这句是将子程序的命令行拉到末端
test = child.expect(pexpect.EOF)
#child中before就是我们要的数据,有时候还会在 after中
print child.before
print child.after
except Exception,e:
print str(e)
# 最终的显示结果是 test.py中打印的hahaha结果,
[root@localhost python]# python test_pexpect.py hahaha <class 'pexpect.exceptions.EOF'>
  • 我们尝试一下开两个虚拟机的情况
上面的代码只需要更改ip user password即可
# ip 192.168.233.133
# user root
# 在另一台虚拟机的相同位置创建/home/pyhton/test.py 内容如下
if __name__=="__main__":
print "another virual machine hahaha"
# 打印结果
[root@localhost python]# python test3.py another virual machine hahaha <class 'pexpect.exceptions.EOF'>

Pexpect 关于 SFTP的使用

与ssh相同,就是使用python在当前机器上输入sftp ip 然后期望结果,输入密码,并发送get下载文件即可。

注:使用的时候发现一点注意:在每次执行sendline之前 都需要重新期望一下当前的sftp>,或者在每次输入sendline之后重新期望一下sftp>。也就是期望到这行,否则输入的命令都没有反应,我理解是远程连接的服务器有输出时候当前的位置可能不在sftp>这里所以在sendline的任何东西都是无意义的。如果这个解释不对望高人指点一下,

# --*-- coding:utf-8 --*--
import pexpect
import os
import time
def sftp(ip , password , command):
# 创建子应用
child = pexpect.spawn("sftp %s"%(ip))
i = child.expect([pexpect.TIMEOUT,'password:'])
# 超时
if i == 0 :
print "Timeout"
return None
# 准备输入密码
if i == 1 :
# 输入密码
child.sendline(password)
j = child.expect([pexpect.TIMEOUT,'sftp>'])
# 超时
if j == 0:
print "Timeout"
return None
# 匹配到进入sftp命令模式
if j==1:
print 'Before sftp get command'
print child.before
print "-----------------"
#发送命令
child.sendline(command)
child.expect(['sftp>'])
print "After sftp get command"
print child.before
print "-----------------"
child.sendline("bye")
#child.expect(['sftp>'])
print "After sftp bye"
print child.before
print "-----------------"
print child.after
return child
if __name__=='__main__':
ip = "192.168.233.133"
command = "get /home/python/test.txt"
password = "********"
child = sftp(ip , password , command)
print child.before
print child.after
if os.path.exists("./test.txt"):
print "Make sure transfer successfully"
else :
print "Can not find the transfer file" # ----------------------------结果-----------------------------------------------
'''
Before sftp get command Connected to 192.168.233.133. -----------------
After sftp get command
get /home/python/test.txt
Fetching /home/python/test.txt to test.txt
/home/python/test.txt 100% 73 25.2KB/s 00:00 -----------------
After sftp bye
get /home/python/test.txt
Fetching /home/python/test.txt to test.txt
/home/python/test.txt 100% 73 25.2KB/s 00:00 -----------------
sftp>
get /home/python/test.txt
Fetching /home/python/test.txt to test.txt
/home/python/test.txt 100% 73 25.2KB/s 00:00 sftp>
Make sure transfer successfully
'''

Python Pexpect库的简单使用的更多相关文章

  1. python第三方库requests简单介绍

    一.发送请求与传递参数 简单demo: import requests r = requests.get(url='http://www.itwhy.org') # 最基本的GET请求 print(r ...

  2. python requests库的简单使用

    requests是python的一个HTTP客户端库,跟urllib,urllib2类似,但比urllib,urllib2更加使用简单. 1. requests库的安装在你的终端中运行pip安装命令即 ...

  3. Python turtle库绘制简单图形

    一.简介 Python中的turtle库是一个直观有趣的图形绘制函数库.turtle库绘制图形有一个基本框架:一个小海龟在坐标系中爬行,其爬行轨迹形成了绘制图形. 二.简单的图形列举 1.绘制4个不同 ...

  4. python urllib2库的简单总结

    urllib2的简单介绍参考网址:http://www.voidspace.org.uk/python/articles/urllib2.shtml Fetching URLsThe simplest ...

  5. python requests库的简单运用

    python requests的简单运用 使用pycharm获取requests包 ctrl+alt+s Project:pythonProject pythoninterpreter 点+号搜索 使 ...

  6. python第三方库,你要的这里都有

    Python的第三方库多的超出我的想象. python 第三方模块 转 https://github.com/masterpy/zwpy_lst   Chardet,字符编码探测器,可以自动检测文本. ...

  7. python常用库(转)

    转自http://www.west999.com/info/html/wangluobiancheng/qita/20180729/4410114.html Python常用的库简单介绍一下 fuzz ...

  8. Python全部库整理

    库名称简介 Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主要用于在终端或浏览器端构建格式 ...

  9. python的库有多少个?python有多少个模块?

    这里列举了大概500个左右的库: !   Chardet字符编码探测器,可以自动检测文本.网页.xml的编码. colorama主要用来给文本添加各种颜色,并且非常简单易用. Prettytable主 ...

随机推荐

  1. ( linker command failed with exit code 1) 错误解决方案 项目使用的是pod

    targets -> build settings -> architectures -> build active architecture only -> debug 改成 ...

  2. vue 之webpack打包工具的使用

    一.什么是webpack? webpack是一个模块打包工具.用vue项目来举例:浏览器它是只认识js,不认识vue的.而我们写的代码后缀大多是.vue的,在每个.vue文件中都可能html.js.c ...

  3. WPA2-PSK无线密码破解

    无线网络WIFI(wireless Fidelity )正确发音 /wai fai/ 是一个建立在IEEE 802.11标准的无线局域网,目前主流的无线上网模式主要有两种分别是 GRPS(手机无线上网 ...

  4. hdu4966 最小树形图+虚根

    /* 辛辛苦苦调试半天, 过了样例,竟然没有ac!! 网上对比了ac代码,感觉添加一个虚根就能ac 但是想不明白为什么 */ /* 第二天想了下,知道了为什么wa:因为从等级0连到其他课程等级i的不止 ...

  5. 错误 java.lang.ClassCastException: com.xx cannot be cast to ResourceBundle

    出现错误: java.lang.ClassCastException: com.xxx cannot be cast to ResourceBundle 百度搜索错误,没有结果.谷歌搜索:http:/ ...

  6. [转] css3变形属性transform

    w3c上的例子是这样子写的:· div { transform:rotate(7deg); -ms-transform:rotate(7deg); /* IE 9 */ -moz-transform: ...

  7. [转]MySQL 数据类型(float)的注意事项

    http://www.cnblogs.com/zhoujinyi/archive/2013/04/26/3043160.html 可能由于版本关系,我的mysql5.7插入数据超过范围时会提示,126 ...

  8. DirectoryEntry配置IIS7出现ADSI Error:未知错误(0x80005000)

    一.错误情况 环境:win7+iis7.0 DirectoryEntry配置IIS7出现如下错误 或者是 下面一段代码在IIS6.0下运转正常,但IIS7.0下运转会出错: System.Direct ...

  9. 【Android】Android apk默认安装位置设置

    在Android工程中,设置apk的默认安装位置 在AndroidManifest.xml文件Manifest标签中添加android:installLocation属性 android:instal ...

  10. mariadb-半同步复制

    半同步复制: 使用插件 对于从节点,有一部分为同步复制,当主节点复制完从节点后才向客户返回ok,同步超时后自动降级为异步 有一部分为异步复制 这样为了与主节点冗余 基于主从的模式上搭建 半同步复制: ...