HOME 生活记录运维/编程
作者/来源:yixinu.com
栏目:运维/编程
日期:2012-04-09 02:19:10

一、概述

    我们通过Shell可以实现简单的控制流功能,如:循环、判断等。但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如telnet服务器等进行交互的功能。而Expect就使用来实现这种功能的工具。

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


安装软件包:

    安装tcl

    

[root@fedora14 ~]# yum list | grep tcl
tcl.i686                                 1:8.5.10-1.fc16               @fedora14
tcl-brlapi.i686                          0.5.5-4.fc15                  fedora14 
[root@fedora14 ~]# yum install tcl
    安装expect


    我在 http://rpmfind.net/linux/rpm2html/search.php 这个网站下载了这个版本


[root@fedora14 ~]# ls expect-5.44.1.15-2.fc14.i686.rpm 
expect-5.44.1.15-2.fc14.i686.rpm
[root@fedora14 ~]# rpm -ivh expect-5.44.1.15-2.fc14.i686.rpm 
example:

    拷贝文件到指定主机的不同用户目录

1、编写expect脚本


[root@fedora14 expect]# vim ep2.expect 
#!/usr/bin/expect -f
#设置变量
set user [ lindex $argv 0  ]
set pwd [ lindex $argv 1  ]
set host [ lindex $argv 2  ]

spawn scp /root/names $user@$host:/home/$user
expect "(yes/no)?"
send "yesr"
expect "password:"
send "$pwdr"
expect eof

   set user [lindex $argv 0]  用来获得脚本的执行参数(其保存在数组$argv中,从0号开始是参数),并将其保存到变量user中

   spawn scp 表示使用Expect的spawn命令来启动脚本和命令的会话,这里启动的是scp命令,实际上命令是以衍生子进程的方式来运行的。

   随后的expect和send命令用来实现交互过程。

   一旦接收到标识子进程已经结束的eof字符,expect脚本也就退出结束。


2、编写配置文件


[root@fedora14 expect]# vim ssy.conf
192.168.73.173:root:administrator
192.168.73.173:user:user
192.168.73.173:centos:centos


3、编写awk脚本



[root@fedora14 expect]# vim ssy.awk
#!/bin/awk
/^[^#]/
{
        system("rm -f ~/.ssh/k* &>/dev/null");
        cmd=sprintf("./ep2.expect %s %s %s",$2,$3,$1);
        print cmd;
        system(cmd);
}


4、编写bash脚本


[root@fedora14 expect]# vim ssy.sh
#!/bin/bash

rm -f ~/.ssh/k* &>/dev/null
gawk -F: -f ./ssy.awk ./ssy.conf


5、执行bash脚本



[root@fedora14 expect]# ./ssy.sh
./ep2.expect root administrator 192.168.73.173
spawn scp /root/names root@192.168.73.173:/home/root
The authenticity of host '192.168.73.173 (192.168.73.173)' can't be established.
RSA key fingerprint is 7f:a4:5a:c8:8a:cb:03:af:3c:09:50:80:85:39:49:19.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.73.173' (RSA) to the list of known hosts.
root@192.168.73.173's password: 
names                                                            100%   80     0.1KB/s   00:00    
./ep2.expect user user 192.168.73.173
spawn scp /root/names user@192.168.73.173:/home/user
The authenticity of host '192.168.73.173 (192.168.73.173)' can't be established.
RSA key fingerprint is 7f:a4:5a:c8:8a:cb:03:af:3c:09:50:80:85:39:49:19.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.73.173' (RSA) to the list of known hosts.
user@192.168.73.173's password: 
names                                                            100%   80     0.1KB/s   00:00    
./ep2.expect centos centos 192.168.73.173
spawn scp /root/names centos@192.168.73.173:/home/centos
The authenticity of host '192.168.73.173 (192.168.73.173)' can't be established.
RSA key fingerprint is 7f:a4:5a:c8:8a:cb:03:af:3c:09:50:80:85:39:49:19.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.73.173' (RSA) to the list of known hosts.
centos@192.168.73.173's password: 
names                                                            100%   80     0.1KB/s   00:00    
[root@fedora14 expect]# 






资料

http://linux.chinaitlab.com/c/809655.html

分享到:

Copyright © 2013-2014 yixinu.com 湘ICP备14004402号

QQ:316686606  Email: 316686606@qq.com