Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1340069
  • 博文数量: 166
  • 博客积分: 46
  • 博客等级: 民兵
  • 技术积分: 4061
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-11 13:45
个人简介

现任职北京某互联网公司运维经理,高级架构师,涉足互联网运维行业已经超过10年。曾服务于京东商城,互动百科等互联网公司,早期运维界新星。 长期专研,C语言开发,操作系统内核,大型互联网架构。http://www.bdkyr.com

文章分类

分类: 系统运维

2013-10-13 10:37:34

         前几天下班的时候,听系统运维组的经理在给本部门的人推荐expect工具,这个工具我曾经用过,本以为现在
关注的人少了,没想到它的粉丝还是很多的,借此机会,我翻出之前整理过的文档,再次发挥余热。
##########################################
# create by maidong
# date 2011--05-13  晚
#
#
      Expect是在Tcl基础上创建起来的,它还提供了一些Tcl所没有的命令,
它可以用来做一些linux下无法做到交互的一些命令操作,在远程管理
方面发挥很大的作用。
spawn命令激活一个Unix程序来进行交互式的运行。 
send命令向进程发送字符串。
expect命令等待进程的某些字符串。 
expect支持正规表达式并能同时等待多个字符串,并对每一个字符串执行不同的操作.

#-------------------------------------------------------------------------#
Other interesting scripts are available separately in the directory
(ftpable as
ftp://ftp.nist.gov/mel/div826/subject/expect/scripts).  (See below for
how to retrieve these.)  You are welcome to send me scripts to add to
this directory.  A number of Expect scripts are also available in the
Tcl archive, available at ftp://ftp.neosoft.com/pub/tcl.
#----------------操作步骤----------------------------------------------------#
A. Tcl 安装
主页:
下载地址: /software/tcltk/downloadnow84.tml

1.下载源码包
wget
wget

2.解压缩源码包
#tar xfvz tk8.4.11-src.tar.gz   #暂时可以不用
tar xfvz tcl8.4.11-src.tar.gz
3.安装配置
cd tcl8.4.11
cd unix
./configure --prefix=/usr/tcl --enable-shared
make && make install

4.安装完毕以后,进入tcl源代码的根目录,把子目录unix下面的tclUnixPort.h copy到子目录generic中。
  暂时不要删除tcl源代码,因为expect的安装过程还需要用。
cp tclUnixPort.h ../generic/

B. expect 安装 (需Tcl的库)
主页:

1.下载源码包
wget expect-5.43.0.tar.gz
wget

2.解压缩源码包
tar xfvz expect-5.43.0.tar.gz

3.安装配置
cd expect-5.43
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib  --with-tclinclude=/home/xuekun/tools/tcl8.4.11/generic/
make
make install
#--实例1.1----by maidong-------------------------------------------------#
[root@nginx scripts]# vi expect.exp

#!/usr/expect/bin/expect
set timeout 60
set port 50718
set host 192.168.15.52
set name root
set password dn3s
spawn ssh -p$port  $host -l $name
expect  {
         "(yes/no)?" {
            send "yes\r"
            expect "password:"
            send "$password\r"
          }
        "password:" { send "$password\r" }

}
expect "#"
send "uname\n"
expect "Linux"
send_user "Now you can do some operation on this terminal\n"

[root@nginx scripts]# /usr/expect/bin/expect expect.exp
spawn ssh -p50718 192.168.15.52 -l root
root@192.168.15.52's password:
Last login: Sun May 15 05:41:26 2011 from 192.168.15.51
[root@lamp-002 ~]# uname
Linux
Now you can do some operation on this terminal
[root@nginx scripts]#

#------------------------------------------------------------------------------#
#说明
 #!/usr/bin/expect

 # 设置超时时间为 60 秒
 set timeout  60
 # 设置要登录的主机 IP 地址
 set host 192.168.15.52
 # 设置以什么名字的用户登录
 set name root
 # 设置用户名的登录密码
 set password dn3s
 #spawn 一个 ssh 登录进程
 spawn  ssh $host -l $name
 # 等待响应,第一次登录往往会提示是否永久保存 RSA 到本机的 know hosts 列表中;
 #等到回答后,在提示输出密码;之后就直接提示输入密码
 expect {
    "(yes/no)?" {
        send "yes\n"
        expect "Password:"
        send "$pasword\n"
    }
    "Password:" {
        send "$password\n"
    }
 }
 expect "#"
 # 下面测试是否登录到 $host
 send "uname\n"
 expect "Linux"
 send_user  "Now you can do some operation on this terminal\n"
 exit
#-----------------finish-------------------------------------------------#
#--实例1.2--expect交互式scp文件--------------by xuekun--------------------#
[xuekun@expect ~]$ cat scp.exp
#!/usr/expect/bin/expect
#modify by xk
#201105-30
#
set ADDR    [lindex $argv 0]   
set LOGIN   [lindex $argv 1]   
set PASSWD  [lindex $argv 2]   
set DIR     [lindex $argv 3]   
 
spawn scp -rp -P50718  "$DIR"  "$LOGIN@$ADDR:/tmp"   
set timeout 60   
 
expect {   
-re ".*es.*o.*" {   
exp_send "yes\r"   
exp_continue   
}   
-re ".*sword.*" {   
exp_send "$PASSWD\r"   
}   
}   
interact  
#--运行测试-------------------------------------------------------------------------------#
[xuekun@expect ~]$ /usr/expect/bin/expect scp.exp 192.168.15.52 root xuekun /home/xuekun/
说明:输入4个参数, $DIR 为要copy到远程服务器的目录或者文件
$LOGIN 为用户名, 通过expect 自动输入yes 和 password
#-----------------------------------------------------------------------------------------#
执行上面这个脚本的代码如下:
#!/bin/sh   
/usr/bin/expect autocopy.exp 192.168.15.53 root 123456 /home/xuekun   
/usr/bin/expect autocopy.exp 192.168.15.54 root 123456 /home/xuekun   
/usr/bin/expect autocopy.exp 192.168.15.55 root 123456 /home/xuekun   
/usr/bin/expect autocopy.exp 192.168.15.56 root 123456 /home/xuekun  

只要 execute 上面这个 sh 脚本, 就可以批量完成数据的远程copy。
阅读(4794) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~