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/
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的工作了!
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
2. 一般Pexpect模块可以直接在python中使用,如果不能,需要如下安装步骤:
tar zxvf pexpect-2.3.tar.g
cd pexpect-2.3
python setup.py install (do this as root)
3.使用示例:
《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"
相关文章推荐
- linux交互式输入自动化 read & expect
- linux脚本自动化实现远程登陆操作和传输文件(scp ssh expect)
- 【Linux】守护进程概述&cron守护进程实现定时任务
- 帮助Linux管理员简化任务并实现自动化的七大工具
- linux下使用expect实现自动化脚本
- expect入门--自动化linux交互式命令
- linux sort, uniq, cut, wc, xargs和管道'|'区别,expect自动化脚本
- 帮助Linux管理员简化任务并实现自动化的七大工具
- 使用expect在linux中实现一些自动化工作
- [推荐]Expect-[摘自Unix/Linux 平台任务的自动化]
- Linux下通过crontab及expect实现自动化处理 --亲测可用
- 【Linux笔记】linux crontab实现自动化任务
- Linux下通过crontab及expect实现自动化处理
- Linux下通过crontab及expect实现自动化处理
- Linux Daemon & 单例模式 设计与实现
- linux通过shelll编程实现自动化远程唤醒(小陈原创作品,转载请尊重作者)
- Linux中使用expect脚本实现远程机器自动登录
- Linux 下的定时任务实现 at 和 crontab
- expect实现远程用户交互式登录
- linux下expect命令实现批量ssh免密