前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >expect java,Expect 使用详解「建议收藏」

expect java,Expect 使用详解「建议收藏」

作者头像
全栈程序员站长
发布2022-11-15 10:49:38
2.8K0
发布2022-11-15 10:49:38
举报
文章被收录于专栏:全栈程序员必看

第1章 expect 概括

expect 期待

expect是Unix系统中用来进行自动化控制和测试的软件工具,由Don Libes制作,作为Tcl脚本语言的一个扩展,应用在交互式软件中如telnet,ftp,Passwd,fsck,rlogin,tip,ssh等等。该工具利用Unix伪终端包装其子进程,允许任意程序通过终端接入进行自动化控制;也可利用Tk工具,将交互程序包装在X11的图形用户界面中。

我们通过Shell可以实现简单的控制流功能,如:循环、判断等。但是对于需要交互的场合则必须通过人工来干预,比如普通用户使用sudo命令时就需要我们手动输入密码;expect就是能够完成这种自动交互任务,而无需人的干预。

第2章 使用总结:

为什么先写总结,刚才是自己使用expect也纠结了一会,总结下,在结合下面的两个案例

1、需要注意先规划好expect 大概结构,实现效果,需要参数,路径、命令

2d7ea6fa940758be3165e78e5c819dfa.png
2d7ea6fa940758be3165e78e5c819dfa.png

2、写shell脚本注意shell脚本中的变量需要对于expect中的变量.

8aa93ac8f5a2218b8477c69258d30ce8.png
8aa93ac8f5a2218b8477c69258d30ce8.png

2.1 使用例子

2.2 首先安装expect

[shell]

Centos OS yum 安装

yum install -y expect

Ubuntu 系统安装

apt-get install expect

[/shell]

2.3 例子一:SSH 自动远程支持mkdir

#提示写expect 需要两个脚本一个 .exp 和 .sh 如下:

#创建一个expect自动执行脚本

[shell]

root@xuebao shell]# cat expect_mkdir.exp

#!/usr/bin/expect

set date [lindex $argv 0]

set password [lindex $argv 1]

#spawn scp src_file username@host:dest_file

spawn ssh 192.20.3.99 mkdir /home/tbt/webappdata/backup/$date

expect {

“yes/no” {send “yes\r”;exp_continue}

“*password” {send “$password\r”}

}

expect eof

[/shell]

#脚本解释

[shell]

[root@xuebao shell]# cat expect_mkdir.exp

#!/usr/bin/expect  #解释器,告诉操作系统,使用expect必须加。

set date [lindex $argv 0] # expect脚本可以接受从shell 脚本中传递过来的参数.可以使用n从0开始,分别表示第一个,第二个,第三个….参数

set password [lindex $argv 1] #从shellz中传递密码

spawn ssh 192.20.3.99 mkdir /home/tbt/webappdata/backup/date # spawn后面加上需要执行的shell命令、其中date 是加的shell脚本中的时间变量

expect {

“yes/no” {send “yes\r”;exp_continue} #行交互动作,与手工输入密码的动作等效。

“*password” {send “$password\r”} #行交互动作,与手工输入密码的动作等效。

}

expect eof

[/shell]

注意:expect脚本必须以expect eof结束。

2.4 相对于的shell脚本

[shell]

[root@xuebao shell]# cat expect_mkdir.sh

#!/bin/sh

#####################

#by xuebao

#2017.05.27

#####################

date=`date +%Y%m%d` #定义了一个时间变量

password=123456 #传递密码

cd /home/shell #进入存放expect_mkdir.exp 的目录

./expect_mkdir.exp date password #执行并传参

[/shell]

2.5 例子2 自动SCP 命令

[shell]

[root@xuebao shell]# cat expect_app.exp

#!/usr/bin/expect

set host [lindex $argv 0]

set port [lindex $argv 1]

set username [lindex $argv 2]

set password [lindex $argv 3]

set src_file [lindex $argv 4]

set dest_file [lindex $argv 5]

#spawn scp src_file username@host:dest_file

spawn scp -P port -r src_file username@host:

expect {

“yes/no” {send “yes\r”;exp_continue}

“*password” {send “$password\r”}

}

expect eof

[/shell]

2.6 相对于的shell脚本

[shell]

[root@xuebao shell]# cat expect_app.sh

#!/bin/sh

#####################

#by xuebao

#2017.05.27

#####################

DATE=`date +%Y%m%d`

src_file=”/home/tbt/webappdata/$DATE/test* “

dest_file=”/home/tbt/webappdata/backup/$DATE/”

host=109.202.3.100

port=22

username=root

password=12345678

#scp back host

cd /home/shell

./expect_app.exp host port username password src_file dest_file

echo “end”

[/shell]

最后提示:

如果文件scp 文件过大、传送中断开,因为expect默认timeout为30S

手动添加set timeout -1设置超时时间为无穷大,问题解决

在expect 脚本中添加

d7a99bdcfdd459191b2f726420f54e86.png
d7a99bdcfdd459191b2f726420f54e86.png

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/234612.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年11月1日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档