If you want to learn Linux, you may encounter the Linux Shell automatic interaction problem, here will introduce the Linux Shell automatic interaction solution, here to share with you.

The background,

Shell scripts can save a lot of time when dealing with automatic loops or large tasks. By creating a list of commands to process the task, and using variables, conditions, arithmetic, and loops to quickly create the script to do the job, it takes much less time and effort than typing in one command at a time from the command line.

However, sometimes we may need to realize the function of interacting with interactive programs such as FTP and Telnet server, etc. At this time, we need to use the automatic interaction function of shell. This paper collects three commonly used automatic interaction methods, and makes a comparison and summary.

Second, the demand for

Requirement 1: FTP from one Linux machine to another, do a series of operations and then close, too lazy to manually enter the password each time. Requirement 2: Change the password of the logged in user, too lazy to enter the old and new password every time. Requirement 3: You want su to automatically log in to the root account, and are too lazy to enter the root password every time.

Three, debugging environment

Terminal: SecureRT System: WinXP, CentOS 4.4(VMware) Shell: Bash

Type B shells (sh, bash, KSH) behave in similar ways. Class C shells (CSH, TCSH) have similar behavior, as well as ZSH and rc shells. The debugging environment of this article is bash.

Four, automatic interaction method I

The key to automatic interaction is the automatic input of interactive information, first associated with file redirection, in shell programming has such a use (see Linux and UNIX shell programming guide Chapt 5.7) : “Command << delimiter reads from standard input until it encounters the delimiter.”

The redirection operator command << delimiter is a very useful command where the shell takes as input everything from delimiter until the next delimiter of the same character. When the next delimiter is reached, the shell knows that the input is over. The most common Delimiter delimiter is EOF, although it can be customized for any other character.

For automatic logging in to FTP, as required by requirement 1, and performing a series of operations, this approach can be used for automatic interaction. The code is as follows:

#! /bin/bash ftp-i-n 192.168.167.187 << EOF user HZC 123456 Pwd CD test Pwd Bye EOF

Five, automatic interaction method two

Requirement 2 requires a non-interactive way to change the login user’s password. Try Method 1, but it cannot be realized. This time associated with the mutual information of the other automatic input method, pipe, through the echo + + | sleep can achieve this requirement.

#! /bin/bash (echo "curpassword" sleep 1 echo "newpassword" sleep 1 echo "newpassword")|passwd

If the test passes, run the script and change the current user’s curpassword to newpassword.

Six, automatic interaction method three

Standard in must be a tty = “root”/” root “/” root “/” root “/” root”

Try looking for outside help. A shell tool, Expect, does this. In fact, Expect is a tool specifically designed for automatic interaction.

#! /usr/bin/expect spawn su root expect "password: " send "123456\r" expect eof expect eof

When the test passes, run the script to log directly from the current user to root.

7. Method summary

Approach one (redirection) is simple and intuitive, and often has practical applications, but has limited functionality in the field of automated interaction. Method two (pipes) is also very simple and intuitive, sometimes even without sleep to show the power of automatic interaction, but sometimes it’s just not there. Approach three (Expect) is the most powerful in terms of functionality. Expect is built for automatic interaction, but the drawback is that it requires an Expect package, which is difficult to install in an embedded environment, for example. Each of the three methods has its own advantages and disadvantages. If the application is good, all of them can complete the automatic interaction of Linux Shell.

Scan the QR code below to explore the world of Linux with me