当前位置:网站首页>shell脚本免交互
shell脚本免交互
2022-04-23 09:57:00 【qq_52825616】
目录
一、Here Document免交互
1、命令格式
使用l/o重定向方式将命令列表提供给交互式程序
标准输入的一个代替品
命令格式
命令<<标记
内容
标记
2、Here Document的注意事项
(1)标记可以使用任意合法字符
(2)结尾的标记一定要顶格写,前面不能有任何字符。
(3)结尾的标记后面也不能有任何字符(包括空格)
(4)开头标记前后的空格会被省略掉
3、实例
(1)免交互实现对行数的统计行数
[root@localhost ~]# wc -l <<EOF
> 1
> 456
> 7810
> EOF
3

(2) 通过read命令接收命令
[root@localhost ~]# read b <<EOF
> like
> EOF
[root@localhost ~]# echo $b
like

(3)免交互方式修改密码
[root@localhost ~]# passwd zs <<EOF
> 123123
> 123123
> EOF
更改用户 zs 的密码 。
新的 密码:无效的密码: 密码少于 8 个字符
重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。

(4) 在写入文件时会先将变量替换成实际值,再结合cat命令完成输出
#!/bin/bash
file="2.txt"
i=tea
cat> $file <<EOF
l like $i
EOF
[root@localhost ~]# cat 2.txt
l like tea

(5) 关闭变量替换的功能,按照字符原本的样子输出,不做任何修改或替换
#!/bin/bash
var="thress year"
myvar=$(cat <<'EOF'
today is friday
qqche
$var
EOF
)
echo $myvar

二、Expect
1、基本命令
建立在tcl语言基础上的一个工具,常被用于自动化控制和测试,解决shell脚本中交换相关的问题
安装软件包
[root@localhost ~]# yum install expect -y
(1)脚本解释器
expect脚本种首先引入文件,表明使用的是哪一个shell
[root@localhost ~]# which expect
/usr/bin/expect
(2)spawn
spawn后面通常跟一个Linux执行命令,表示开启一个会话、启动进程,并跟踪后续交互信息。例:spawn passwd root
(3)expect
判断上次输出结果中是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回
只能捕捉由spawn启动的进程的输出
用于接收命令执行后的输出,然后和期望的字符串匹配
(4)send
向进程发送字符串,用户模拟用户的输入;该命令不能自动回车换行,一般要加\r(回车)或者\n
(5)interact
执行完成后保持交互状态,把控制权交给控制台
(6)set
设置超时时间,过期则继续执行后续指令
单位是秒
timeout -l表示永不超时
默认情况下,timeout是10秒
(7)exp_continue
允许expect继续向下执行指令
解析:如果有一条语句错误,不加exp_continue,就会到此为止,退出
加上exp_continue会继续执行后续语句,不退出。
(8)send_user
回显命令,相当于echo
(9)接收参数
Expect脚本可以接受从bash传递的参数
可以使用[lindex $argv n]获得
n从0开始,分别表示第一个,第二个,第三个…参数
/bin/bash的位置变量是从$1开始到$9结束
Expect[lindex $argv 0]相当于/bin/bash的$1
2、实例
设置ssh登录免交互.
#!/usr/bin/expect
# ssh免交互
set timeout 60 设置超时时间60秒
set hostname [lindex $argv 0] 设置变量名,变量值来自位置变量0
set password [lindex $argv 1]
log_file a.log 设置日志文件记录保存的文件夹
log_user 1 记录屏幕输出
spawn ssh root@$hostname 发起启动进程ssh某主机名
expect {
"(yes/no)" {send "yes\r;exp_continue"}
"password" {send "$password\r"}
}
interact

版权声明
本文为[qq_52825616]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_52825616/article/details/124346817
边栏推荐
- 2022年流动式起重机司机考试题库模拟考试平台操作
- 一文读懂PlatoFarm新经济模型以及生态进展
- 论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
- 杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
- SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
- 【无标题】
- Function realization of printing page
- 通过流式数据集成实现数据价值(1)
- A concise course of fast Fourier transform FFT
- ansible playbook语法和格式 自动化云计算
猜你喜欢

SAP salv14 background output salv data can directly save files and send emails (with sorting, hyperlink and filtering format)

Number theory blocking (integer division blocking)

工业元宇宙平台规划与建设
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

SAP pi / PO soap2proxy consumption external WS example

Yarn核心参数配置

Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie

Nvidia最新三维重建技术Instant-ngp初探

Nine abilities of agile manufacturing in the era of meta universe

Sim Api User Guide(4)
随机推荐
MapReduce计算流程详解
使用IDEA开发Spark程序
Introduction to graph theory -- drawing
SAP pi / PO function operation status monitoring and inspection
ABAP implementation publishes restful services for external invocation example
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
0704、ansible----01
Golang force buckle leetcode 396 Rotation function
Sim Api User Guide(8)
SAP pi / PO soap2proxy consumption external WS example
杰理之栈溢出 stackoverflow 怎么办?【篇】
F-niu Mei's apple tree (diameter combined)
[untitled]
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
Realizing data value through streaming data integration (5) - stream processing
(Extended) bsgs and higher order congruence equation
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
Epidemic prevention registration applet
Juc并发编程09——Condition实现源码分析
《Redis设计与实现》