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

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, 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:

 
 
  1. #!/bin/bash  
  2. ftp -i -n 192.168.167.187 << EOF 
  3. user hzc 123456  
  4. pwd  
  5. cd test  
  6. pwd  
  7. close  
  8. bye  
  9. 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 + |.

 
 
  1. #!/bin/bash  
  2. (echo "curpassword"  
  3. sleep 1  
  4. echo "newpassword"   
  5. sleep 1  
  6. 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:

 
 
  1. #!/usr/bin/expect  
  2. spawn su root  
  3. expect "password: "  
  4. send "123456\r"  
  5. expect eof  
  6. 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.

  1. Open the door to Linux
  2. Anti-hacker attack disguised by Linux Firewall
  3. 20 years of experience: Linux embedded
  4. 10 of the best free Linux ERP software
  5. Chrome 4.0 Beta for Linux and Mac

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.