Do you know about Linux? Are you an application of Linux? If you want to learn about linux, you may encounter the problem of automatic Linux Shell interaction. Here we will introduce how to solve the problem of automatic Linux Shell interaction. Here we will share it with you.
Shell scripts can save a lot of time in processing automatic or large tasks. By creating a command list for processing tasks, using variables, conditions, arithmetic, and loops to quickly create scripts to complete the corresponding work is much more time-and effort-saving than typing the next command in the command line.
However, sometimes we may need to implement interaction functions with interactive programs such as ftp and telnet servers. At this time, we need to use the automatic shell interaction function, this article collects three commonly used Automatic Interaction methods and compares and summarizes them.
Log on from one Linux machine ftp to another Linux machine and disable it after a series of operations. You are too reluctant to manually enter the password every time.
Change the password of the logon user, so you do not have to enter the new and old passwords every time.
If you want su to log on to the root account automatically, you are too reluctant to enter the root password every time.
Iii. debugging environment
System: WinXP, CentOS 4.4 (VmWare)
Note: There are many shell types, such as SHELLsh, bash, and ksh of Class B, similar behavior, similar behavior between Class C SHELLcsh and tcsh, and shell such as zsh and rc, the debugging environment in this article is bash.
Iv. Automatic Interaction method 1
The key to automatic interaction is the automatic input of interaction information. First, we think of file redirection. In shell programming, there is such a usage (refer to the Linux and unix shell programming guide chapt 5.7 ): "command <delimiter reads data from the standard input until the delimiter is encountered. "
The redirection operator command <delimiter is a very useful command. shell uses all the content after the delimiter until the next same delimiter as input, and encounters the next delimiter, shell knows that the input is over. The most common delimiter is EOF. Of course, you can define it as another character.
You can use this method to automatically log on to ftp as required by requirement 1 and perform a series of operations. The Code is as follows:
- ftp -i -n 192.168.167.187 << EOF
- user hzc 123456
- cd test
The test showed that the above Code successfully logged on to the ftp server with the account name hzc and password 123456, and entered the directory, printed out the pwd.
V. Automatic Interaction method 2
In requirement 2, non-interactive login password change is required. method 1 cannot be used.
At this time, we think of another Automatic Input Method of interactive information, pipeline, which can be achieved through echo + sleep + |.
- (echo "curpassword"
- sleep 1
- echo "newpassword"
- sleep 1
- echo "newpassword")|passwd
Test passed. Run this script to change the current user's curpassword to newpassword.
Vi. Automatic Interaction method 3
In requirement 3, automatic logon to the root account is required. If methods 1 and 2 are attempted, the error code "standard in must be a tty" is displayed.
At this time, I tried to find external help. A shell tool called javasct can implement this function. In fact, javasct is a tool specifically used to implement automatic interaction. The syntax of javasct can refer to the relevant information. The Code is as follows:
- spawn su root
- expect "password: "
- send "123456\r"
- expect eof
Test passed. Run this script to log on to the root user directly from the current user.
VII. method summary
Method 1 (redirection) is simple and intuitive, and often has practical applications. However, it has limited functions in the field of automatic interaction.
Method 2 (pipeline) is simple and intuitive. Sometimes it can show powerful automatic interaction without sleep, but sometimes it is helpless.
Method 3 (CT) is the most powerful in terms of functions. CT was originally born to realize the automatic interaction function, but the disadvantage is that it is difficult to install the CT package in an embedded environment.
Each of the three methods has its own advantages and disadvantages. If the application is good, automatic Linux Shell interaction can be completed.
- Open the door to Linux
- Anti-hacker attack disguised by Linux Firewall
- 20 years of experience: Linux embedded
- 10 of the best free Linux ERP software
- Chrome 4.0 Beta for Linux and Mac