当前位置:网站首页>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
边栏推荐
- [hdu6868] absolute math (pusher + Mobius inversion)
- 杰理之通常程序异常情况有哪些?【篇】
- Longest common front string
- Odoo 服务器搭建备忘
- Number theory blocking (integer division blocking)
- Sim Api User Guide(7)
- Function realization of printing page
- [untitled]
- Prefix sum of integral function -- Du Jiao sieve
- GCD of p2257 YY (Mobius inversion)
猜你喜欢
0704、ansible----01
Sim Api User Guide(6)
Random neurons and random depth of dropout Technology
从知识传播的维度对比分析元宇宙
Yarn核心参数配置
Easy to understand subset DP
一文看懂 LSTM(Long Short-Term Memory)
The central control learning infrared remote control module supports network and serial port control
自定义登录失败处理
Example of data object mask used by SAP translate
随机推荐
Go language practice mode - functional options pattern
The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to
1D / 1D dynamic programming learning summary
【无标题】
[hdu6868] absolute math (pusher + Mobius inversion)
failureForwardUrl与failureUrl
DBA常用SQL语句(1)— 概况信息
Planning and construction of industrial meta universe platform
Realize data value through streaming data integration (1)
NEC infrared remote control coding description
Computer network security experiment II DNS protocol vulnerability utilization experiment
Example of data object mask used by SAP translate
Juc并发编程09——Condition实现源码分析
自定义登录失败处理
Odoo 服务器搭建备忘
art-template 模板引擎
Sim Api User Guide(6)
使用IDEA开发Spark程序
通过流式数据集成实现数据价值(5)- 流处理
Longest common front string