本篇主要写一些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. Java System.getProperty vs System.getenv

    转自:https://www.baeldung.com/java-system-get-property-vs-system-getenv 1. Introduction The package ja ...

  2. ajax与重定向

    网上有不少说法ajax的请求url浏览器不会重定向的说法是片面的,正常是这样的: 当服务器将302响应发给浏览器时,浏览器并不是直接进行ajax回调处理,而是先执行302重定向——从Response ...

  3. 详解 ASP.NET Core MVC 的设计模式

    MVC 是什么?它是如何工作的?我们来解剖它 在本节课中我们要讨论的内容: 什么是 MVC? 它是如何工作的? 什么是 MVC MVC 由三个基本部分组成 - 模型(Model),视图(View)和控 ...

  4. [LeetCode] 924. Minimize Malware Spread 最大程度上减少恶意软件的传播

    In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j ...

  5. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  6. TypeWriting

    头文件getputch.h /* * getputch.c */ /* 用于getch/putch的通用头文件"getputch.h" */ #ifndef __GETPUTCH ...

  7. logback&log4j异步日志配置

    logback 原始配置 配置 appender, 控制文件的滚动方式,日志的输出格式. <appender name="method-time-appender" clas ...

  8. BizTalk证书相关操作

    OPEN SSL 神技能 从PFX文件中导出私钥 openssl pkcs12 -in Cert.pfx -nocerts -nodes -out private_pc.key 从PFX文件中导出CS ...

  9. 三大类sql语句——该记录是本人以前微博上的文章

    一.DML语句二.DDL语句三.事务控制语句一.DML语句-Data Mulipulation LanguageDML语句数据操作野菊执行后会生成一个事务,事务需要提交才能够永久生效,在commit前 ...

  10. mgcp的alg功能实现

    刚吃了一碗还算正宗的潮汕牛筋丸粿条和一颗卤蛋,算是给自己的生日礼物. 这一周工作只围绕了一个主题“mgcp的alg功能实现”. 1. 应用场景: 一台运行mgcp语音协议的终端设备,经过一台路由器到达 ...