Brief introduction:
The 1.expect explanation
2. Instance Operation automatic Logon script
Expect explanation
Expect allows us to automatically log on to remote machines, and can implement automatic remote execution of commands. Of course, if you use key authentication without a password, you can also implement automatic logon and automatic remote command execution. But when we can't use key verification, we don't have a choice. So, just know the other machine's account number and password can be implemented through the expect script login and remote command.
The most critical four commands in expect are send,expect,spawn,interact.
send:用于向进程发送字符串
expect:从进程接收字符串
spawn:启动新的进程
interact:允许用户交互
1. Send command
The Send command receives a string parameter and sends the parameter to the process.
expect1.1> send "hello world\n"hello world
2. Expect command
The expect command is just the opposite of the Send command, and expect is typically used to wait for feedback from a process. Expect can receive a string parameter, or it can receive a regular expression parameter.
Expect "hi\n" send "Hello there!\n"
These two lines of code mean: After the standard input medium to hi and the newline key, output hello there to the standard output
3. Spawn command
All of the demos above are interacting with the standard input and output, but we want him to interact with a process. The SPAWM command is used to start a new process.
set timeout -1spawn ftp ftp.test.com //打开新的进程,该进程用户连接远程ftp服务器
expect "Name" //进程返回Name时
send "user\r" //向进程输入
anonymous\rexpect "Password:" //进程返回Password:时
send "123456\r" //向进程输入[email protected]\r
expect "ftp> " //进程返回ftp>时
send "binary\r" //向进程输入binary\r
expect "ftp> " //进程返回ftp>时
send "get test.tar.gz\r" //向进程输入get test.tar.gz\r
4.interact
So far, we have been able to combine spawn, expect, and send automation to accomplish many tasks. But how to get people to intervene in the process at the right time. For example, when downloading the ftp file, you can still stay in the FTP command line state, in order to manually execute subsequent commands. Interact can achieve these goals. The following demo allows users to interact after automatically logging on to FTP.
spawn ftp ftp.test.com
expect "Name"
send "user\r"
expect "Password:"
send "123456\r"
interact
2 main units
Service side: back:192.168.19.134
Client: other:192.168.19.135
Service-side operations
1. Before using expect, you need to install expect:
Yum Install-y expect
Installing SSH
Yum Install Openssh-server-y
Yum-y Install Openssh-clients
2. Create a new automatic logon script file
Format:
Spawn ssh-l username 192.168.1.1
expect{
"(yes/no)?" {send "yes"; Exp_continue }
"Password:" {send "AAAA"}
}
Interact
Vim Login.expect
#! /usr/bin/expect
#about Login
#written by Lizheng
Set Host "192.168.19.135"
Set passwd "Lizheng123"
Spawn ssh [email protected] $host
Expect {
"Yes/no" {send "yes\r"; Exp_continue}
"Password:" {send "$passwd \ r"}
}
Interact
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/8C/23/wKioL1hjdA2A6OHOAACZD-_uh4A256.png-wh_500x0-wm_3 -wmp_4-s_2171822207.png "title=" 1.png "alt=" Wkiol1hjda2a6ohoaaczd-_uh4a256.png-wh_50 "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
3. Change permissions
chmod +x Login.expect
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s1.51cto.com/wyfs02/m02/8c/23/wkiol1hjdbxw_-f4aaah4pcchdg161.png-wh_500x0-wm_3-wmp_4-s_526202758.png "title= "2.png" alt= "Wkiol1hjdbxw_-f4aaah4pcchdg161.png-wh_50"/>
4. Executing script statements
./login.expect
Enter the client's password to log in
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m01/8c/23/wkiol1hjdb6wpdhnaaaaly_y8tk576.png-wh_500x0-wm_3-wmp_4-s_1898479088.png "title = "3.png" alt= "Wkiol1hjdb6wpdhnaaaaly_y8tk576.png-wh_50"/>
5. Exit
Logout
Text Tutorial Automatic login expect script instance