1-26-1-expect no interactive-regular expression

Source: Internet
Author: User
Tags log log

Outline:

1, expect environment construction and script writing

Overview

Expect script explanation

Expect environment construction

Expect scripting for remote SSH connection

Expect script for SSH remote connection (pass parameters via Shell)

2. A detailed description of the regular expression and an example

Detailed

Regular regular

Extended Regular

Case

1, expect environment construction and script writing

I. Overview

Through the shell we can achieve simple control flow functions, such as: cycle, judgment and so on.

But for situations where interaction needs to be done manually, we may sometimes need to implement interactions with interactive programs such as SSH servers.

Expect the tools used to implement this function.

Expect is a free programming language that enables automated and interactive tasks to communicate without the need for human intervention.

Expect's author, Don Libes, began writing expect in 1990 with the following definition of expect:

1.Expect is a software suite for automating interactive functions.

2. The expect can simulate the input provided by the standard input to the program according to the program's prompt to realize the interactive program execution.

3. You can even implement a simple BBS chat robot.

Expect is a continuous development, with the passage of time, its function becomes more and more powerful, has become the system administrator's a powerful assistant. expect requires the support of the TCL programming language and TCL must first be installed to run expect on the system.

SOURCE Download Link

Http://jaist.dl.sourceforge.net/project/tcl/Tcl/8.6.4/tcl8.6.4-src.tar.gz

Http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download

Second, expect script detailed

1. Like writing shell scripts, the first line tells the command interpreter who is the program that executes the script.

#!/usr/bin/expect #expect程序的路径

This line tells the code in the operating system script to use that one main program to execute.

2, of course, in the non-interactive script, we also need to set a response time, when more than this time, still no response when the script will automatically disconnect, not always wait, base unit: seconds (s)

Set Timeout 30

Set setting variables

This is set to timeout, and now you just have to remember his time unit is: seconds.

Timeout-1 to Never timeout

3. In the expect script, use the spawn (expect internal command) to execute the interactive command

Spawn ssh [email protected]

If the expect is not installed or is executed directly under the default shell, the Spawn command cannot be found.

Its main function is to add a shell to the SSH running process to pass the interactive instructions.

4, after the execution of the interactive command, of course, it is necessary to expect the script according to the output judgment, to determine the analog input data! In this case, the expect (expect internal command) is used, in the following format:

Expect {

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

"Password" {send "123456\r"}

}

Expect's shell commands are the same as the internal commands, but not a function, and the habit is good.

This command means to determine whether the last output contains a string, if any, return immediately, or wait for a period of time to return, where the waiting length is the previous set of 30 seconds

"Yes/no" to determine the previous output if the string "yes/no", send will be analog input, sending "yes" to the terminal, but need to add the Terminator \ r in order to determine, this time the input is finished. Exp_continue continues to execute judgment.

5.Send

This is where the interactive action is performed, which is equivalent to the manually entered action.

Tip: Don't forget to add "\ r" to the end of the command string, and check if there is an abnormal wait.

6,exp_continue

Continue with the next interactive operation

7,interact

After the completion of the implementation of the interactive State, the control to the console, this time can be manually operated.

If this is not done, it will exit instead of remaining on the remote terminal.

8.$argv parameter Array

The expect script can receive parameters passed from the shell, using the

