python数据类型基本知识入门(Python Pexpect库的简单使用方法)
python数据类型基本知识入门
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 ''' |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:http://www.cnblogs.com/primadonna/p/10329094.html
- pythonide使用教程(对Python Pexpect 模块的使用说明详解)
- python 读文件报错处理(解决python写入带有中文的字符到文件错误的问题)
- python3循环使用教程(Python3.5常见内置方法参数用法实例详解)
- python用于机器人(python机器人运动范围问题的解答)
- pythontkinter循环显示文本(Python实现定时自动关闭的tkinter窗口方法)
- python3和python2 兼容(Python2和Python3的共存和切换使用)
- Python实现模拟点击(用python实现刷点击率的示例代码)
- python3 代码解读(Python3列表内置方法大全及示例代码小结)
- 利用python合并pdf(Python合并同一个文件夹下所有PDF文件的方法)
- java怎么调用支付宝的扫码支付(python实现银联支付和支付宝支付接入)
- python对于gbk处理(Python 2/3下处理cjk编码的zip文件的方法)
- python读取文件的方法和区别(浅谈PYTHON 关于文件的操作)
- python中if的条件语句(浅谈Python的条件判断语句if/else语句)
- python删除list的指定元素(Python list列表中删除多个重复元素操作示例)
- python经典算法(浅谈python常用程序算法)
- python使用什么函数定义匿名函数(Python匿名函数及应用示例)
- 追连续剧,品古今联4 明代三杨,联妙诗佳(追连续剧品古今联4)
- 三杨 共辅四朝帝王,构建明帝国内阁行政圈(三杨共辅四朝帝王)
- 红色文化进国企(红色文化进国企)
- 车友的选择| 轮毂该如何选(车友的选择轮毂该如何选)
- 秦海璐炫耀和王新军热恋蜜事,不料对方吐槽她吃饱后肚子撅老高(秦海璐炫耀和王新军热恋蜜事)
- 秦海璐一袭旗袍惹人倾心,将高级与淡雅展现的游刃有余(秦海璐一袭旗袍惹人倾心)
热门推荐
- mybatis测试出现空指针(Mybatis非配置原因,导致SqlSession was not registered for synchronization异常)
- SqlServer类似正则表达式的字符处理问题(SqlServer类似正则表达式的字符处理问题)
- 用css实现滚动效果(CSS完成视差滚动效果)
- js验证身份证号
- python 模块详解(举例讲解Python常用模块)
- ExtJs中Store的种类
- dedecms栏目内容怎么插入分页符(dedecms自动给已经发布过的文章中的关键词加超链接)
- 如何查找JS错误
- php 上传临时文件扩展名(浅析PHP 中move_uploaded_file 上传中文文件名失败)
- iis为什么找不到文件(iis 不能下载包含中文文件名的rar文件)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9