linux下expect环境安装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux下expect环境安装相关的知识,希望对你有一定的参考价值。

 

expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,很实用!
expect依赖于tcl,而linux系统里一般不自带安装tcl,所以需要手动安装

下载:expect-5.43.0.tar和tcl8.4.11-src.tar
下载地址:https://pan.baidu.com/s/1kVyeLt9
提取密码:af9p

 

将expect和tcl的软件包下载放到/usr/local/src目录下

(1)解压tcl,进入tcl解压目录,然后进入unix目录进行编译安装
[[email protected] src]# tar -zvxf tcl8.4.11-src.tar.gz
[[email protected] src]# cd tcl8.4.11/unix
[[email protected] unix]# ./configure
[[email protected] unix]# make && make

(2)安装expect
[[email protected] src]# tar -zvxf expect-5.43.0.tar.gz
[[email protected] src]# cd expect-5.43.0
[[email protected] expect-5.43.0]# ./configure --with-tclinclude=/usr/local/src/tcl8.4.11/generic --with-tclconfig=/usr/local/lib/
[[email protected] expect-5.43.0]# make && make install

(3)安装完成后进行测试
[[email protected] ~]# expect
expect1.1>
expect1.1>

 

----------------------------------------------------------------------------------------------------

下面结合shell脚本做简单测试:

从本机自动登录到远程机器192.168.1.200(端口是22,密码是:PASSWORD)
登录到远程机器后做以下几个操作:
1)useradd wangshibo
2)mkdir /opt/test
3) exit自动退出


[[email protected] tmp]# cat test-ssh.sh
#!/bin/bash
passwd=‘PASSWORD‘
/usr/local/bin/expect <<-EOF
set time 30
spawn ssh -p22 [email protected]
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

 

[[email protected] tmp]# sh test.sh
spawn ssh -p22 [email protected]
[email protected]‘s password:
Last login: Fri Sep 23 16:21:20 2016 from 192.168.1.23
[[email protected] ~]# useradd wangshibo
[[email protected] ~]# mkdir /opt/test
[[email protected] ~]# [[email protected] tmp]#

 

以上是关于linux下expect环境安装的主要内容,如果未能解决你的问题,请参考以下文章

Linux环境用shell调用Dubbo服务

Linux下安装gcc环境作用是啥?

linux下怎么安装Go开发环境

linux下怎么安装Go开发环境

linux下怎么安装java环境

Linux环境下安装JDK并配置环境变量