3 ways to automatically interact with the shell "Go"

Source: Internet
Author: User

I. Background

Shell scripts can save a lot of time in dealing with automatic loops or large tasks, creating a command manifest that handles tasks, using variables, conditions, arithmetic, and loops to quickly create scripts to do the job, which saves time and effort when typing commands one at a command line.

But sometimes we may need to implement and interactive programs such as Ftp,telnet Server interactive function, this time we need to use the shell's automatic interaction function, this article collected more commonly used three automatic interaction methods, and the comparison and summary.

Second, demand

Requirement 1:
From a Linux machine FTP landing to another Linux machine, after the series of operations to shut down, too lazy to manually enter the password each time.
Requirement 2:
Change the login user password, do not bother to enter the old and new password each time.
Requirement 3:
I want Su to automatically log in to the root account and not bother to enter the root password every time.

Third, debugging environment

Terminal: SECURECRT
System: WinXP, CentOS 4.4 (VmWare)
Shell:bash

Note: There are many shell types, similar behavior between class B shells (SH, bash, ksh), similar behavior between Class C shells (CSH, tcsh), and shells such as zsh and RC, the debugging environment for this article is bash.

Iv. Automatic Interaction Method One

Automatic interaction is the key to the automatic input of interactive information, first Lenovo to file redirection, in shell programming has such a use (refer to the Linux and Unix Shell Programming Guide chapt 5.7): "Command << delimiter Reads from the standard input until the delimiter delimiter is encountered. "

The redirect operator Command << delimiter is a very useful order, and the shell will know that the input is finished by delimiter the delimiter until all the content before the next same delimiter is entered as input and the next delimiter is encountered. The most common delimiter delimiter is EOF, which can, of course, be self-defined for other characters.

For the requirements of 1 automatic landing FTP, and for the series of operations, you can use this method for automatic interaction. The code is as follows:

#!/bin/bashftp192.168. 167.187 <<123456pwdCD testpwdclosebyeeof

Test can be found, such as the code using the account name HZC, password 123456 successfully landed the FTP server, and entered the directory, printed out the PWD.

v. Methods of automatic interaction two

Requirements 2 requires a non-interactive way to change the login user password, try to use Method 1, can not be achieved.

This time Lenovo to the interactive information of another automatic input method, pipe, through echo + sleep + | This requirement can be achieved.
#!/bin/Bash (echo"curpassword"sleep1Echo  "newpassword"sleep1echo "  NewPassword") | passwd

Test pass, run this script, directly change the current user's curpassword to NewPassword.

Six, automatic interaction method Three

Requirement 3 requires the automatic login to the root account, try Method 1 and Method 2, both errors are indicated in the must be a TTY.

At this time to try to find outside help, a shell tool expect can implement this function, in fact, expect is a dedicated to implement automatic interactive function of the tool, expect syntax can refer to the relevant information, the code is as follows:
#!/usr/bin/su"" "123456\r  "expect Eofexit

The test passes, runs the script, and logs directly from the current user to the root user.

Vii. Summary of methods

Method One (redirection) is simple, intuitive, and often practical, but has limited functionality in the field of auto-interaction.
Method Two (pipeline) is also very simple and intuitive, and sometimes even without sleep with the ability to show a powerful automatic interaction, but at some point also helpless.
Method Three (expect) is the most powerful in function, expect originally is to realize automatic interactive function, but the disadvantage is to install expect package, in the embedded environment difficult to install.

Three methods each have the merits and demerits, the application is good, can let the actual work many trifles once and for all. (end) Transfer from http://blog.sina.com.cn/s/blog_62bbc49c0100fs0i.html

3 ways to automatically interact with the shell "Go"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.