您的位置:首页 > 运维架构 > Linux

linux下交互式任务的自动化实现(expect&Pexpect)

2012-03-30 12:53 330 查看
参考:

《Linux Shell脚本攻略》 by Sarath Lakshman
http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/ http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/

一、概述

Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。Expect的作者Don Libes在1990年开始编写Expect时对Expect做有如下定义:Expect是一个用来实现自动交互功能的软件套件(Expect [is a] software suite for automating interactive tools)。使用它,系统管理员可以创建脚本来对命令或程序进行输入,而这些命令和程序是期望从终端(terminal)得到输入,一般来说这些输入都需要手工输入进行的。Expect则可以根据程序的提示模拟标准输入提供给程序需要的输入来实现交互程序执行。

二、linux下expect的安装方法

1、 首先,准备好所需的两个安装包

tcl8.4.13-src.tar.gz

expect-5.43.0.tar.gz

2、 安装tcl8.4.13

Tcl 的 configure 脚本有一个语法错误,下面的命令可以纠正它:

sed -i "s/relid'/relid/" configure (8.4.13不再有这样的错误了)

编译tcl

cd unix

./configure --prefix=/expect

make

make install

mkdir -p /tools/lib

需要的内容都拷贝到/tools/lib目录

cp tclConfig.sh /tools/lib/

安装完毕完先不要删除源码,以会安装expect还要用到

将/tools/bin目录export到环境变量,

3、 安装expect

./configure --prefix=/tools --with-tcl=/tools/lib --with-x=no

如果报

checking for Tcl private headers... checking for tclInt.h... no

configure: error: Can't find Tcl private headers

就再添加一个头文件目录参数

--with-tclinclude=../tcl8.4.13/generic,即:

./configure --prefix=/tools --with-tcl=/tools/lib --with-x=no --with-tclinclude=../tcl8.4.13/generic

脚本运行正常,进行执行make进行编译

make

编译过程中未出现错误,执行安装:

make install

编译完成后会生在/tools/bin内生成expect命令

. .bash_profile

此时在命令行执行expect应该可以执行了!

4、 完成安装

下面就可以做expect的工作了!

三、Expect工具使用方法

示例:

interactive.sh文件内容->

#! /bin/sh

read -p "Enter number:" no

read -p "Enter name:" name

echo You have entered $no, $name;

automate_expect.sh文件内容->

#! /usr/bin/expect

#1.执行需要人工交互的命令

spawn ./interactive.sh

#2.期待的提示

expect "Enter number:"

#3.对应的输入

send "1\n"

expect "Enter name:"

send "hello\n"

expect eof

四、Pexpect

1. Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现。Pexpect 可以从 SourceForge 网站下载(http://sourceforge.net/projects/pexpect/)。

2. 一般Pexpect模块可以直接在python中使用,如果不能,需要如下安装步骤:

tar zxvf pexpect-2.3.tar.g

cd pexpect-2.3

python setup.py install (do this as root)

3.使用示例:

#! /usr/bin/env python
#-*- coding: utf-8 -*-

import sys
ssh_command (sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])import pexpect

def ssh_command (user, host, password, command):
"""
This runs a command on the remote host. This could also be done with the
pxssh class, but this demonstrates what that class does at a simpler level.
This returns a pexpect.spawn object. This handles the case when you try to
connect to a new host and ssh asks you if you want to accept the public key
fingerprint and continue connecting.
"""
ssh_newkey = 'Are you sure you want to continue connecting'
# 为 ssh 命令生成一个 spawn 类的子程序对象.
child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
# 如果登录超时,打印出错信息,并退出.
if i == 0: # Timeout
print 'ERROR!'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
return None
# 如果 ssh 没有 public key,接受它.
if i == 1: # SSH does not have the public key. Just accept it.
child.sendline ('yes')
child.expect ('password: ')
i = child.expect([pexpect.TIMEOUT, 'password: '])
if i == 0: # Timeout
print 'ERROR!'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
return None
# 输入密码.
child.sendline(password)
return child

ssh_command (sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])

#调用方式,假设文件名为sshauto.py
./sshauto.py "user" "host" "password" "command"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: