当前位置:网站首页>10. Expect interaction free
10. Expect interaction free
2022-07-25 10:29:00 【Is a stupid child】
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] ky20]# rpm -q expect
[[email protected] ky20]# rpm -q tcl
[[email protected] ky20]# 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] ky20]# vim ssh.sh
#!/usr/bin/expect // You need to use expect Own interpreter , Be careful not to write bash Otherwise, I can't recognize
spawn ssh [email protected] // Open a program , This program is ssh Remote login
expect {
// Capture content , When there is a password When , Will send the password to the program , The default is no line break , So we need to \r Carriage returns , Multiple conditions need to be enclosed in curly braces , Pay attention to the format !
"password"
{
send "123\r"; }
}
interact
#!/usr/bin/expect
spawn ssh [email protected]
expect {
"(yes/no*)"
{
send "yes\r"; exp_continue; }
"password:"
{
send "100700\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] ky20]# ./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 .
{send "123456\r";}
}
EOF
The verification results :
[[email protected] ky20]# ./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 .
边栏推荐
猜你喜欢

Open virtual private line network load balancing

Fastdfs offline deployment (Graphic)

关于slf4j log4j log4j2的jar包配合使用的那些事

11.iptables 防火墙

Number theory -- Research on divisor

2.介绍部署LAMP平台+DISCUZ论坛

Angr (II) -- angr_ ctf

Angr(五)——angr_ctf

mongoDB的使用

Supervisor deployment (offline deployment requires downloading the deployment package in advance)
随机推荐
测试计划、测试方案
Vscode latex workshop set xelatex compilation
Angr (IX) -- angr_ ctf
2021 京东笔试总结
PyTorch 代码模板 (CNN)
Yiwen society, three necessary packet capturing tools for hackers
Dynamic planning, shopping list problem
Angr (V) - angr_ ctf
Attention is all you need 论文精读笔记 Transformer
Salt FAQs
Supervisor deployment (offline deployment requires downloading the deployment package in advance)
存储、计算、分布式虚拟化篇(收集整理适合小白)
Mysql5.7 master-slave database deployment (offline deployment)
Set creation and common methods
Swing的组件图标
Angr (VII) -- angr_ ctf
【无标题】
鼠标监听,画笔
Bug elements
Exception handling exception