Home>Article>Operation and Maintenance> What is the usage of expect in linux

What is the usage of expect in linux

WBOY
WBOY Original
2021-12-31 17:01:36 6474browse

In Linux, expect is an automated interactive suite. It is mainly used when executing commands and programs. The system requires the input of a specified string in an interactive form to achieve interactive communication. The execution syntax is "yum install -y expect" ; The prerequisite for this script to be executed is that expect needs to be installed.

What is the usage of expect in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is the usage of expect in linux

expect is an automated interactive suite, which is mainly used to interact with the system when executing commands and programs. The form requires the input of a specified string to achieve interactive communication.

expect automatic interaction process:

spawn starts the specified process---expect to obtain the specified keyword---send sends the specified character to the specified program---exit after completion.

Note that the script can be executed only if expect is installed

yum install -y expect

Expect common command summary:

  • spawn The interactive program starts followed by the command or specified program

  • # Expect Get the matching information successfully, execute the program action after Expect

  • ## Send exp_send used to send the specified string information

  • exp_continue You need to use

  • send_user for multiple matches in expect. It is used to print the output equivalent to echo in the shell

  • exit Exit the expect script

  • eof Expect execution ends and exit

  • set Define variables

  • puts Output variable

  • set timeout Set timeout

Example:

1.Ssh log in to the remote host to execute the command, execute Method expect 1.sh or ./1.sh

# vim 1.sh #!/usr/bin/expect spawn ssh saneri@192.168.56.103 df -Th expect "*password" send "123456\n" expect eof

2. Log in to the host remotely via ssh to execute the command, execute the expect command in the shell script, and execute the method sh 2.sh, bash 2.sh or ./2 .sh can be executed.

#!/bin/bash passwd='123456' /usr/bin/expect <<-EOF set time 30 spawn ssh saneri@192.168.56.103 df -Th expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "$passwd\r" } } expect eof EOF

Related recommendations: "

Linux Video Tutorial"

The above is the detailed content of What is the usage of expect in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn