Using expect to implement automatic interaction of shell scripts

Source: Internet
Author: User

Using expect to implement automatic interaction of shell scripts

It is very effective for complex interactions and even different processing with different screen output.

And the General Linux, Unix have. It's very easy to use and powerful.

Spawn Telnet XXX
Expect "username"
Send "XXXXX/R"
Expect "password"
Send "XXXX/R"
Expect "last login*"
Send "XXX"
...

using expect to implement automatic interaction Expect's author, Don Libes, began writing expect in 1990 with the following definitions of expect:

(Expect is a software The suite for automating interactive tools.

Expect's official homepage describes it as follows:
Expect is a tool for automating interactive applications such as Telnet,
FTP, passwd, fsck, rlogin, Tip, etc. Expect really makes this stuff
Trivial. The Expect is also useful to testing these same applications. and
By adding Tk, your can also wrap interactive applications in X11 GUIs.

Expect is based on Tcl, and as a scripting language, expect can be implemented without administrator involvement

Automatic interaction (such as passwd,fsck,telnet, etc.), expect can also be used to automatically test some applications.

Expect syntax is very similar to the syntax of the shell, it supports function calls, there are while statements, switch

Statement.

1) Expect uses Spawn to invoke other execution programs, such as

Spawn Telnet 218.199.20.98 2600

However, in the course of use, it is found that spawn does not support pipelines and redirects, which means

LS |more mysql-p < update.sql such a command spawn does not parse correctly.

The solution is to put these commands in a shell script that executes the shell with spawn

Script.


2 Expect create a child function using the proc flag, which is also:

Proc functionname {Parameter1,parameter2} {
......

}

Calling a child function is very simple

functionname $param 1 $param 2

3) Expect uses expect, send combination to implement automatic interaction, the syntax is as follows:

Expect {
"Login;" {send ' $user/n '}
' passwd: ' {send ' $passwd/n '}

}
The content following the use of send is not displayed to the user, if you want to display to the user, you should use the Send_user

4) Note the point:

1. Expect inside are basically all using {} instead of using (), such as function parameter input outside application {},

should be while {} instead of while ().

2. {should be in accordance with other spaces, expect {correct, expect{will be an error.

3. Spawn does not support piping and redirection.

5 The following is the implementation of the MySQL database Automatic update expect script:

Proc Do_console_login {Pass} {

Set Timeout 5

Set done 1

While {$done} {
Expect {
"Password:" {
Send "$pass/n"
}
EOF {
Set done 0
}
}
}
}


If {$ARGC <1} {

Puts stderr "Usage: $argv 0 password./n"
Exit 1
}

Set pass [lindex $argv 0]

Spawn/usr/local/mysql/data/updatemysql

Do_console_login $PASS

Related Article

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.