Teach you three methods of automatic Linux Shell Interaction

Source: Internet
Author: User

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.

I. background

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 and interact with each other.ProgramFor example, FTP and Telnet servers are used for interaction. At this time, we need to use the automatic interaction function of shell. This article collects three commonly used Automatic Interaction methods and compares and summarizes them.

Ii. Requirements

Requirement 1:
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.

Requirement 2:
Change the password of the logon user, so you do not have to enter the new and old passwords every time.

Requirement 3:
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

Terminal: securecrt

System: WINXP, centos 4.4 (VMware)

Shell: Bash

Note: There are many shell types. Class B shell (Sh, Bash, KSh) have similar behavior; Class C shell (CSH, tcsh) have similar behavior, 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 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 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.CodeAs follows:

#! /Bin/bash
FTP-I-N192.168.167.187<EOF
User hzc123456
PWD
CD Test
PWD
Close
Bye
EOF

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 + |.

 # ! /Bin/bash   
(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:

#! /Usr/bin/CT
Spawn su Root
Secondary CT"Password:"
Send"123456 \ r"
CT EOF
Exit

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.

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.