Expect usage of shell

1. Installation

   The expect command does not come with the system and requires additional installation. installation method:

  #yum install expect

  #which expect

    /usr/bin/expect

2. Grammar

  

#!/usr/bin/expect #Declare that the interpreter is not bash

set timeout 2 #Set the timeout time, exit if it does not match the value of expect for more than 2 seconds

set username [lindex $argv 0] #Define the variable and assign it to the first parameter of the script

set password [lindex $argv 1] 

set hostname [lindex $argv 2] 

spawn /usr/bin/ssh $username@$hostname #spawn starts a process or executes a command, the expect and send later serve him

expect {

"yes/no"

{send "yes\r"; exp_continue;}

"Password:"

{send "$password\r";}

}

expect eof

Description:

When spawn creates a connection instead of executing a separate command, you need to use expect eof to end the process , otherwise the process may not end normally

When executing a command, you cannot bring expect eof.        Using expect eof will report an error, because no process can be disconnected


Guess you like

Origin blog.51cto.com/9911287/2562495