当前位置:网站首页>Expect interaction free
Expect interaction free
2022-07-19 02:45:00 【Nothing in the world is difficult 754】
List of articles
One ,expect
1. expect summary
Based on the tcl A tool based on language , It is often used for automatic control and testing , solve shell Interaction related issues in scripts
The main role :
1. Track instructions that require interaction
2. Capture keywords that prompt interaction
3. Incoming interactive instruction , Enter to execute
2. expect Basic installation of
- expect It's a program , So it also needs to be installed in advance before it can be used
[[email protected]]# rpm -q expect
[[email protected]]# rpm -q tcl
[[email protected]]# yum install expect -y // install expect When the tcl Install as a dependent package
3. Basic commands
3.1 Script interpreter
expect The script first introduces the file , Indicate which one is used shell
#!/usr/bin/expect
3.2 spawn
spawn Usually followed by a Linux Carry out orders , Open a conversation 、 Start the process , And track subsequent interaction information .
for example :
spawn passwd root
3.3 expect
- Judge whether the last output result contains the specified string , If so, return immediately , Otherwise, it will wait for the timeout to return
- Can only be captured by spawn The output of the started process
- Used to receive output after command execution , And then match the expected string
3.4 send
- Send string to process , Used to simulate user input ; This command does not automatically enter to wrap lines , Generally, we need to add \r ( enter ) perhaps \n
Mode one :
expect " password "{
send "abc123\r" } // The same line send There should be {
}
Mode two :
expect " password "
send "abc123\r" // Line break send Part of it doesn't need to have {
}
Mode three :
expect Supports multiple branches
expect // Just match one of the cases , Execute corresponding send Statement to exit the expect sentence
" password 1"{
send "abc123\r" }
" password 2"{
send "123456\r" }
" password 3"{
send "123123\r" }
3.5 Terminator
- expect eof
Indicates the end of the interaction , Waiting for execution to end , Return to the original user , And spawn Corresponding .
For example, switch to root user ,expect The script default is to wait 10s, When the command is finished , Default stay 10s after , Automatically switched back to the original user
- interact
Keep interactive after execution , Give control to the console , Will stay at the target terminal without returning to the original terminal , At this time, it can be operated by hand ,interact The last order doesn't work , such as interact Add exit, It's not going to quit root user . And if not interact Will exit after login , Instead of staying on the remote terminal .
- Be careful :expect eof And interact You can only choose one
3.6 set
expect The default timeout is 10 second , adopt set Command to set the session timeout , If the timeout is not limited, it should be set to -1.
example :
set timeout 30
3.7 exp_continue
Attached to a expect After the judgment , After the item is matched , Can continue to match the expect- Determine other items in the statement .exp_continue Similar to... In a control statement continue sentence . It means to allow expect Continue down the instruction
for example : The following example will determine if there is yes/no or *password. If the match yes/no The output yes And execute judgment again ;
If the match *password The output abc123 And end the paragraph expect sentence .
expect{
"(yes/no)"
{
send "yes\r";exp_continue;}
"*password"
{
set timeout 300;send "abc123\r";}
}
# Be careful : Use exp continue when , If tracking is like passwd This is the command to end the process after entering the password ,expect{} Don't add expect eof
# because spawn At the end of the process, it will default to expect send out eof, It will lead to the following expect eof Error report in execution
# notes : Indicates that users are allowed to interact , Always keep the call back connection
3.8 send user
- send user Represents the echo command , amount to echo
3.9 Receiving parameters
expect Scripts can accept from bash Parameters passed from the command line , Use [lindex $argvn] get .
among n from o Start , They are the first , the second , Third .. Parameters . example :
set hostname [ lindex $argy 0 ]
amount to hostname=$1
set password [ lindex $argy 1 ]
amount to password=$2
4. use expect Write a script
example :ssh No interactive login to the remote server
[[email protected] ~]# cat ss.sh
#!/usr/bin/expect
spawn ssh [email protected]
expect {
"(yes/no)*"
{
send "yes\r"; exp_continue; }
"password:"
{
send "123456\r"; }
}
interact
4.1 stay shell Call in script expect
#!/bin/bash
username=$1
useradd $username
/usr/bin/expect <<-EOF spawn passwd $username expect { " The new code " // Be careful : The obtained content and the sent content cannot be on the same line, otherwise the execution will not succeed {send "123456\r";exp_continue} " Retype the new password " {send "123456\r";} } EOF
The verification results :
[[email protected]]# ./add.sh wangwu
spawn passwd wangwu
Change user wangwu Password .
new password :
Invalid password : The password is less than 8 Characters
Reenter the new password :
passwd: All authentication tokens have been successfully updated .
3456\r";}
}
EOF
The verification results :
[[email protected]]# ./add.sh wangwu
spawn passwd wangwu
Change user wangwu Password .
new password :
Invalid password : The password is less than 8 Characters
Reenter the new password :
passwd: All authentication tokens have been successfully updated .
边栏推荐
- 摇摆摇摆~防火墙
- Uniapp wechat applet login (authorize wechat first and then mobile phone number) - (1)
- VLAN和TRUNK口配置
- [solved] after referring to the local MySQL and forgetting the password, [server] --initialize specified but the data directory has files in it Aborti
- How to do a good job of test case review
- Reprint: SQL injection common bypass
- How to add software shortcuts to the right mouse button list
- CTFHub----RCE
- squid代理服务部署
- BeanShell script gets the current time
猜你喜欢

WINRAR命令拷贝指定文件夹为压缩文件,调用计划任务进行备份。

PowerStor500T报错0x01806803

Interpretation of concurrent virtual users, RPS and TPS

The JMeter BeanShell implementation writes the parameterized data generated by the request to the file

JMeter response time test component & multi interface concurrency

CTFHub----RCE

Detailed explanation of caduceus project of metauniverse public chain (I): project concept and technical framework of caduceus metaverse protocol

Full link voltage measurement

使用Grafana8.5.2显示zabbix6.0的信息

Bladex - a well-designed microservice architecture
随机推荐
Leetcode 1: Two Sum
Performance bottleneck positioning XMIND
Shell脚本for、while循环语句、猜价格小游戏
shell脚本之循环语句与函数
[solution] the local Group Policy Editor (gpedit.msc) in Win 11 cannot be opened
Leetcode 198:House Robber
Full link voltage measurement
MySQL备份和恢复
After unity imports the FBX model, the rotation and position of the object will change automatically at runtime
Sigaga
Subnet division (see details)
性能测试实施规范指南
FTP service
VLAN和TRUNK口配置
高质量的子程序
No, no, No. yesterday, someone really didn't write binary enumeration
Shell脚本整数值比较、逻辑测试、if语句、提取性能监控指标
Longest ascending subsequence - Optimization
If a hunter shoots a rabbit with a gun
expect免交互