千家信息网

linux下expect环境的安装以及简单脚本测试

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要介绍"linux下expect环境的安装以及简单脚本测试",在日常操作中,相信很多人在linux下expect环境的安装以及简单脚本测试问题上存在疑惑,小编查阅了各式资料,整理出简单好用的
千家信息网最后更新 2026年11月01日linux下expect环境的安装以及简单脚本测试

这篇文章主要介绍"linux下expect环境的安装以及简单脚本测试",在日常操作中,相信很多人在linux下expect环境的安装以及简单脚本测试问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"linux下expect环境的安装以及简单脚本测试"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、安装expect和tcl

(1)、解压tcl,进入tcl解压目录,然后进入unix目录进行编译安装

[root@slient tmp]# tar -xzvf tcl8.4.11-src.tar.gz

..................................

[root@slient tmp]# cd tcl8.4.11/unix/

[root@slient unix]# ./configure

..................................

[root@slient unix]# make && make install

..................................

(2)、安装expect

[root@slient tmp]# tar -xzvf expect-5.43.0.tar.gz

..................................

[root@slient tmp]# cd expect-5.43

[root@slient expect-5.43]# ./configure --with-tclinclude=/tmp/tcl8.4.11/generic --with-tclconfig=/usr/local/lib/

..................................

[root@slient expect-5.43]# make && make install

..................................

(3)、安装完成后进行测试

[root@slient expect-5.43]# expect

expect1.1>

expect1.1>

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

示例:从本机自动登录到远程机器192.168.56.12(端口是22,密码是:oracle)

登录到远程机器后做以下几个操作:

1)useradd wangshibo

2)mkdir /opt/test

3) exit自动退出

[root@slient ~]# cat test-ssh.sh

#!/bin/bash

passwd='oracle'

/usr/local/bin/expect <<-EOF

set time 30

spawn ssh -p22 root@192.168.56.12

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

[root@slient ~]#

--执行:

[root@slient ~]# sh test-ssh.sh

spawn ssh -p22 root@192.168.56.12

The authenticity of host '192.168.56.12 (192.168.56.12)' can't be established.

RSA key fingerprint is 16:8d:5a:fb:f2:58:e1:ee:4c:98:3d:76:ec:48:bb:46.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.56.12' (RSA) to the list of known hosts.

root@192.168.56.12's password:

Last login: Tue Jan 30 15:58:08 2018 from 192.168.56.1

[root@one ~]# useradd wangshibo

[root@one ~]# mkdir /opt/test

[root@one ~]#

[root@slient ~]#

[root@slient ~]#

上面的例子如果只是自动登陆,登陆机器后不做操作的脚本内容如下:

shell脚本的写法:

[root@slient ~]# cat test.sh

#!/bin/bash

passwd='oracle'

/usr/local/bin/expect <<-EOF

set time 30

spawn ssh -p22 root@192.168.56.12

expect {

"*yes/no" { send "yes\r"; exp_continue }

"*password:" { send "$passwd\r" }

}

expect eof

EOF

[root@slient ~]#

--执行:

[root@slient ~]# sh test.sh

spawn ssh -p22 root@192.168.56.12

root@192.168.56.12's password:

Last login: Tue Jan 30 16:03:23 2018 from 192.168.56.20

[root@one ~]#

expect脚本的写法:

[root@slient ~]# cat test

#!/usr/local/bin/expect

set timeout 30

spawn ssh -p22 root@192.168.56.12

expect "*password:"

send "oracle\r"

interact

[root@slient ~]#

--执行:

[root@slient ~]# ./test

spawn ssh -p22 root@192.168.56.12

root@192.168.56.12's password:

Last login: Tue Jan 30 16:07:23 2018 from 192.168.56.20

[root@one ~]#

注意:spawn后面跟的是操作动作,比如登陆机器后执行uptime,即:spawn ssh -p22 root@192.168.1.201 "uptime"

三、示例使用expetc自动化传送文件到远程主机目录上

[oracle@slient ~]$ cat sftp.sh

#!/usr/local/bin/expect

expect<

spawn sftp oracle@192.168.56.12

expect "password:"

send "oracle\n";

expect "sftp>"

send "lcd /home/oracle/dmp\n";

expect "sftp>"

send "cd /home/oracle/soft\n";

expect "sftp>"

send "put tb_pt.dmp\n";

expect "sftp>"

send "exit\n"

interact

!

[oracle@slient ~]$

[oracle@slient ~]$ chmod +x sftp.sh

--执行:

[oracle@slient ~]$ sh sftp.sh

spawn sftp oracle@192.168.56.12

Connecting to 192.168.56.12...

oracle@192.168.56.12's password:

sftp> lcd /home/oracle/dmp

sftp> cd /home/oracle/soft

sftp> put tb_pt.dmp

Uploading tb_pt.dmp to /home/oracle/soft/tb_pt.dmp

tb_pt.dmp 100% 216KB 216.0KB/s 00:00

sftp> [oracle@slient ~]$

[oracle@slient ~]$

--验证:

[oracle@one soft]$ pwd

/home/oracle/soft

[oracle@one soft]$

[oracle@one soft]$ ls

[oracle@one soft]$ pwd

/home/oracle/soft

[oracle@one soft]$ ls

tb_pt.dmp

[oracle@one soft]$

四、示例解释

例子:

#!/usr/bin/expect

#set timeout 20 #设置超时时间

spawn ssh root@192.168.43.131

expect "*password:"

send "123\r"

# expect "*#"

interact

解释:

1.#!/usr/bin/expect :需要先安装软件,然后来说明用expect来执行

2.spawn ssh root@192.168.43.131 :spawn是进入expect环境后才可以执行的expect内部命令,用来执行它后面的命令。

3.expect "*password:" :也是expect的内部命令,用来解惑关键的字符串,如果有,就会立即返回下面设置的内容,如果没有就看是否设置了超时时间。

4.send "123\r":这时执行交互式动作,与手工输入密码等效,在expect截获关键字之后,它就会输入send后面的内容。

5.interact :执行完毕后把持交互状态,把控制台,这时候就可以进行你想要进行的操作了。如果没有这一句,在登陆完成之后就会退出,而不是留在远程终端上。

到此,关于"linux下expect环境的安装以及简单脚本测试"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

脚本 测试 环境 机器 学习 登陆 内容 命令 目录 示例 例子 关键 写法 动作 密码 时间 更多 帮助 登录 解释 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 伟宏互联网科技 互联网科技网 互联网科技工作委员会主任 广州百单互联网科技有限公司 重生之互联网科技帝国 河南真二互联网科技有限公司 新浪科技互联网 菲比寻常互联网科技 上游互联网科技 互联网巨头投资的硬核科技 哈尔滨秒房互联网科技有限公司 太原新华互联网科技学校怎么样 中盈科技互联网信息有限公司 湖南诚度互联网科技有限公司 花神(南京)互联网科技有限公司 广州富喜互联网络科技 本届互联网大会上的黑科技 广西泛欧互联网科技有限公司 互联网科技品牌 山东亿川互联网科技有限责任公司 云信互联网科技有限公司 居家互联网科技有限公司 蓝色互联网科技 互联网大会黑科技排名 健康食品互联网创新科技园项目 2021互联网科技公司排名 雨阳互联网卡科技有限公司 互联网安防科技感 云鲸互联网络科技有限公司 科技发展第三阶段互联网时代
0