当前位置:网站首页>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 .
边栏推荐
- Trojaning Attack on Neural Networks 论文阅读笔记
- When installing mysql, string the service installation failure > mysql80 startup failure
- Basic knapsack problem
- Array static initialization, traversal, maximum value
- Bug elements
- Output stream in io stream
- 7.shell实用的小工具cut等
- JS encryption parameter positioning
- 5.NFS共享服务和ssh远程控制服务
- Pytorch calculates the loss for each sample in the batch
猜你喜欢

Angr (V) - angr_ ctf

搭建LNMP+DISCUZ论坛

Duplicate SSL_ Anti spoofing, spoofing attacks and deep forgery detection using wav2vec 2.0 and data enhanced automatic speaker authentication

5.NFS共享服务和ssh远程控制服务

VSCode Latex Workshop 设置 XeLatex 编译

MySQL offline deployment

Erlang (offline deployment)

Pow(x,n)

1. Shell programming specifications and variables

Supervisor deployment (offline deployment requires downloading the deployment package in advance)
随机推荐
Pytoch separates tensor by the value of one dimension of tensor (simple)
【无标题】
Use of mongodb
Angr(五)——angr_ctf
Angr(十)——官方文档(Part1)
3.跟你思想一样DNS域名解析服务!!!
Angr (VIII) -- angr_ ctf
Erlang(离线部署)
9.shell文本处理三剑客之awk
2.介绍部署LAMP平台+DISCUZ论坛
安装mysql时,string the service 安装失败>mysql80启动失败
Oh my Zsh and TMUX configuration (personal)
Angr(六)——angr_ctf
Swing component
Angr(二)——angr_ctf
Angr (VI) -- angr_ ctf
1、 The difference between unittest framework and pytest framework
二、unittest框架主要做什么
异常处理Exception
6.shell之正则表达式