当前位置:网站首页>shell programming without interaction
shell programming without interaction
2022-08-10 22:00:00 【Guannan cattle x people】
目录
在写入文件时要先将变量替换成实际值,再结合cat命令完成写入1
引言:
Interaction-free is an alternative to standard input,Using interaction-free allows us to construct input information more conveniently at work.
Here Document概述
1、使用I/o重定向的方式将命令列表提供给交互式程序或命令,比如ftp、cat或read命令.
2、 HiereDocument是标准输入的一种替代品,可以帮助脚本开发人员不必使用临时文件来构建输入信息,Instead, a file is produced directly in-place and used as standard input for backup commands.
语法格式:
命令 <<标记
...
内容
...
标记
Here Document使用注意事项
标记可以使用任意合法字符(通常为EOF)
结尾的标记一定要顶格写,前面不能有任何字符
结尾的标记后面也不能有任何字符(包括空格)
开头标记前后的空格会被省略掉
Interaction-free case:
创建yum源
Interaction-free implementation of row count statistics
通过read命令接受输入并打印
通过passwd给用户设置密码
变量设定(支持变量替换)
在写入文件时要先将变量替换成实际值,再结合cat命令完成写入1
案例:在写入文件时要先将变量替换成实际值,在结合cat命令完成写入
整体赋值给一个变量,然后通过echo命令将变量值打印出来
格式控制
关闭变量替换的功能
去掉每行之前的TAB字符
多行注释
Bash 的默认注释是"#",该注释方法只支持单行注释,在shell脚本的工作中,"#"右侧的任何字符串,bash都会将其忽略.Here Document的引入解决了多行注释的问题.
:"代表什么都不做的空命令.中间标记区域的内容不会被执行,会被bash 忽略掉,因此可达到批量注释的效果.
expect基本命令
expect概述
建立在 tcl之上的一个工具
用于进行自动化控制和测试
解决shellInteraction related in the script 问题
expect的安装
expect它是一个程序,所以它也是需要提前安装才可以使用的.
rpm -q expect
rpm -q tcl
yum -y install expect //yum或者光盘安装都可
脚本解释器
expect脚本中首先引入文件,表明使用的是哪一个shell
#!/usr/bin/expect
spawn
spawn后面通常跟一个Linux执行命令,表示开启一个会话、启动进程,并跟踪后续交互信息.
例: spawn passwd root
expect
判断上次输出结果中是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回;只能捕捉由spawm启动的进程的输出;用于接收命令执行后的输出,然后和期望的字符串匹配
send
向进程发送字符串,用于模拟用户的输入;该命令不能自动回车换行,一般要加\r (回车)或者\n
例:
方法一:
expect "密码"{ send "abc321\r"} 同一行send部分要有{}
方法二:
expect""密码"
send "abc321\r" 换行send部分不需要有{}
方法三:
expect支持多个分支
expect 只要匹配了其中一个情况,执行相应的send语句后退出该expect语句
"密码1"{send "abc321\r"}
"密码2"{send "123456\r"}
"密码3"{send "123123\r"}
结束符expect eof
expect eof:表示交互结束,等待执行结束,退回到原用户,与spawn对应.
比如切换到root用户,expect脚本默认的是等待10s,当执行完命令后,默认停留10s后,自动切回了原用户
interact:执行完成后保持交互状态,把控制权交给控制台,会停留在目标终端而不会退回到原终端,这个时候就可以手工操作了,interact后的命令不起作用,比如interact后添加exit,并不会退出root用户.而如果没有interact则登录完成后会退出,而不是留在远程终端上
使用interact会保持在终端而不会退回到原终端,比如切换到root用户,会一直在root用户状态下;比如ssh到另一服务器,会一直在目标服务器终端,而不会切回的原服务器
注意:expect eof与interact只能二选一
set
expect默认的超时时间是10秒,通过 set命令可以设置会话超时时间,若不限制超时时间则应设置为-1.例:set timeout 30
exp_continue
exp_continue附加于某个expect判断项之后,可以使该项被匹配后,还能继续匹配该expect-判断语句内的其他项.exp_continue类似于控制语句中的continue 语句.表示允许expect继续向下执行指令.
例如:下例将判断交互输出中是否存在yes/no 或*password.如果匹配yes/no则输出 yes并再次执行判断;如果匹配*password则输出abc123并结束该段expect语句.
expect {
" (yes/no) "{ send "yes\r" ; exp_continue;}
"*password"{set timeout 300;send "abc321\r";}
注:使用exp_ continue时,如果跟踪像passwd 这样的输入密码后就结束进程的命令,expect{}外不要 再加上expect eof因为spawn进程结束后会默认向expect发送eof,,会导致后面的expect eof 执行报错
send_user
send_user表示回显命令,相当于echo
接收参数
expect脚本可以接受从bash命令行传递的参数,使用[lindex $argvn]获得.其中n从o开始,分别表示第一个,第二个,第三个..参数.
set hostname [lindex $argv 0] 相当于hostname=$1
set password [lindex $argv 1] 相当于passswd=$2
set hostname [lindex $argv 0] 相当于hostname=$1
set password [lindex $argv 1] 相当于passswd=$2
1./usr/bin/expect //脚本解释器,脚本中首先引入文件,表明使用的是哪一个shell
#!/usr/bin/expect
2.spawn //Usually followed by oneLinux执行命令,表示开启一个会话、启动进程,并跟踪后续交互信息
span passwd root
3.expect //Determines whether the last output result contains the specified string,如果有则立即返回,Otherwise, wait for the timeout and return
//只能捕捉有spawn启动的进程输出;用于接收命令执行后的输出,Then match the expected character
4.send //向进程发送字符串,用于模拟用户的输入;该命令不能自动回车换行,一般要加\r回车,或\n换行
5.expect eof //表示交互结束,等待执行结束,退回到原用户,与spawn对立
//比如sshConnect to other terminals,时间到后(默认10s)will automatically log out to the current end user
interact //执行完后保持交互状态,Manual operation is possible,interact后的命令不起作用
//expect eof与interact只能二选一
6.set //expect默认超时时间为10s,可以通过setThe command sets the session timeout
7.exp_continue //附加于某个expect判断后,After making the option match,Proceed to the next judgment
//exp_continue类似于控制语句中的continue
8.send user //表示回显命令,类似于echo
9.[lindex $argv n] //接受从bashParameters passed on the command line,n从0开始,分别表示第一个、The second and so on parameter
比如:set hostname [lindex $argv 0] 类似于bahs中的hostname=$1
expectWrite a script case
ssh无交互登录到远程服务器
#!/usr/bin/expect
spawn ssh [email protected]
expect {
"(yes/no)"
{send "yes\r";exp_continue}
#Capture if there ispasswd字符串
"password"
#enter123456,\r为回车
{send "123456\r"}
}
expect "#" #当捕获到#的时候
send "ls\r" #执行ls命令
send "ifconfig ens33\r" #执行ifconfig ens33命令
send "exit\r" #执行完exit退出登录
#remain in the terminal
interact
定义变量与引用–set
使用set定义变量,变量名和变量的值中间用空格分开,其他用法与shell脚本一致
定义变量
#!/usr/bin/expect
set user root
set ip 192.168.58.19
set passwd 123456
引用变量
#!/usr/bin/expect
#定义变量user为root
set user root
#设置变量ipis the first position variable
set ip [lindex $argv 0]
#设置变量passwdfor the second variable position
set passwd [lindex $argv 1]
spawn ssh [email protected]$ip
expect {
"(yes/no)"
{send "yes\r";exp_continue}
"password"
{send "$passwd\r"}
}
expect "#"
send "ls\r"
send "exit\r"
expect eof
~
Append it when starting the scriptip 加密码123456
超时设置
#!/usr/bin/expect
#定义变量user为root
set user root
#设置变量ipis the first position variable
set ip [lindex $argv 0]
#设置变量passwdfor the second position variable
set passwd [lindex $argv 1]
#定义超时时间20s
set timout 20
#日志文件
log_file test.log #定义日志文件名
#1output to the screen,0为不输出
log_user 1
spawn ssh [email protected]$ip
expect {
"(yes/no)"
{send "yes\r";exp_continue}
"password"
{send "$passwd\r"}
}
expect "#"
send "ls\r"
expect eof
Startup script plusip 加密码123456
shell脚本调用expect
将expectprocess intoshell当中,方便执行和处理在shell脚本中调用expect
修改用户密码
#!/bin/bash
username=$1
/usr/bin/expect <<EOF
spawn passwd $username
expect {
"新的 *"
{send "1qwe23456789\r";exp_continue}
"重新输入 *"
{send "1qwe23456789\r";}
}
EOF
脚本调用
使用expect -c " "调用
[[email protected] ~]# vim 7.sh
#!/bin/bash
expect -c "
spawn ssh [email protected]
expect {
\"(yes/no)\"
{send \"yes\r\";exp_continue}
#Capture if there ispasswd字符串
\"password\"
#enter123456,\r为回车
{send \"123456\r\"}
}
#Capture if there is#号
expect \"#\"
send \"ls\r\"
expect eof
"
总结
This article mainly describes the useHere Documentwhen the tag is custom,Can be any valid character,When writing interactive programs alone,解释器可以使用/usr/bin/expect在/bin/bash解释器中,调用expect程序,需要使用Here Document,或者expect -c “”,使用expect -c “” 时,All double quotes within double quotes need to be added\转义符,因为shellIdentify the first two double quotes as a pair.
边栏推荐
- Before implementing MES management system, these three questions to consider
- 《mysql 从入门到内卷再到入土》——Mysql基础 学习笔记目录
- HighTec快捷键(Keys)设置位置
- Intelligent scheme design - intelligent rope skipping scheme
- web逆向之丁香园
- 自建函数 测试例和语法——《mysql 从入门到内卷再到入土》
- In 2021 China industrial Internet security competition (competition) in fujian province and the first industry of fujian province Internet innovation competition
- 基于Pix4Dmapper的运动结构恢复法无人机影像三维模型重建
- 【PCBA scheme design】Bluetooth skipping scheme
- 异常的了解
猜你喜欢
为什么一般公司面试结束后会说「回去等消息」,而不是直接告诉面试者结果?
Use Cloudreve to build a private cloud disk
【nvm】【node多版本管理工具】使用说明和踩坑(exit status 1)
快消品行业经销商协同系统:实现经销商可视化管理,提高沟通执行效率
华为路由器旁挂引流实验(使用流策略)
LeetCode-498 - Diagonal Traversal
着力提升制造业核心竞争力,仪器仪表产业迎高质量发展
Future与CompletableFuture
Redis Performance Impact - Asynchronous Mechanisms and Response Latency
化学制品制造业数智化供应链管理系统:建立端到端供应链采购一体化平台
随机推荐
内置模板市场,DataEase开源数据可视化分析平台v1.13.0发布
ENVI感兴趣区ROI文件由XML格式转为ROI格式
How to secure users in LDAP directory service?
JS中的filter、map、reduce
优化是一种习惯●出发点是'站在靠近临界'的地方
【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
B. Codeforces Subsequences
Detailed explanation of the use of Oracle's windowing function (2)
【SQL刷题】Day3----SQL必会的常用函数专项练习
石油化工行业商业供应链管理系统:标准化供应商管理,优化企业供应链采购流程
基于Pix4Dmapper的空间三维模型重建应用——空间分析选址
Kerberos认证
APP UI自动化测试常见面试题,或许有用呢~
MATLAB神经网络拟合工具箱Neural Net Fitting使用方法
2022.8.9 Mock Competition
ArcGIS自动随机生成采样点的方法
查询:复杂查询的语法和使用例——《mysql 从入门到内卷再到入土》
SELECT:简单的查询操作语法&使用例——《mysql 从入门到内卷再到入土》
Huawei router clock near the drainage experiment (using stream strategy)
着力提升制造业核心竞争力,仪器仪表产业迎高质量发展