Implementation of non-interactive commands in two types of Linux

Source: Internet
Author: User

Implementation of non-interactive commands in two types of Linux

I. Overview

In Linux, there are a variety of automated scripting languages, such as Shell, Python, Perl, and Tcl. The Shell language is the most common automated scripting language because it is closely integrated with the Shell.

At the same time, there are a lot of single-function gadgets in the Linux environment-usually they can get output immediately after specifying input, such as echo, cat and other string/text printing tools, or the most common string editing tools such as sed and awk. By writing Shell scripts, we can use these gadgets repeatedly to implement automated batch processing.

In a few cases, we also need some interactive tools, such as passwd and fdisk-which means we have to pause in the automated execution process, the interactive process that meets the requirements of the tool itself. This article describes two ways to automate interactions when using interactive commands.

 

2. Interactive commands are controlled using delimiters

The '<' symbol in Shell indicates a special redirection, which is used to pass multiple lines of input to a command. When this symbol appears in the script, Shell first regards a string followed by '<' as a specialDelimiterThen, the user can input a series of data. Finally, the user starts a single row and uses the sameDelimiterEnd. In twoDelimiterIs considered as the input parameter of the command.

In actual software development projects, cat is used to input a piece of text into a file. For example, the content in the following example1.sh script:

Cat> example1. SQL <EOFSQL

Declare @ num int, @ SQL nvarchar (1000)
Set @ SQL = 'select @ a = count (*) from tablename'
Exec sp_executesql @ SQL, n' @ a int output', @ num output
Select @ num

<EOFSQL

The running result of this script is to generate an example1. SQL file with the content shown between the two delimiters EOFSQL.

Using cat commands to input strings into a text file is not interactive. When interactive commands are used, the delimiters can control the input parameters of commands to meet different control flow requirements.

 

3. Use the keep CT command to control multiple commands

When performing other operations (such as ssh logon), we cannot enter multiple commands using the delimiter, because the ssh Input Source is terminal (the delimiter input, should be understood as a string in the general sense ). In this case, we can consider using the reverse CT command.

The trusted CT command is a tool used in Linux to Process Interactive commands in non-interactive ways. Compared with the delimiters, it is more powerful and can implement all functions that the delimiters can achieve.

The operating principle of the reset CT command is to start a new process in shell and simulate the terminal in the process to fully monitor the input/output. To use the keep CT command, you must write an independent script.

CT has many parameters and syntaxes. Below we will only introduce some basic sections for your reference.

1 .[#! /Usr/bin/CT]

The first line of the CT script always uses this syntax. This is similar to the shell script #! /Bin/sh. The meaning of this sentence is also the interpreter that defines the script.

It is worth noting that, depending on the operating system, the locate CT command may also be at/usr/local/bin/locate CT. Before use, the user should confirm whether the tool exists (the confirmation method is to execute the "which risk CT" command under the root user ).

 

2. [set timeout 30]

Set the response timeout time in seconds. Set timeout-1 to never time out.

 

3. [spawn ssh-l username ip]

Spawn is an internal audit CT command that can be executed only after entering the audit CT environment. Its main function is to add a shell to the ssh running process to transmit interactive commands.

 

4. [cT "xxxxx:"]

CT is also an internal command. This command is used to determine whether the previous output result contains a specific string of "xxxxx:". If yes, it will be returned immediately.

Secondary CT also has some extended usage, the most common of which is as follows:

CT {

"Xxx1" {command1; exp_continue}

"Xxx2" {command 2}

}

 

The above code is similar to the switch statement in C language. Different conditions are similar to different case clauses. The exp_continue is likeDo not addBreak statement. The program continues to execute the following statements.

 

5. [send "ispass \ r"]

This is an interactive action, that is, the input of a simulated terminal. It is worth noting that do not add "\ r" to the end of the command string ".

 

6. [cT eof]

Exit the secondary CT interpreter.

 

The following is an example of using the keep CT command:

#! /Usr/bin/CT-f

Set timeout-1

Spawn ssh-l zhou 10.10.10.10

CT {

"Yes/no" {send "yes \ r"; exp_continue}

"* Assword:" {send "zhou "}

}

Keep CT "*>" {send "echo 'helloworld! '\ R "}

CT "*>" {send "exit \ r "}

CT eof

In this example, we use ssh to log on to 10.10.10.10 as the user zhou, and then enter the user's password in ssh. After successful login, we print "Hello World !", Then exit ssh and end the execution of the reset CT command.

For more detailed usage of the reverse CT command, see the man manual.

 

Iv. Summary

This article briefly introduces two methods in Linux to achieve Automation by avoiding interaction when using interactive commands, for the reference of relevant developers.

 

 

-----------------

My public account: zhouzxi. Please scan the following QR code:

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.