本篇主要写一些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. (translation.E004) You have provided a value for the LANGUAGE_CODE setting that is not in the LANGUAGES setting.

    django3.0开始LANGUAGE_CODE前面必须配相应的LANGUAGES配置如下: from django.utils.translation import gettext_lazy as ...

  2. 论文阅读笔记六十三:DeNet: Scalable Real-time Object Detection with Directed Sparse Sampling(CVPR2017)

    论文原址:https://arxiv.org/abs/1703.10295 github:https://github.com/lachlants/denet 摘要 本文重新定义了目标检测,将其定义为 ...

  3. 使用教育邮箱免费申请JetBrains套装(IntelliJ, PhpStorm, WebStorm...)

    想下个PhpStorm来写php,发现可以使用教育账号白嫖. 申请步骤 打开 申请页面 ,点击 “APPLY NOW” 开始申请. 填写姓名,以及学校提供给你的邮箱(edu后缀邮箱,或.end.cn) ...

  4. Apex API 请求

    Salesforce与网络服务的通信 在Salesforce中可以利用Apex类与远程站点的网络服务进行通信.当远程网络服务支持REST方法时,开发者可以利用Apex代码进行数据的操作. 设置远程站点 ...

  5. cd1101d 树形dp

    cd1101d 简单dp 链接 codeforces 思路 所有数的质因数存下来,最多6个. 然后\(f[i][j][0/1]\)表示i子树内链gcd为j的i是否为链头. 暴力转移就行了 代码 #in ...

  6. CF1149D Abandoning Roads(图论,最短路,状态压缩,最小生成树)

    题目大意:$n$ 个点,$m$ 条边的无向图,边权只有两种,小的为 $a$,大的为 $b$. 对于每个点 $p$,询问在这张图所有的最小生成树上,$1$ 到 $p$ 的最短距离的最小值. $2\le ...

  7. [比赛题解]CWOI2019-1

    [比赛题解]CWOI2019-1 比赛日期:2019.10.12 T1 一道神仙DP题. 我们考虑\(dp[i][j][k]\)表示最后\(i\)位数,\(i-1\)位都是9,最后一位为\(j\),最 ...

  8. ZROI1153 【线上训练3】数个数

    ZROI1153 [线上训练3]数个数 传送门 一道非常有意思的题,涵盖了各种知识点. 首先,很显然,这是个容斥.容斥可以过掉\(30pts\). 这里我们考虑容斥+DP. 我们令\(dp[i][j] ...

  9. js原型和原型链,以及__proto__、prototype属性

    __proto__和prototype属性: 1.__proto__属性: 在JS里,万物皆对象(函数是对象.原型也是对象...).对象都具有属性__proto__,这个属性会指向该对象的原型. 2. ...

  10. 使用nodemon提高nodejs调试效率

    1.安装 nodemon 直接用npm安装既可,键入命令: npm -g install nodemon .如果不行,检查电脑有没有联网,联网后输入 sudo npm -g install nodem ...