Implementation of non-interactive commands under two Linux types

Source: Internet
Author: User

I. Overview

In the Linux environment, there are many scripting languages that automate, such as Shell, Python, Perl, Tcl, and so on. The shell language is the most common scripting language for automating, because it is tightly coupled with shell shells.

At the same time, there are a lot of single gadgets in the Linux environment-usually they get output immediately after specifying input, such as Echo, Cat, string/text printing tools, or the most commonly used string editing tools such as SED, awk, and so on. By writing shell scripts, we can use these gadgets over and over again to automate batch processing.

In a few cases, we also need to use some interactive tools, such as passwd, Fdisk, and so on-which means that in automating the execution of the process, we have to pause to meet the interactive process of the tool itself. This article describes two ways to automate when you use interactive commands to circumvent interactions.

Ii. controlling interactive commands with a delimiter

The ' << ' symbol in the shell represents a special redirection that is used to pass multiple lines of input to a command. When this symbol appears in the script, the shell first treats a string that follows the ' << ' as a special delimiter, and then the user can enter a series of data, and finally, the user starts one line at a time, ending with the other same delimiter. The contents between the two delimiters are treated as input parameters of the command.

What we see most in actual software development projects is the use of cat to enter 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 result of the script is to generate a Example1.sql file with content that is shown between the two delimiter eofsql.

Entering a string into a text with the Cat command is not interactive by itself. When using interactive commands, the delimiter can control the input parameters of the command and meet the requirements of different control flows.

Third, control multiple commands with expect command

When performing other operations (such as SSH login), we cannot enter multiple commands using the Delimiter method, because the input source of SSH is the terminal (the input of the delimiter, which should be understood as a string in the usual sense). At this point, we can consider using the expect command.

The expect command is a tool in Linux that specifically handles interactive commands in a non-interactive way. It is more powerful than the delimiter, and can achieve all the functions that the delimiter can achieve.

The expect command works by starting a new process in the shell and simulating the terminal in this process to fully monitor the input/output. To use the expect command, you need to write a separate script.

Expect has a considerable number of parameters and syntax, below we will only introduce some basic parts for your reference.

1.[#! /usr/bin/expect]

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

It is important to note that depending on the operating system, the location of the expect command may also be in/usr/local/bin/expect. Before use, the user should confirm that the tool exists (the confirmation method is to execute the "which expect" command under the root user).

2.[set Timeout 30]

Sets the time-out period for the response, in seconds. Setting timeout-1 means never time out.

3.[spawn ssh-l username IP]

Spawn is a expect internal command that can be executed after entering the expect environment. Its main function is to add a shell to the SSH running process to pass the interactive instructions.

4.[expect "xxxxx:"]

The expect here is also an internal command. This command means to determine whether a specific string of "XXXXX:" is included in the last output and returns immediately if any.

Expect also has some extended usage, the most common of which are as follows:

Expect {

"xxx1" {Command1;exp_continue}

"xxx2" {Command 2}

}

The above code has the same meaning as the C switch statement, with different conditions similar to different case clauses. The exp_continue is like a no -break statement, and the program continues to execute the following statement.

5.[send "Ispass\r"]

This is the execution of an interactive action, that is, an analog terminal for input. It is important to note that the end of the command string is not forgotten to add "\ r".

6.[expect EOF]

Exit the expect interpreter.

Here is an example of using the expect command:

#!/usr/bin/expect-f

Set Timeout-1

Spawn Ssh-l Zhou 10.10.10.10

Expect {

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

"*assword:" {send "Zhou"}

}

Expect "*>" {send "echo ' helloworld! ' \ r "}

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

Expect EOF

example, we logged in 10.10.10.10 with user Zhou via SSH, and then entered the user's password to SSH. After the login is successful, we print "Hello world! ", then exit SSH and end the execution of the expect command.

For more detailed usage of the expect command, refer to the man manual.

Iv. Summary

This article provides a brief introduction to the two ways to automate the use of interactive commands to circumvent interactions under Linux for reference by relevant developers.

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

My public number: ZHOUZXI, please scan the following two-dimensional code:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of non-interactive commands under two Linux types

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.