Shell 编程 免交互 expect

本篇主要写一些shell脚本免交互expect的使用。
概述
Expect是建立在tcl基础上的一个工具,Expect 是用来进行自动化控制和测试的工具。主要解决shell脚本中不可交互的问题。
安装
- 使用此工具前需先安装
yum install -y expect
基本命令
send
向进程发送字符串,用于模拟用户的输入
该命令不能自动回车换行,一般要加
\r(回车)
expect
expect的一个内部命令,判断上次输出结果里是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回。只能捕捉由
spawn启动的进程的输出
spawn
- 启动进程,并跟踪后续交互信息
interact
- 执行完成后保持交互状态,把控制权交给控制台
timeout
指定超时时间,过期则继续执行后续指令
单位是:秒
timeout -1永不超时默认情况下,
timeout是10秒
exp_continue
- 允许
expect继续向下执行指令
send_user
- 回显命令,相当于
echo
$argv 参数数组
expect脚本可以接受从bash传递的参数.可以使用[lindex $argv n]获得,n从0开始,分别表示第一个,第二个,第三个...参数
expect 脚本
expect脚本必须以interact或expect eof结束,执行自动化任务通常expect eof就够了expect eof是在等待结束标志。由spawn启动的命令在结束时会产生一个eof标记,expect eof即在等待这个标记
expect 语法
- 单分支
expect "password:" {send "mypassword\r";}
- 多分支
expect "aaa" {send"AAA\r"}
expect "aaa" {send"AAA\r"}
expect "aaa" {send"AAA\r"}
send命令不具备回车换行功能,一般要加\r或\n
expect {
"aaa" {send "AAA\r"}
"bbb" {send "BBB\r"}
"ccc" {send "CCC\r"}
}
只要配置
aaa或bbb或ccc中的任何一个,执行相应的send语句后退出该expect语句
expect {
"aaa" {send "AAA";exp_continue}
"bbb" {send "BBB";exp_continue}
"ccc" {send "CCC"}
}
exp_continue表示继续后面的匹配,如果匹配了aaa,执行完send语句后还要继续向下匹配bbb
执行方式
- 基本语法结构
spawn 命令
expect "提示信息"
send "代替人工输入的字符串\r"
- 直接执行
#!/usr/bin/expect
# 超时时间
set timeout 20
log_file test.log
log_user 1
# 参数传入
set hostname [lindex $argv 0]
set password [lindex $argv 1]
# 追踪命令
spawn ssh root@$hostname
# 捕捉信息并匹配,免交互执行
expect {
"(yes/no)" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
# 控制权交给控制台执行
interact
[root@host01 ~]# yum install expect -y
[root@host01 ~]# vim ssh.sh
[root@host01 ~]# chmod +x ssh.sh
[root@host01 ~]# ./ssh.sh 192.168.28.129 000000
spawn ssh root@192.168.28.129
The authenticity of host '192.168.28.129 (192.168.28.129)' can't be established.
ECDSA key fingerprint is SHA256:QmZtJT0piBUSkF9P3GfYf3uEogzBWs08sI7j0eBE/cI.
ECDSA key fingerprint is MD5:ef:e6:06:22:8a:0f:24:00:f8:af:a5:59:5b:a2:b8:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.28.129' (ECDSA) to the list of known hosts.
root@192.168.28.129's password:
Last login: Thu Oct 17 09:35:35 2019
[root@host02 ~]#
- 嵌入执行
#!/bin/bash
hostname=$1
password=$2
/usr/bin/expect <<-EOF
spawn ssh root@$hostname
expect {
"(yes/no)" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
expect "*]#"
send "exit\r"
expect eof
EOF
-EOF只能容错制表符tab
[root@host01 ~]# vim ssh.sh
[root@host01 ~]# ./ssh.sh 192.168.28.129 000000
spawn ssh root@192.168.28.129
root@192.168.28.129's password:
Last login: Thu Oct 17 09:38:23 2019 from 192.168.28.128
[root@host02 ~]# exit
logout
Connection to 192.168.28.129 closed.
[root@host01 ~]#
案例1 useradd
#!/bin/bash
username=$1
password=$2
useradd $username
/usr/bin/expect << EOF
spawn passwd $username
expect "New password:"
send "$password\r"
expect "Retype new password:"
send "$password\r"
expect eof
EOF
[root@host01 ~]# vim useradd.sh
[root@host01 ~]# chmod +x useradd.sh
[root@host01 ~]# ./useradd.sh zhangsan 000000
spawn passwd zhangsan
Changing password for user zhangsan.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
案例2 ssh
#!/usr/bin/expect
# 超时时间
set timeout 20
log_file test.log
log_user 1
# 参数传入
set hostname [lindex $argv 0]
set password [lindex $argv 1]
# 追踪命令
spawn ssh root@$hostname
# 捕捉信息并匹配,免交互执行
expect {
"Connection refused" exit
"Name or service not known" exit
"(yes/no)" {send "yes\r";exp_continue}
"*password" {send "$password\r"}
}
# 控制权交给控制台执行
interact
exit
[root@host02 ~]# systemctl stop sshd
[root@host01 ~]# ./ssh.sh 192.168.28.129 000000
spawn ssh root@192.168.28.129
ssh: connect to host 192.168.28.129 port 22: Connection refused
[root@host01 ~]#
[root@host02 ~]# systemctl start sshd
[root@host01 ~]# ./ssh.sh host02 000000
spawn ssh root@host02
ssh: Could not resolve hostname host02: Name or service not known
[root@host01 ~]#
[root@host01 ~]# ./ssh.sh 192.168.28.129 000000
spawn ssh root@192.168.28.129
root@192.168.28.129's password:
Last login: Thu Oct 17 09:49:38 2019
[root@host02 ~]#
Shell 编程 免交互 expect的更多相关文章
- shell中expect免交互
expect前言观察ssh登录的交互现象有些程序难以避免的需要交互.你该如何解决脚本与程序的交互问题?名词解释期待, 预期, 盼望, 预料,料想, 指望, 希望, 要求,想, 认为一.概述 我们通过S ...
- shell编程之免交互 (不要再让你的双手过度劳累了)
shell编程之免交互 1.Here Document免交互 2.Expect免交互 3.免交互磁盘创建 1.Here Document免交互 概述: Here Document使用I/O重定向的方式 ...
- 8.shell编程之免交互
shell编程之免交互 目录 shell编程之免交互 Here Document免交互 免交互定义 Here Document变量设定 多行的注释 expect expect 定义 expect基本命 ...
- shell编程之免交互
目录: 一.Here Document 免交互 二.Expect 一.Here Document 免交互 使用I/O重定向的方式将命令列表提供给交互式程序或命令, 比如 ftp.cat 或 read ...
- shell编程-ssh免交互批量分发公钥脚本
脚本基本原理 1.控制端免交互创建秘钥和公钥: 1 ssh-keygen -t rsa -f /root/.ssh/id_rsa -N "" 2.免交互发送公钥 1 sshpass ...
- linux免交互登陆远程主机并执行命令(密钥对和Expect)
原文章摘自:http://lizhenliang.blog.51cto.com/7876557/1607723/ Linux下实现免交互登陆一般有两种: 1. SSH无密码认证方式 客户端使用ssh- ...
- centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课
centos shell编程4[分发系统] 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要 ...
- expect实现免交互
如果想写一个能够自动处理输入输出的脚本又不想面对C或Perl,那么expect是最好的选择.它可以用来做一些Linux下无法做到交互的一些命令操作. (1).安装和使用expect expect是不会 ...
- expect命令和here document免交互
目录 一.Here Document免交互 1.1 概述 1.2 语法格式 1.3 简单案例 1.4 支持变量替换 1.5 多行注释 1.6 完成自动划分磁盘免交互 二.Expect进行免交互 2.1 ...
随机推荐
- WARN Connection to node 2 could not be established. Broker may not be available. (
启动 kafka 集群, 出现这个问题 WARN Connection to node 2 could not be established. Broker may not be available. ...
- 关于git设置多个账号的问题,ideal中clone项目
一.排坑 1.ping github.com超时.修改C:\Windows\System32\drivers\etc的hosts文件,添加 # GitHub地址 192.30.255.112 gith ...
- 【oracle】 months_between(date1,date2)
(20090228,20080228)====12 (20090228,20080229)====12 (20080229,20070228)====12 (20100331,20100228)=== ...
- RAID及磁盘配额
RAID的对比: 版本 特点 磁盘个数 可用空间 故障磁盘数 应用环境 RAID0 读写速度快,数据容易丢失 两个 全部 一块 测试,临时性 RAID1 读写速度慢,数据可靠 至少两个,可以2的倍数 ...
- [LeetCode] 658. Find K Closest Elements 寻找K个最近元素
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- [LeetCode] 76. Minimum Window Substring 最小窗口子串
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- git强制合并另一个分支
New分支和Old分支都修改了同样的部分,有冲突,但是想在Old分之上合并New分支的内容,并且以New分支为主,就是不自己手动解决冲突,碰到冲突,直接以New分支为主. 参考 https://git ...
- 推荐一款万年历App 诸葛万年历
推荐一款万年历App 诸葛万年历 1 介绍 应用简介: 提供标准和专业的时间信息查询,记录和承载生活中的美好记忆,帮助用户高效快捷的管理个人时间.精美的日期展示和完善的重要事件提醒功能,可以方便安排日 ...
- JS存取Cookies值
这里对cookie进行了说明,也介绍了几个方法,但是我要取我存的cookie时取不到,他的方法只是针对存的 名字-值,不涉及键,所以自己写了个方法,来满足我的需求. 封装了简单存取Cookie: / ...
- sqlyog管理关系型数据库mysql数据库之sqlyog的安装管理
.关系型数据库 有库有表,有关系 非关系型数据库 存储对象.集 下面的所有演示截图都是基不超过SQLyog 11进行的. 1. 2.点击上图中的应用程序,进行安装. 安装sqlyog , 账户dd0 ...