[lindex $argv N] obtained the n+1 parameter (n starting from 0, representing the first ($), the second ($), the third ($) ... Parameters

$ARGV 0

Third, Environment construction:

1, upload the download Tcl and expect source package

2. Installing TCL

2.1. Unzip the TCL source package into the/USR/SRC directory

TAR-ZXF tcl8.6.4-src.tar.gz-c/USR/SRC

2.2 into the source directory, in the source directory, there are many system support, see (Linux, Windows, iOS):

2.3 (Linux system) enters the UNIX directory, configures the source installation directory/USR/LOCAL/TCL, and opens the shared

Cd/usr/src/tcl8.4.20/unix

./configure--PREFIX=/USR/LOCAL/SRC--enbale-shared

echo $? #检查上一条命令是否执行成功

2.4 Compiling and installing

Make-j 4 && echo $?

Make install && echo $?

2.5 Copying and receiving oral documents into the generic directory

Cp/usr/src/tcl8.4.20/unix/usr/src/tcl8.4.20/generic

3, installation expect

3.1 Unzip the expect5.45.tar.gz to the/USR/SRC directory

TAR-ZXF expect5.45.tar.gz-c/USR/SRC

3.2 Switch directories and configure the source code: Specify the installation directory for/usr/local/expect specify TCL library directory, specify TCL header file directory

./configure--prefix=/usr/local/expect--with-tcl=/usr/local/tcl/bin/lib--with-tclinclude=/usr/src/tcl8.4.20/ Generic

echo $?

3.3 Compiling and installing

Make-j 4 && echo $?

Make install && echo $?

3.4 Optimization Program

Expect is Tcl's command, so we need to connect expect to expect directory

Ln-s/usr/local/tcl/bin/expect/usr/local/expect/bin/expect

In order to write a script, it is convenient to write, we can connect the expect to the directory of/bin or/usr/bin

Ln-s/usr/local/tcl/bin/expect/usr/bin/expect

See MORE:

Note: For convenience and post-inspection, I saved the configuration, compilation, and installation process information separately in the. Log log file

3, write expect script, realize no interactive SSH remote connection

vim ssh.exp #使用exp结尾 for later management and identification

chmod +x ssh.exp #对脚本添加可执行权限, can also use expect Ssh.exp

./ssh.exp

For details, see:

4, scripting, through the shell passed the parameters to implement SSH Telnet

Vim Argv_ssh.exp

Cat Argv_ssh.exp

Expect Argv_ssh.exp 192.168.171.63 123456

For details, see:

2. A detailed description of the regular expression and an example

First, Detailed:

The Linux Shell conveys meaning to the system as a string of characters,

Metacharacters: Characters with special meanings

A character used to describe the meaning of a character expression, which is a character that describes a character.

Used for the content of a character expression.

Description of the conversion and operation information.

Description: An expression that conforms to certain rules

Function: Text query and character manipulation, can match a character or character set of text.

String (match. Cut. Replace. Get)

So learning regular expressions is the use of learning some special symbols.

Benefit: You can simplify complex operations on strings. Cons: The more symbol definition, the longer the regular, the worse the reading.

Application Range:

Programming language: Java Perl Python

Shell tools:

grep finds rows containing a string from a specific file or from standard input

Sed reads information from input, edits output

awk Text Processing

Database: MYSQL

grep command

Use grep with regular expressions to search for data to print matching rows

Usage:grep [OPTION] ... PATTERN [FILE] ...

Pattern can be a string, can be a variable, or it can be a regular expression

When your pattern contains spaces, you need to enclose the pattern in double quotation marks, which can easily be mistaken for a file name after a space.

grep Hello World A.txt If you don't put the Hello World in quotes then you think world is a file;

GREP supports multi-file queries

OPTIONS

- e extended Regular Expression egrep (PATTERN is an extended regular expression (ERE))

-I ignores case Ignore case Distinctions

-V Show mismatched rows Select non-matching lines

-F #fgrep (PATTERN is a set of newline-separated fixed strings)

-e--regexp=pattern #use PATTERN for matching

-F--file=file #obtain PATTERN from File

-L #print only names of FILEs containing matches

-L #print only names of FILEs containing no match

-Q #suppress All normal output (keep silent)

-S #suppress error messages

-W--word-regexp #force PATTERN to match only whole words

-X--line-regexp #force PATTERN to match only whole lines

-H #省略文件的名称 Suppress the file name prefix on output

-N #列出所有的匹配行, and the line number is displayed with output lines

-C #只显示匹配行数量print only a count of matching lines per FILE

Regular expression composition

General characters: characters with no special meaning

Special characters: meta-character Meta, which has special meaning in regular

POSIX Bre and ere have meta characters: Common meta characters:

\ Escape, turn off the meaning of subsequent special characters. such as \ (... \) with \{...\}

. Match any single character (except null)

* matches any number of characters preceding it or not, in wildcards, * matches any character

^ matches the line at the beginning of the following character or string,^ in [], indicating that the corresponding value does not match

$ matches the preceding character or the line at the end of the string

[] matches any one of the characters contained in the brackets, where a hyphen (-) is used to refer to the range of contiguous characters, and the ^ symbol, if it appears in the first position of the square brackets, matches any character that is not in the list.

Regular Regular:

\{n,m\}: Interval expression that matches the nth occurrence of the character appearing to the first m

\ (\) reserve space, you can store up to 9 independent sub-patterns in a single mode. such as \ (ab\). *\1: Refers to the two occurrences of a matching AB combination, with any number of characters in the middle.

\ N Repeats the mode of this point in nth sub-mode within ()

Extended Regular:

{n,m} is the same as Bre's \{n,m\} function

+ Match one or more extensions of the preceding regular expression

? Match 0 or one extension of preceding regular expression

| Match | Regular expression before and after a symbol

() match parentheses surround regular expression group

Simple formal expression matching case

China: Matches rows with Chinese characters anywhere on this line

^china: Matches lines starting with China

china$: Matches lines ending in China

^china$: Matches lines with only five characters in China

[Cc]hina: Matches rows containing China or China

Ch.na: Matches a line containing CH two letters followed by an arbitrary character, followed by a na two characters

China

Chana

Ch.*na: Matches a row with ch characters followed by 0 or more characters, and then continues with the NA two character

[1234] matching one of the 1,2,3,4

[1-9] One of the number 1-9

[A-za-z] All letters

[^0-9] A non-numeric character

A\{2\}b matching AaB

A\{4,\}b matches a string that has a B after at least 4 occurrences

A\{2,4\}b match a appears between 2 and 4 times, followed by a string of B

Case:

Show/proc/meminfo lines starting with S, not case-sensitive

Grep-i "^s"/proc/meminfo

Displays lines in/etc/passwd that do not end with/bin/bash

Grep-v "/bin/bash$"/etc/passwd

Find lines that start with root/etc/passwd display line numbers

grep "^root"-n/etc/passwd

Find rows that contain numbers

grep "[0-9]"/etc/yum.repos.d/rhel7.repo

Script Filter Site domain name

grep "^https\{0,1\}.*\. * "Url.txt

grep "^https\{0,1\}.*\. *\.com "Url.txt

grep "^https\{0,1\}://.*\. *\.com "Url.txt

grep "^https\{0,1\}://.*\.baidu.*\.com" Url.txt

grep "\ (https\) \{2\}" Url.txt

Match IP Address:

Ifconfig | grep "[0-9]*\. [0-9]*\. [0-9]*\. [0-9]*]

Ifconfig | grep "[0-9]*\. [0-9]*\. [0-9]*\. [0-9]*] | head-1 | awk {' Print $ '}

1-26-1-expect no interactive-regular expression

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.