本篇主要写一些shell脚本免交互expect的使用。


概述

Expect是建立在tcl基础上的一个工具,Expect 是用来进行自动化控制和测试的工具。主要解决shell脚本中不可交互的问题。

安装

  • 使用此工具前需先安装
yum install -y expect

基本命令

send

  • 向进程发送字符串,用于模拟用户的输入

  • 该命令不能自动回车换行,一般要加\r (回车)

expect

  • expect的一个内部命令,判断上次输出结果里是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回。

  • 只能捕捉由spawn启动的进程的输出

spawn

  • 启动进程,并跟踪后续交互信息

interact

  • 执行完成后保持交互状态,把控制权交给控制台

timeout

  • 指定超时时间,过期则继续执行后续指令

  • 单位是:秒

  • timeout -1永不超时

  • 默认情况下,timeout10

exp_continue

  • 允许expect继续向下执行指令

send_user

  • 回显命令,相当于echo

$argv 参数数组

  • expect脚本可以接受从bash传递的参数.可以使用[lindex $argv n]获得,n0开始,分别表示第一个,第二个,第三个...参数

expect 脚本

  • expect脚本必须以interactexpect 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"}
}

只要配置aaabbbccc中的任何一个,执行相应的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的更多相关文章

  1. shell中expect免交互

    expect前言观察ssh登录的交互现象有些程序难以避免的需要交互.你该如何解决脚本与程序的交互问题?名词解释期待, 预期, 盼望, 预料,料想, 指望, 希望, 要求,想, 认为一.概述 我们通过S ...

  2. shell编程之免交互 (不要再让你的双手过度劳累了)

    shell编程之免交互 1.Here Document免交互 2.Expect免交互 3.免交互磁盘创建 1.Here Document免交互 概述: Here Document使用I/O重定向的方式 ...

  3. 8.shell编程之免交互

    shell编程之免交互 目录 shell编程之免交互 Here Document免交互 免交互定义 Here Document变量设定 多行的注释 expect expect 定义 expect基本命 ...

  4. shell编程之免交互

    目录: 一.Here Document 免交互 二.Expect 一.Here Document 免交互 使用I/O重定向的方式将命令列表提供给交互式程序或命令, 比如 ftp.cat 或 read ...

  5. shell编程-ssh免交互批量分发公钥脚本

    脚本基本原理 1.控制端免交互创建秘钥和公钥: 1 ssh-keygen -t rsa -f /root/.ssh/id_rsa -N "" 2.免交互发送公钥 1 sshpass ...

  6. linux免交互登陆远程主机并执行命令(密钥对和Expect)

    原文章摘自:http://lizhenliang.blog.51cto.com/7876557/1607723/ Linux下实现免交互登陆一般有两种: 1. SSH无密码认证方式 客户端使用ssh- ...

  7. centos shell编程4【分发系统】 服务器标准化 mkpasswd 生成密码的工具 expect讲解 expect传递参数 expect自动同步文件 expect指定host和要同步的文件 expect文件分发系统 expect自动发送密钥脚本 Linux脚本执行方式 第三十八节课

    centos shell编程4[分发系统] 服务器标准化  mkpasswd 生成密码的工具  expect讲解   expect传递参数   expect自动同步文件  expect指定host和要 ...

  8. expect实现免交互

    如果想写一个能够自动处理输入输出的脚本又不想面对C或Perl,那么expect是最好的选择.它可以用来做一些Linux下无法做到交互的一些命令操作. (1).安装和使用expect expect是不会 ...

  9. expect命令和here document免交互

    目录 一.Here Document免交互 1.1 概述 1.2 语法格式 1.3 简单案例 1.4 支持变量替换 1.5 多行注释 1.6 完成自动划分磁盘免交互 二.Expect进行免交互 2.1 ...

随机推荐

  1. 百度PaddlePaddle:

    百度正式发布PaddlePaddle深度强化学习框架PARL 近日,百度PaddlePaddle正式发布了深度强化学习框架 PARL,同时开源了基于该框架的.在 NeurIPS 2018 强化学习赛事 ...

  2. GIT : IDEA切换到某个tag

    背景看一本presto的书,发现版本用的是presto-0.107这个版本.然后我去Apache clone下源码,发现分支只有几个,但是下载页面却有很多不同的版本 然后看Tag发现有很多. 然后我现 ...

  3. 数据结构——链栈(link stack)

    /* linkStack.c */ /* 链栈 */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h ...

  4. CF908G New Year and Original Order(DP,数位 DP)

    又一次降智…… (数位 DP 原来可以写这么短,学到了) 问题可以转化为求数位中 $\ge k$ 的有恰好 $j$ 位的数的个数.设为 $c_{j,k}$. 那么答案就是:(考虑把 $k$ 的贡献拆开 ...

  5. [LeetCode] 207. Course Schedule 课程清单

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  6. intelliJ 社区版-找不到 plugins选项

    丢人了... 今天 在intelliJ社区版上面找不到 plugins 选项了, 其实是有的,我看的是项目的 settings 当然没有了, (1)如果直接点击File==> 这样就是没有plu ...

  7. TypeError: Dense_net() takes 0 positional arguments but 1 was given

    书写孪生网络的时候出现的错误,调用单通道时出现如下错误. 看了别人写的博客大概和类内的初始化还有self之类的有关系,没有弄清楚.将单通道的文件在函数外声明,在函数内统一调用可以解决这个问题

  8. SpringBoot2配置prometheus浏览器访问404

    背景:SpringBoot2的项目要配置 actuator + prometheus的健康检查,按照教程配置好之后再浏览器测试 http://localhost:port/prometheus 后40 ...

  9. 启动apache,将debug日志输出在console窗口

    sudo apachectl -k start -e DEBUG 点个广告吧亲:

  10. Qt Quick 常用元素:RadioButton(单选框),CheckBox(复选框) 与 GroupBox(分组框)

    先介绍一下 ExclusiveGroup. ExclusiveGroup (互斥分组)本身是不可见元素,用于将若干个可选择元素组合在一起, 供用户选择其中的一个选项.你可以在 ExclusiveGro ...