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

发布时间:2021-09-14 17:56:14 作者:chen
来源:亿速云 阅读:137

这篇文章主要介绍“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环境的安装以及简单脚本测试”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. CentOS安装expect
  2. expect 发送脚本

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

expect linux

上一篇:怎么在Linux中给每个屏幕设置不同的壁纸

下一篇:css3中新特性的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》