Reference:
Http://www.cnblogs.com/lzrabbit/p/4298794.html
Expect is a command that can be used to interact with child processes in a Linux system, and it can do some automated work. Python also has a module pexpect, which provides similar functionality.
For example: Using SSH login requires a password, you can use expect instead of manual input.
For example: Use passwd to modify your account password, or you can use expect instead.
Expect executes the file interactively, executes the command with various execution methods.
1. Interactively enter expect on the command line to enter interactive
Common commands
Send the string to the child process or to the current process, and note that you want to end with \ n
Expect the expected string
Spawn opening a child process
Interact
2. Write the expect command to the file and execute the file
Expect's most commonly used grammar is the mode of the TCL language-action
Multiple branches, matched to a string, perform the subsequent action.
expect {
"hi" { send "You said hi\n"}"hello" { send "Hello yourself\n"}"bye" { send "That was unexpected\n"}}
#!/usr/bin/expect-f
Set ipaddr "192.168.16.222"
Set passwd "YOURPASSWD"
Spawn ssh [email protected] $ipaddr #spawn to open a child process execution command.
# expect is executed sequentially, the braces are matched to the corresponding pattern, and the corresponding command is executed.
Expect {
"(yes/no)?" {
Send "yes\n"
Expect "Assword:" {send "$pasword \ n"}
}
"Assword:" {
Send "$password \ n"
}
}
3. Execute directly on the command line with the-c parameter
Expect-c ' expect "dddd" {send "dddd"}
Expect basic use method