expect Tool

9.1 expect Tool

9.1.1 definitions

Write interactive scripting tool, but also a language

9.1.2 Installation

[root@localhost ~]#yum install expect 

9.1.3 define the variable

1.set variable name value

2.set variable name [lindex $ argv 0]

3. Special Variables

  • $ Argc number of arguments
  • All parameters $ argv array index number from 0

9.1.4 Statement

1.set defined variables

  • in seconds set timeout 300 #

2.spawn COMMAND

  • Monitor the output of the command

3.expect "STRING" {send "STRING"} capture the output, and perform other commands

4.send "STRING \ r" Send "String"

5.send_user "STRING" output information, corresponding to the shell of the echo

6.exp_continue continue to capture the output

7.interact stay in the interface, the control to the user

5.expect way to create a script

1. Create a file ending .exp

The first line #! / Usr / bin / expect

9.1.5 script

#!/usr/bin/expect

set user "root"
set pass "xxx"
set ip "192.168.217.130"


spawn ssh $user@$ip
expect {
        "yes/no" {
                send "yes\r"
                exp_continue
        }
        "password: " {
                send "$pass\r"
                exp_continue
        }

}

expect "#" {
		send "echo 123\r"
        send "exit\r"
}
 

expect eof
send_user "123\r"

Here Insert Picture Description

Pay attention to add line breaks or carriage returns. . .

Published 134 original articles · won praise 16 · views 6320

Guess you like

Origin blog.csdn.net/weixin_46108954/article/details/104707045