expect的使用
1. expect概述
1.1 expect的功能
脚本执行时,有时会需要人工进行交互输入,这时可以通过expect工具来实现自动交互。
expect是一种shell解释器,但是expect可以在命令交互时捕捉指定的内容,然后再输出指定的内容。
1.2 安装expect
yum install -y expect
1.3 一些基本的expect关键字
# 设置expect捕捉的超时时间,单位为秒
set timeout 10 # 使用expect,一般与重定向结合使用,以达到在bash解释器下也能执行捕捉
/usr/bin/expect # 启动新进程,用于执行shell命令
spawn # 与spawn结合使用,使用exp_continue进行多次捕捉
expect {“等待捕捉的内容” {send "需要输入的内容"}} # 发送输入的内容
send # 继续捕捉,不断开会话
exp_continue # 允许用户交互,即此命令之后,交互将不会由expect进行,而是交回给用户
interact
2. expect使用示例
2.1 mysql初始化
#!/bin/bash
# Description: Initialize MySQL Server
# Root password
user='root'
password='woshiniba' set timeout 10 function initMysql() {
/usr/bin/expect << EOF # 表示以下内容将传递给 /usr/bin/expect 程序来执行
spawn /usr/bin/mysql_secure_installation # 表示用spawn来执行这个命令,spawn只能在expect环境中才能执行
expect {
"Enter current password for root (enter for none):" {send "\r";exp_continue} # 这里\r表示回车,也可以使用\n
"Set root password?" {send "Y\r";exp_continue} # exp_continue表示还要继续保持捕捉动作(会话)
"New password" {send "$password\r";exp_continue}
"Re-enter new password" {send "$password\r";exp_continue}
"Remove anonymous users?" {send "Y\r";exp_continue}
"Disallow root login remotely?" {send "n\r";exp_continue}
"Remove test database and access to it?" {send "Y\r";exp_continue}
"Reload privilege tables now?" {send "Y\r";exp_continue}
}
expect eof # 表示结束expect
EOF
} initMysql
2.2 批量做免密登录
#!/bin/bash
# Description: Ansible SSH password free login configuration
# Author: Praywu
# Blog: https://cnblogs.com/hgzero server=('172.18.0.150' '172.18.0.202')
passwd='woshiniba'
key='/root/.ssh/id_rsa' function genKey(){
[ -e "$key" ] || ssh-keygen -t rsa -P "" -f $key
} function connServer(){
/usr/bin/expect << EOF
spawn /usr/bin/ssh-copy-id -i ${key}.pub root@$1
expect {
"want to continue connecting (yes/no)?" {send "yes\r";exp_continue}
"s password" {send "${passwd}\r";exp_continue}
}
EOF
} function exec(){
genKey && echo -e "\033[32m[INFO]\033[0m Generate key OK!" || echo -e "\033[31m[ERROR]\033[0m Generate key Failed!"
set timeout 15
for i in $(seq 0 $((${#server[*]}-1)));do
connServer "${server[$i]}" &> /dev/null
[[ $? -eq 0 ]] && echo -e "\033[32m[INFO]\033[0m Get ${server[$i]} Success!" || echo -e "\033[32m[INFO]\033[0m Get ${server[$i]} Failed!"
done
} function clear(){
for i in $(seq 0 $((${#server[*]}-1)));do
sed -i "/${server[$i]}/d" /root/.ssh/known_hosts
done
} function help(){
echo "Usage: $0 [ exec | clear ]"
echo -e "Ansible SSH password free login configuration \n"
} function main(){
case $1 in
exec)
exec;;
clear)
clear;;
*)
help;;
esac
} main $1
2.3 其他更详细内容
expect的使用的更多相关文章
- expect用法
1. [#!/usr/bin/expect] 这一行告诉操作系统脚本里的代码使用那一个shell来执行.这里的expect其实和linux下的bash.windows下的cmd是一类东西. 注意: ...
- expect基本使用方法
参考: http://www.cnblogs.com/lzrabbit/p/4298794.html expect是linux系统中可以和子进程进行交互的一个命令,使用它可以做一些自动化工作.pyth ...
- expect实现自动登录
自动登录主机(ssh) 建脚本item2login.sh,包含如下内容 #!/usr/bin/expect set timeout 30 spawn ssh -p [lindex $argv 0] [ ...
- 自动化运维 Expect
Mac 下载:brew install homebrew/dupes/expect expect : -> 自动化脚本工具: 用于处理交互命令; #注意 调用时并不是使用的 /bin/b ...
- expect
#!/usr/bin/expect -fset ipaddr "192.168.5.4"set passwd "123qwe"set timeout 30 sp ...
- expect命令问题1
[root@localhost scripts]# cat exptest.sh #!/usr/bin/expect spawn ssh root@127.0.0.1 expect "pas ...
- expect脚本语言用法示例
#!/usr/bin/expect set hostname [lindex $argv 0] set username [lindex $argv 1] set passwd [lindex $ar ...
- 测试框架Mocha与断言expect
测试框架Mocha与断言expect在浏览器和Node环境都可以使用除了Mocha以外,类似的测试框架还有Jasmine.Karma.Tape等,也很值得学习. 整个项目源代码: 为什么学习测试代码? ...
- Linux - expect自动化远程登录脚本
简单模式: #!/usr/bin/expect -f spawn ssh root@192.168.0.1 expect "*assword*" send "root\r ...
- 使用 expect 命令执行自动分发系统
一.命令 except 实例详解 1. 介绍 expect 使用场景 expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令.当然若是使用不带密码的密钥验证同样可以实现自动登录和自动 ...
随机推荐
- 移动端 better-scroll基础
一.什么是better-scroll better-scroll 是一款重点解决移动端(已支持 PC)各种滚动场景需求的插件 #滚动原理 1. 与浏览器滚动原理一致,父容器高度固定,子元素内容撑开,必 ...
- 用php简单区别泛解析
<?php header('content-type:text/html; charset=utf-8'); $host = '58.com'; $hosts = gethostbyname(' ...
- dropload.min.js 下拉刷新后,无法上拉加载更多
使用方法 1.引入文件 <script src="/app/media/js/dropload.min.js"></script> 111111111111 ...
- CentOS7 实战源码安装mysql5.7.17数据库服务器
CentOS7 实战源码安装mysql5.7.17数据库服务器 简介:实战演练mysql数据库服务器的搭建 mysql简介: mysql是一个开源的关系型数据库管理系统,现在是oracle公司旗下的 ...
- 基于docker,consul,consul-template, registrator, nginx服务注册发现集群
介绍 该工程主要实现服务的自动注册发现,从而达到提高运维效率,做到服务的自动发现和动态扩展. 服务注册发现 服务启动后自动被发现 动态变更负载均衡 自动伸缩 工具 1.Registrator 这是 ...
- Mybatis-Plus的Service方法使用 之 泛型方法default <V> List<V> listObjs(Function<? super Object, V> mapper)
首先 我们先看到的这个方法入参是:Function<? super Object , V> mapper ,这是jdk1.8为了统一简化书写格式引进的函数式接口 . 简单 解释一下我对Fu ...
- Singleton Pattern -- 不一样的单例模式
Singleton Pattern -- 单例模式 单例模式是用来创建一个只能又一个实例的对象. 单例模式类图如下. 单例模式有两大好处: (1)对于频繁使用的对象,可以省略创建对象所话费的时间,这对 ...
- Echarts数据可视化,easyshu图表集成。
介绍: ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Sa ...
- 广告计价方式:CPM,CPC,CPA
转自知乎链接:https://www.zhihu.com/question/20416888/answer/15076251 1.CPM 收费最科学的办法是按照有多少人看到你的广告来收费.按访问人次收 ...
- SQL数据库创建,创建表,增删改查
创建数据库:create datebase数据库名 删除数据库:drop datebase 数据库名称 创建表格式: create table 表名(字段名1,字段类型1,字段名2,字段类型2) 查询 ...