ExpectMediumShaoxing
expect is by Don Libes based on Tcl (Tool Command Language) Language developed, primarily for automated interactive operation scenarios, using expect to handle interactive commands, you can automate the process by writing interactive processes such as SSH logins, FTP logins, and so on in a single script. This is especially useful in environments where multiple servers are required to perform the same operation, which can greatly improve the productivity of system administrators.
expectCommandExpectSyntax:
expect [ options ] [- c Cmds] [[[-[f|b]] cmdfile] [args]
Options
-C: Executes the expect scriptfrom the command line , and the default expect is executed interactively
Example:
Expect-c ' expect ' \ n ' {send ' pressed enter\n '}
-D: Output debug information can be exported
Example:
Expect-d Ssh.exp
expect related commands in
Spawn: Start a new processSend: Used to send a string to a processExpect: Receive a string from a processInteract: Allow user interactionExp_continueMatch multiple strings add this command after performing an action
ExceptThe most common syntax(Tcllanguage:Mode-Action)Single branch mode syntax:
Expect "HI" {send "you said Hi\n"}
After matching to hi , it will output "you said hi"and wrap
Multi-branch mode syntax:
Expect "HI" {send "you said hi\n"} \
"hehe" {send "hehe yourself\n"} \
"Bye" {send "good bye\n"}
Match Hi,hello,bye any string, the corresponding output is executed. Equivalent to the following:
Expect {
"HI" {send "you said Hi\n"}
"hehe" {send "hehe yourself\n"}
"Bye" {send "good bye\n"}
}
Example
Cat Ssh1.exp
#!/usr/bin/expect
Spawn ssh 192.168.8.100
Expect {
"Yes/no" {send "yes\n"; Exp_continue}
"Password" {send "magedu\n"}
}
Interact
#expect EOF
Example:variables
Cat Ssh2.exp
#!/usr/bin/expect
Set IP 192.168.8.100
Set User root
Set Password magedu
Set Timeout 10
Spawn ssh [email protected] $ip
Expect {
"Yes/no" {send "yes\n"; Exp_continue}
"Password" {send "$password \ n"}
}
Interact
Example:Position Parameters
Vim Ssh3.exp
#!/usr/bin/expect
Set IP [lindex $argv 0]
Set user [lindex $argv 1]
Set password [lindex $argv 2]
Spawn ssh [email protected] $ip
Expect {
"Yes/no" {send "yes\n"; Exp_continue}
"Password" {send "$password \ n"}
}
Interact
#./ssh3.exp 192.168.8.100 rootmagedu
Example: executing multiple commands
Cat Ssh4.exp
#!/usr/bin/expect
Set IP [lindex $argv 0]
Set user [lindex $argv 1]
Set password [lindex $argv 2]
Set Timeout 10
Spawn ssh [email protected] $ip
Expect {
"Yes/no" {send "yes\n"; Exp_continue}
"Password" {send "$password \ n"}
}
Expect "]#" {send "Useradd haha\n"}
Expect "]#" {send "echo magedu |passwd--stdin haha\n"}
Send "exit\n"
Expect EOF
#./ssh4.exp 192.168.8.100 rootmagedu
Example:ShellScript callsexpect
Vim ssh5.sh
#!/bin/bash
Ip=$1
User=$2
Password=$3
Expect <<eof
Set Timeout 10
Spawn [email protected] $ip
Expect {
"Yes/no" {send "yes\n"; Exp_continue}
"Password" {send "$password \ n"}
}
Expect "]#" {send "useraddhehe\n"}
Expect "]#" {send "echomagedu|passwd--stdinhehe\n"}
Expect "]#" {send "exit\n"}
Expect EOF
Eof
#./ssh5.sh 192.168.8.100 Rootmagedus
This article is from the "13145479" blog, please be sure to keep this source http://13155479.blog.51cto.com/13145479/1970741
Expect commands in Linux