Shell script Common commands: Expect

Source: Internet
Author: User
Tags set time ssh ftp

Expect:is based onTcla relatively simple language for a free scripting tool

used to implement automated and interactive task programs for communication without manual intervention. For example:ssh ftp , and so on, these programs normally need to manually interact with them, but with expect can be used to simulate the process of human interaction, automatic and Remote program interaction, so as to achieve the purpose of automation.

Support: Unix, Linux, and Windows platforms

1) Expect program workflow:spawn Start process àexpect expected keyword àsend to process send character à exit End

2) Expect Software Installation:

Configure Yum andthen execute yum install expect–y to install expect software

3) Expect syntax : Command [ options ] parameter

4) Spawn

Spawn:expect initial command to start a process, all expect operations are performed in this process, and if there is no spawn statement, the entire expect will not be able to execute.

Spawn How to use:spawn ssh [email protected]

Spawn support options: -open start file process

-ignore ignoring certain signals

5) expect

expect How to use: expect expression action expression action .....

The expect command is used to wait for the output of a matched content, matching the command following the execution of expect.

6) Exp_continue

exp_continue: The use of the harsh conditions, first it is in a expect command, and then belong to an action, no exp_continue will be in turn down to match multiple conditions, add

each match after exp_continue is the first keyword to begin with. (see example below)

7) Send_user

Send_user: Used to output parameters to standard output, the default send,exp_send command is to export the parameters to the program.

Send_user "Please input passwd:"

This will print in the standard output , please input passwd: character, similar to "Echo".

Common parameters:-re means matching using regular expressions, using the following:

Spawn ssh [email protected] $ip

Expect {

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

"Password:" {send "$SSHUSRPASSWD \ r"}

}

Expect "SSHUSR"

Send "su-root\r"

8) Expect variable

variable setting method in expect

Set Variable name Variable value # Set Variable method

puts $ Variable name                    # Read Variable method

9) Expect keywords

Expect keywords used in the match process, representing certain special meanings or states, are generally used in expect family commands and cannot be used alone outside, but also can understand events.

use similar to: expect EOF {}

9.1 EOF

The EOF keyword is used to match the Terminator, such as the Terminator of the file, the FTP transmission stop, and so on, the keyword followed by the action to do further control, especially the FTP interactive operation

,EOF has a great effect.

Spawn ftp [email protected]

Expect {

"Password:" {exp_sed "who Im I"}

EOF {FTP Connect close}

}

Interact {}

9.2 Timeout

Timeout is an important variable of expect, it is a global time switch, timeout parameter (seconds), matching time-out.

Example: Telnet to the device and enter the password username and password to modify the operation

#!/bin/sh

#username

User= ' Sshusr '

user2= ' Root '

#sshusr

newpasswd= ' 1111 '

#root

Newpasswd2= ' 2222 '

#sshusrpasswd

#newpasswd

#sshusrpasswd = ' 1111 '

#EnterPasswd

sshusrpasswd= ' 0000 '

#rootpasswd

#newpasswd

#rootpasswd = ' 2222 '

#EnterPasswd

rootpasswd= ' 0000 '

Count=0

Rm/tmp/iomdeskscript/changepasswd/test/expect.log

For IP in ' cat/tmp/iomdeskscript/changepasswd/test/devlist.txt '

Do

Expect << EOF

Set time 5

Log_file/tmp/iomdeskscript/changepasswd/test/expect.log

Spawn ssh [email protected] $ip

Expect {

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

"Password:" {send "$SSHUSRPASSWD \ r"}

}

Expect "SSHUSR"

Send "su-root\r"

Expect "Pa"

Send "$ROOTPASSWD \ r"

Expect "*#"

Send "passwd $user \ r"

Expect "New"

Send "$NEWPASSWD \ r"

Expect "reenter"

Send "$NEWPASSWD \ r"

Send "passwd $user 2\r"

Expect "New"

Send "$newpasswd 2\r"

Expect "reenter"

Send "$newpasswd 2\r"

Send "exit\r"

Send "exit\r"

Expect EOF

Eof

Count= ' expr $count + 1 '

Done

Success= ' Grep-i "Password changed"/tmp/iomdeskscript/changepasswd/expect.log |wc-l '

echo Change $count Dev, success $success dev~~~


Shell script Common commands: Expect

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.