本篇主要写一些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. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  2. 【day02】PHP

    一.数据类型(8个主要数据类型和4个伪类型)  1.8个主要数据类型     (1)标量类型(存储单一值)        a.整型(Integer Int)        b.浮点型(Float Do ...

  3. 显示隐藏文件.reg

    显示隐藏文件.reg Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\Curren ...

  4. [LeetCode] 277. Find the Celebrity 寻找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  5. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. [LeetCode] 18. 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  7. 公司ES升级带来的坑怎么填?

    前言 公司的ES最近需要全部进行升级,目的是方便维护和统一管理.以前的版本不统一,这次准备统一升级到一个固定的版本. 同时还会给ES加上权限控制,虽然都是部署在内网,为了防止误操作,加上权限还是有必要 ...

  8. 【Struts】Struts框架配置详解

    1.首先将所必须的Jar包放到项目的WebRoot/WEB-INF/lib目录下. 如果你没有这些Jar文件,你可以到Struts官网上下载:http://struts.apache.org/.因为经 ...

  9. java json解析(转)

    转自:https://www.cnblogs.com/sunnywindycloudy/p/8343013.html 给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分情况下都是XML格式 ...

  10. 路径规划基础A*算法

    1,Dijkstra’s  算法 一种发散性寻找最短路径算法. 由起点开始向四周开始发散,直到碰到目标点为止.这时就是最短路径.优点:能找到与目标点的最短路径:缺点:搜索花费的时间会比较长. 2,Gr ...