一、介绍
expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量部署工具!
expect依赖于tcl,而linux系统里一般不自带安装tcl,所以需要手动安装。
软件包下载:http://pacap.paic.com.cn/software/download/expect-5.43.0.tar-tcl8.4.11-src.tar.zip
二、安装
将expect和tcl的软件包下载放到/usr/local/src目录下
(1)解压tcl,进入tcl解压目录,然后进入unix目录进行编译安装
[root@xw4 src]# tar -zvxf tcl8.4.11-src.tar.gz
[root@xw4 src]# cd tcl8.4.11/unix
[root@xw4 unix]# ./configure
[root@xw4 unix]# make && make install
(2)安装expect
[root@xw4 src]# tar -zvxf expect-5.43.0.tar.gz
[root@xw4 src]# cd expect-5.43.0
[root@xw4 expect-5.43.0]# ./configure --with-tclinclude=/usr/local/src/tcl8.4.11/generic --with-tclconfig=/usr/local/lib/
[root@xw4 expect-5.43.0]# make && make install
(3)安装完成后进行测试
[root@xw4 ~]# expect
expect1.1>
三、测试
从本机自动登录到远程机器192.168.1.200(端口是22,密码是:PASSWORD)
登录到远程机器后做以下几个操作:
1)useradd wangshibo
2)mkdir /opt/test
3) exit自动退出
[root@xw4 tmp]# cat test-ssh.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#!/bin/bash
passwd
=
'PASSWORD'
/usr/local/bin/expect
<<-EOF
set
time
30
expect {
"*yes/no"
{ send
"yes\r"
; exp_continue }
"*password:"
{ send
"$passwd\r"
}
}
expect
"*#"
send
"useradd wangshibo\r"
expect
"*#"
send
"mkdir /opt/test\r"
expect
"*#"
send
"exit\r"
interact
expect eof
EOF
|