首页
学习
活动
专区
工具
TVP
发布

自动化控制工具expect的部署

引言:

整理自多年前的博客文章

正文:

前几天下班的时候,听系统运维组的经理在给本部门的人推荐expect工具,这个工具我曾经用过,本以为现在关注的人少了,没想到它的粉丝还是很多的,借此机会,我翻出之前整理过的文档,再次发挥余热。

# create by 薛坤

# date 2011--05-13 晚

# http://www.bdkyr.com

Expect是在Tcl基础上创建起来的,它还提供了一些Tcl所没有的命令,它可以用来做一些linux下无法做到交互的一些命令操作,在远程管理方面发挥很大的作用。

spawn命令激活一个Unix程序来进行交互式的运行。

send命令向进程发送字符串。

expect命令等待进程的某些字符串。

expect支持正规表达式并能同时等待多个字符串,并对每一个字符串执行不同的操作.

#---------------------------------------------------------------------#

Other interesting scripts are available separately in the directory

http://expect.nist.gov/scripts/ (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 安装

主页: http://www.tcl.tk

下载地址: http://www.tcl.tk/software/tcltk/downloadnow84.tml

1.下载源码包

wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz

wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tk8.4.11-src.tar.gz

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的库)

主页: http://expect.nist.gov/

1.下载源码包

wgethttp://expect.nist.gov/expect-5.43.0.tar.gz

wgethttp://cdnetworks-kr-1.dl.sourceforge.net/project/buluoos/0.1/src/expect-5.43.0.tar.gz

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 xuekun-------------------------------------------------#

[root@nginx scripts]# vi expect.exp

#!/usr/expect/bin/expect

set timeout 60

set port 20222

set host 192.168.15.52

set name root

set password bdkyr

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 -p20222 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 bdkyr

#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 薛坤

#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 -P20222 "$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

#----------------------------------------------------------------------#

执行上面这个脚本的sh脚本代码如下:

#!/bin/sh

/usr/bin/expect autocopy.exp 192.168.15.53 root bdkyr /home/xuekun

/usr/bin/expect autocopy.exp 192.168.15.54 root bdkyr /home/xuekun

/usr/bin/expect autocopy.exp 192.168.15.55 root bdkyr /home/xuekun

/usr/bin/expect autocopy.exp 192.168.15.56 root bdkyr /home/xuekun

只要 execute 上面这个 sh 脚本, 就可以批量完成数据的远程copy。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180128G0CLNP00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券