- 浏览: 580706 次
- 来自: 北京
文章分类
最新评论
-
lidi2011:
很通俗易懂的文章,很形象。
同步synchronized方法和代码块 -
inuyasha027:
领教了,谢谢。
Hadoop安装, Hive 安装。 -
xbmujfly:
好文 ,本人发现晚了
学习笔记 - java.util.concurrent 多线程框架 -
hanazawakana:
学习学习!
ANT-build.xml文件详解 -
david.org:
似乎还忽略一点,那就是cassandra不同数据中心的同步,H ...
Cassandra Vs HBase
简介: Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块。 Pexpect 可以用来和像 ssh、ftp、passwd、telnet 等命令行程序进行自动交互。本文章介绍 Pexpect 的主要用法和在实际应用中的注意点。 Python 语言的爱好者,系统管理人员,部署及测试人员都能使用 Pexpect 在自己的工作中实现与命令行交互的自动化。 Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Python 模块。 Pexpect 的使用范围很广,可以用来实现与 ssh、ftp 、telnet 等程序的自动交互;可以用来自动复制软件安装包并在不同机器自动安装;还可以用来实现软件测试中与命令行交互的自动化。 Pexpect 可以从 SourceForge 网站下载。 本文介绍的示例使用的是 2.3 版本,如不说明测试环境,默认运行操作系统为 fedora 9 并使用 Python 2.5 。 由于其依赖 pty module ,所以 Pexpect 还不能在 Windows 的标准 python 环境中执行,如果想在 Windows 平台使用,可以使用在 Windows 中运行 Cygwin 做为替代方案。 根据 Wiki 对 MIT License 的介绍“该模块被授权人有权利使用、复制、修改、合并、出版发行、散布、再授权及贩售软件及软件的副本。被授权人可根据程序的需要修改授权条款为适当的内容。 在软件和软件的所有副本中都必须包含版权声明和许可声明。” 函数 run 可以用来运行命令,其作用与 Python os 模块中 system() 函数相似。run() 是通过 Pexpect 类实现的。 如果命令的路径没有完全给出,则 run 会使用 which 命令尝试搜索命令的路径 。 与 os.system() 不同的是,使用 run() 可以方便地同时获得命令的输出结果与命令的退出状态 。 command_out 中保存的就是 /bin 目录下的内容。 spawn是Pexpect模块主要的类,用以实现启动子程序,它有丰富的方法与子程序交互从而实现用户对子程序的控制。它主要使用 pty.fork() 生成子进程,并调用 exec() 系列函数执行 command 参数的内容。 可以这样使用: 当子程序需要参数时,还可以使用一个参数的列表: 在构造函数中,maxread 属性指定了 Pexpect 对象试图从 tty 一次读取的最大字节数,它的默认值是2000字节 。 由于需要实现不断匹配子程序输出, searchwindowsize 指定了从输入缓冲区中进行模式匹配的位置,默认从开始匹配。 logfile 参数指定了 Pexpect 产生的日志的记录位置。 例如: 还可以将日志指向标准输出: 如果不需要记录向子程序输入的日志,只记录子程序的输出,可以使用: 为了控制子程序,等待子程序产生特定输出,做出特定的响应,可以使用 expect 方法。 在参数中: pattern 可以是正则表达式, pexpect.EOF , pexpect.TIMEOUT ,或者由这些元素组成的列表。需要注意的是,当 pattern 的类型是一个列表时,且子程序输出结果中不止一个被匹配成功,则匹配返回的结果是缓冲区中最先出现的那个元素,或者是列表中最左边的元素。使用 timeout 可以指定等待结果的超时时间 ,该时间以秒为单位。当超过预订时间时, expect 匹配到pexpect.TIMEOUT。 如果难以估算程序运行的时间,可以使用循环使其多次等待直至等待运行结束: expect() 在执行中可能会抛出两种类型的异常分别是 EOF and TIMEOUF,其中 EOF 通常代表子程序的退出, TIMEOUT 代表在等待目标正则表达式中出现了超时。 此时可以将这两种异常放入expect等待的目标列表中: expect 不断从读入缓冲区中匹配目标正则表达式,当匹配结束时 pexpect 的 before 成员中保存了缓冲区中匹配成功处之前的内容, pexpect 的 after 成员保存的是缓冲区中与目标正则表达式相匹配的内容。 此时 child.before 保存的就是在根目录下执行 ls 命令的结果。 这些方法用来向子程序发送命令,模拟输入命令的行为。 与 send() 不同的是 sendline() 会额外输入一个回车符 ,更加适合用来模拟对子程序进行输入命令的操作。 当需要模拟发送 “Ctrl+c” 的行为时,还可以使用 sendcontrol() 发送控制字符。 由于 send() 系列函数向子程序发送的命令会在终端显示,所以也会在子程序的输入缓冲区中出现,因此不建议使用 expect 匹配最近一次 sendline() 中包含的字符。否则可能会在造成不希望的匹配结果。 Pexpect还可以调用 interact() 让出控制权,用户可以继续当前的会话控制子程序。用户可以敲入特定的退出字符跳出,其默认值为“^]” 。 下面展示一个使用Pexpect和ftp交互的实例。 该程序与 ftp 做交互,登录到 ftp.openbsd.org ,当提述输入登录名称和密码时输入默认用户名和密码,当出现 “ftp>” 这一提示符时切换到 pub/OpenBSD 目录并下载 README 这一文件。 以下实例是上述方法的综合应用,用来建立一个到远程服务器的 telnet 连接,并返回保存该连接的 pexpect 对象。 Pxssh 做为 pexpect 的派生类可以用来建立一个 ssh 连接,它相比其基类增加了如下方法: login() 建立到目标机器的ssh连接 ; losuckgout() 释放该连接 ; prompt() 等待提示符,通常用于等待命令执行结束。 下面的示例连接到一个远程服务器,执行命令并打印命令执行结果。 该程序首先接受用户输入用户名和密码,login 函数返回一个 pxssh 对象的链接,然后调用 sendline() 分别输入 “uptime”、“ls” 等命令并打印命令输出结果。 在使用spawn执行命令时应该注意,Pexpect 并不与 shell 的元字符例如重定向符号 > 、>> 、管道 | ,还有通配符 * 等做交互,所以当想运行一个带有管道的命令时必须另外启动一个 shell ,为了使代码清晰,以下示例使用了参数列表例如: Perl 也有 expect 的模块 Expect-1.21,但是 perl 的该模块在某些操作系统例如 fedora 9 或者 AIX 5 中不支持在线程中启动程序执行以下实例试图利用多线同时程登录到两台机器进行操作,不使用线程直接调用时 sub1() 函数可以正常工作,但是使用线程时在 fedora9 和 AIX 5 中都不能正常运行。 Pexpect 则没有这样的问题,可以使用多线程并在线程中启动程序运行。但是在某些操作系统如 fedora9 中不可以在线程之间传递 Pexpect 对象。 清单 使用 Pexpect 在线程中启动控制子程序 在使用 expect() 时,由于 Pexpect 是不断从缓冲区中匹配,如果想匹配行尾不能使用 “$” ,只能使用 “\r\n”代表一行的结束。 另外其只能得到最小匹配的结果,而不是进行贪婪匹配,例如 child.expect ('.+') 只能匹配到一个字符。 在实际系统管理员的任务中,有时需要同时管理多台机器,这个示例程序被用来自动编译并安装新的内核版本,并重启。它使用多线程,每个线程都建立一个到远程机器的 telnet 连接并执行相关命令。 该示例会使用上文中的登录函数。From: http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/index.html
探索 Pexpect,第 1 部分:剖析 Pexpect
download pexpect-2.3.tar.gz
tar zxvf pexpect-2.3.tar.g
cd pexpect-2.3
python setup.py install (do this as root)
清单 1. run() 的定义
run(command,timeout=-1,withexitstatus=False,events=None,\
extra_args=None,logfile=None, cwd=None, env=None)
清单 2. 使用 run()执行 svn 命令
from pexpect import *
run ("svn ci -m 'automatic commit' my_file.py")
清单 3. run() 的返回值
from pexpect import *
(command_output, exitstatus) = run ('ls -l /bin', withexitstatus=1)
清单 4. spawn 的构造函数
class spawn:
def __init__(self,command,args=[],timeout=30,maxread=2000,\
searchwindowsize=None, logfile=None, cwd=None, env=None)
清单 5. spawn() 使用示例
child = pexpect.spawn ('/usr/bin/ftp') #执行ftp客户端命令
child = pexpect.spawn ('/usr/bin/ssh user@example.com') #使用ssh登录目标机器
child = pexpect.spawn ('ls -latr /tmp') #显示 /tmp目录内容
清单 6. 参数列表示例
child = pexpect.spawn ('/usr/bin/ftp', [])
child = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
child = pexpect.spawn ('ls', ['-latr', '/tmp'])
清单 7. 记录日志
child = pexpect.spawn('some_command')
fout = file('mylog.txt','w')
child.logfile = fout
清单 8. 将日志指向标准输出
child = pexpect.spawn('some_command')
child.logfile = sys.stdout
清单 9. 记录输出日志
child = pexpect.spawn('some_command')
child.logfile_send = sys.stdout
清单 10. expect() 定义
expect(self, pattern, timeout=-1, searchwindowsize=None)
清单 11. 使用循环
while True:
index = child.expect(["suc","fail",pexpect.TIMEOUT])
if index == 0:
break
elif index == 1:
return False
elif index == 2:
pass #continue to wait
清单 12. 使用并捕获异常
try:
index = pexpect (['good', 'bad'])
if index == 0:
do_something()
elif index == 1:
do_something_else()
except EOF:
do_some_other_thing()
except TIMEOUT:
do_something_completely_different()
清单 13. 避免异常
index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
if index == 0:
do_something()
elif index == 1:
do_something_else()
elif index == 2:
do_some_other_thing()
elif index == 3:
do_something_completely_different()
清单 14. 打印 before 成员的内容
child = pexpect.spawn('/bin/ls /')
child.expect (pexpect.EOF)
print child.before
清单 15. send 系列函数
send(self, s)
sendline(self, s='')
sendcontrol(self, char)
清单 16. 发送 ctrl+c
child.sendcontrol('c')
清单 17. interact() 定义
interact(self, escape_character = chr(29), input_filter = None, output_filter = None)
清单 18. ftp 交互的实例:
# This connects to the openbsd ftp site and
# downloads the README file.
import pexpect
child = pexpect.spawn ('ftp ftp.openbsd.org')
child.expect ('Name .*: ')
child.sendline ('anonymous')
child.expect ('Password:')
child.sendline ('noah@example.com')
child.expect ('ftp> ')
child.sendline ('cd pub/OpenBSD')
child.expect('ftp> ')
child.sendline ('get README')
child.expect('ftp> ')
child.sendline ('bye')
清单 19. 登录函数:
import re,sys,os
from pexpect import *
def telnet_login(server,user, passwd,shell_prompt= “#|->”):
"""
@summary: This logs the user into the given server.
It uses the 'shell_prompt' to try to find the prompt right after login.
When it finds the prompt it immediately tries to reset the prompt to '#UNIQUEPROMPT#'
more easily matched.
@return: If Login successfully ,It will return a pexpect object
@raise exception: RuntimeError will be raised when the cmd telnet failed or the user
and passwd do not match
@attention:1. shell_prompt should not include '$',on some server, after sendline
(passwd) the pexpect object will read a '$'.
2.sometimes the server's output before its shell prompt will contain '#' or
'->' So the caller should kindly assign the shell prompt
"""
if not server or not user \
or not passwd or not shell_prompt:
raise RuntimeError, "You entered empty parameter for telnet_login "
child = pexpect.spawn('telnet %s' % server)
child.logfile_read = sys.stdout
index = child.expect (['(?i)login:', '(?i)username', '(?i)Unknown host'])
if index == 2:
raise RuntimeError, 'unknown machine_name' + server
child.sendline (user)
child.expect ('(?i)password:')
child.logfile_read = None # To turn off log
child.sendline (passwd)
while True:
index = child.expect([pexpect.TIMEOUT,shell_prompt])
child.logfile_read = sys.stdout
if index == 0:
if re.search('an invalid login', child.before):
raise RuntimeError, 'You entered an invalid login name or password.'
elif index == 1:
break
child.logfile_read = sys.stdout # To tun on log again
child.sendline(“PS1=#UNIQUEPROMPT#”)
#This is very crucial to wait for PS1 has been modified successfully
#child.expect(“#UNIQUEPROMPT#”)
child.expect("%s.+%s" % (“#UNIQUEPROMPT#”, “#UNIQUEPROMPT#”))
return child
清单 20. pxssh 示例
import pxssh
import getpass
try:
s = pxssh.pxssh()
hostname = raw_input('hostname: ')
username = raw_input('username: ')
password = getpass.getpass('password: ')
s.login (hostname, username, password)
s.sendline ('uptime') # run a command
s.prompt() # match the prompt
print s.before # print everything before the propt.
s.sendline ('ls -l')
s.prompt()
print s.before
s.sendline ('df')
s.prompt()
print s.before
s.logout()
except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)
清单 21. 启动新的 shell 执行命令
shell_cmd = 'ls -l | grep LOG > log_list.txt'
child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
child.expect(pexpect.EOF)
清单 22. perl 使用 expect 由于线程和 expect 共同使用导致不能正常工作的程序
use threads;
use Expect;
$timeout = 5;
my $thr = threads->create(\&sub1(first_server));
my $thr2 = threads->create(\&sub1(second_server));
sub sub1
{
my $exp = new Expect;
$exp -> raw_pty(1);
$exp -> spawn ("telnet",$_[0]) or die "cannot access telnet";
$exp -> expect ( $timeout, -re=>'[Ll]ogin:' );
$exp -> send ( "user\n");
$exp -> expect ( $timeout, -re=>'[Pp]assword:' );
$exp -> send ( "password\n" );
$exp -> expect ( $timeout, -re=>" #" );
$exp -> send ( "date\n" );
$exp -> expect ( $timeout, -re=>'\w\w\w \w\w\w \d{1,2}
\d\d:\d\d:\d\d \w\w\w \d\d\d\d');
$localtime=$exp->match();
print "\tThe first server’s time is : $localtime\n";
$exp -> soft_close ();
}
print "This is the main thread!";
$thr->join();
$thr2->join();
清单 23. 管理多台机器示例
import sys,os
from Login import *
PROMPT = “#UNIQUEPROMPT#”
class RefreshKernelThreadClass(threading.Thread):
""" The thread to downLoad the kernel and install it on a new server """
def __init__(self,server_name,user,passwd):
threading.Thread.__init__(self)
self.server_name_ = server_name
self.user_ = user
self.passwd_ = passwd
self.result_ = [] # the result information of the thread
def run(self):
self.setName(self.server_name_) # set the name of thread
try:
#call the telnet_login to access the server through telnet
child = telnet_login(self.server_name_,self.user_, self.passwd_)
except RuntimeError,ex:
info = "telnet to machine %s failed with reason %s" % (self.server_name_, ex)
self.result_.=(False, self.server_name_+info)
return self.result_
child.sendline(' cd ~/Download/dw_test && \
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.28.tar.gz && \
tar zxvf linux-2.6.28.tar.gz && \
cd linux-2.6.28 \
&& make mrproper && make allyesconfig and make -j 4 && make modules && \
make modules install && make install')
# wail these commands finish
while True:
index = child.expect([PROMPT,pexpect.TIMEOUT,pexpect.EOF])
if index == 0:
break
elif index == 1:
pass
elif index ==2 :
self.result_=(False,'Sub process exit abnormally ')
return False
# reboot the server
child.sendline('shutdown -Fr')
child.expect('\r\n')
retry_times = 10
while retry_times > 0:
index_shutdown = child.expect(["Unmounting the file systems",
pexpect.EOF,
pexpect.TIMEOUT])
if index_shutdown == 0 or index_shutdown == 1 :
break
elif index_shutdown == 2:
retry_times = retry_times-1
if retry_times == 0:
self.result_=(False,'Cannot shutdown ')
return self.result_
def refresh_kernel(linux_server_list,same_user,same_passwd):
"""
@summary: The function is used to work on different linux servers to download
the same version linux kernel, conpile them and reboot all these servers
To keep it simple we use the same user id and password on these servers
"""
if not type(linux_server_list) == list:
return (False,"Param %s Error!"%linux_server_list)
if same_user is None or same_passwd is None or not
type(same_user)== str or not type(same_passwd) == str:
return (False,"Param Error!")
thread_list = []
# start threads to execute command on the remote servers
for i in range (len(linux_server_list)):
thread_list[i] = RefreshKernelThreadClass(linux_server_list[i],
same_user,same_passwd)
thread_list[i].start()
# wait the threads finish
for i in range (len(linux_server_list)):
thread_list[i].join()
# validate the result
for i in range (len(linux_server_list)):
if thread_list[0].result_[0] == False:
return False
else:
return True
if __name__ == "__main__":
refresh_kernel(server_list,"test_user","test_passwd")
相关推荐
Python中的pexpect模块下载Python中的pexpect模块下载
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 ...
Python 模块的代码,pexpect3.3.tar.gz ...........
1).配置公钥私钥 2).使用shell下的命令,expect 3).使用python的pexpect模块 下面介绍的代码,是使用python的pexpect模块实现的: 代码 import os import sys import pexpect import datetime #获取昨天的日期 date...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
Pexpect是一个用来启动子程序并对其进行自动控制的Python模块,它可以用来和像ssh,ftp,passwd,telnet等命令行程序进行自动交互。
资源分类:Python库 所属语言:Python 资源全名:pexpect-4.4.0-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
pexpect_deploy_tools 批量远程操作脚本-- scp 和 ssh 无环境依赖,解压压缩包即可使用,较原生 expect 慢,可定制性高。 使用方法: 当前目录创建 ip list: 192.168.0.100 192.168.0.101 pscp.py 拷贝本地文件到...
离线安装包,亲测可用
很好的工具包
最初的第一个 Expect 是由 TCL 语言实现的,所以后来的 Expect 都大致参考了最初的用法和流程,整体来说大致的流程包括: 运行程序 程序要求人的判断和输入 Expect 通过关键字匹配 根据关键字向程序发送符合的字符串...
使用了python中的pexpect模块,在测试代码之前,可输入python进入交互界面,输入help(‘pexpect’),查询是否本地含有pexpect模块。 如果没有,linux系统输入 easy_install pexpect便可自动安装。 测试代码,连接...
最近需要远程操作一个服务器并执行该服务器上的一个python脚本,查到可以使用Pexpect这个库。记录一下。 什么是Pexpect?Pexpect能够产生子应用程序,并控制他们,并能够通过期望模式对子应用的输出做出反应。...
Pexpect是一个Python模块,用于生成子应用程序并自动对其进行控制。您是否需要wexpect,如果... 您想通过python脚本控制任何Windows控制台应用程序。 您要为Windows控制台应用程序编写测试自动化脚本。 您想通过...
python pexpect实践
本想直接在bash文件中使用Pexpect 来实现自动输入密码,并scp拷贝文件,但是权限的限制导致不能安装使用Pexpect 。 最后考虑使用python实现。 环境: python2.7 centos from pexpect import * import os cwd = os....
官方离线安装包,亲测可用
ipython_genutils-0.2.0.tar.gz是linux下支持ipython的tar包。
Pexpect是一个纯Python模块,可以用来和ssh、ftp、passwd、telnet等命令行命令进行交互使用,在Linux系统下尤其好用,下面我们就来具体来看一下Python安装使用命令行交互模块pexpect的基础教程:
python自动化运维框架,同时封装paramiko和pexpect