ITPub博客

首页 > Linux操作系统 > Linux操作系统 > linux下expect环境安装以及简单脚本测试

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

Linux操作系统 作者:不一样的天空w 时间:2018-12-03 17:55:05 0 删除 编辑

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

https://www.cnblogs.com/kevingrace/p/5900303.html


linux expect详解(ssh自动登录)

https://www.cnblogs.com/dion-90/articles/8570601.html


expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量部署工具!

expect依赖于tcl,而linux系统里一般不自带安装tcl,所以需要手动安装


下载:expect-5.43.0.tar和tcl8.4.11-src.tar

下载地址:https://pan.baidu.com/s/1kVyeLt9 

提取密码:af9p


将expect和tcl的软件包下载放到root用户下的/tmp目录下


一、安装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 :执行完毕后把持交互状态,把控制台,这时候就可以进行你想要进行的操作了。如果没有这一句,在登陆完成之后就会退出,而不是留在远程终端上。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31397003/viewspace-2222571/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-09-22

  • 博文量
    916
  • 访问量
    1400249