Introduction and expect an example of use (rpm)

expect Profile

expect is an automated script interpreter type of tool.

Based expect tcl script, the script needs to run expect the support tcl.

expect some interactive input of commands require very helpful, such as ssh ftp scp telnet.

Remote login linux server time, ssh command need to manually enter a password when log on multiple machines can be very tedious.

We can expect according to the rules set automatically help us to enter a password, saving time.

expect Installed

General machine comes not expect, you need to be installed manually.

The system is RHEL / CentOS:

yum install expect

The system is Debian / Ubuntu:

apt-get install expect

expect the basics

expect script

Beginning of the script

generally expect script #! / usr / bin / expect the beginning of the -f, similar bash script.

Common suffixes

expect script often .exp or .ex end.

expect major command

  • spawn a new process, this process by interacting expect control
  • expect to wait for the process to accept the return of the string until the timeout, decide the next step according to the rules
  • send send strings expect to process control
  • set set the variable to a value
  • exp_continue expect to re-execute the command branch
  • [Lindex $ argv 0] Gets expect script first parameter
  • [Lindex $ argv 1] Gets expect script the first two parameters
  • set timeout Set timeout of -1 to wait forever
  • set timeout 30 set the timeout time is 30 seconds
  • interact to control the script to the user, the user can continue to enter the command
  • After waiting spawn the process expect eof eof exit signals

expect command branch

expect command uses tcl modes - Action Syntax, which has the following modes:

Single branch grammar

set password 123456
expect "*assword:" { send "$password\r" }

When the output matching * assword: when the value of the variable password, and outputs the transport.

Multi-drop mode syntax

set password 123456
expect {
      "(yes/no)?" { send "yes\r"; exp_continue }
      "*assword:" { send "$password\r" } }

When the output is included (yes / no)?, Output yes and press Enter, and multi-branch re-execute this statement.

When the output matching * assword: when the value of the variable password, and outputs the transport.

expect explain in detail

ssh remote login expect script

Here is an automatic system login script hostname1 and hostname2 disconnected after performing uname -a.

First establish login.exp

touch login.exp
chmod +x login.exp
vim login.exp

It reads as follows:

#!/usr/bin/expect -f

set timeout -1  //永远等待,不会超时
spawn ssh root@hostname1   //spawn 后面跟命令名称和参数

//如果匹配到*assword,那么发送密码,并进入下面的expect语句(uname -a语句)。 //如果匹配到yes/no,那么发送yes,并重新执行这个expect语句。 expect { "*assword" {send "123456\r";} "yes/no" {send "yes\r";exp_continue} } //匹配到*]#,那么运行uname -a命令 expect "*]#" {send "uname -a\r"} send "exit\r" //退出远程登录 expect eof //结束spawn //开始下一个命令 spawn ssh root@hostname2 expect { "*assword" {send "123456\r";} "yes/no" {send "yes\r";exp_continue} } expect "*]#" {send "uname -a\r"} send "exit\r" //退出远程登录 expect eof //结束spawn exit //退出expect脚本 

ssh remote login shell script (nested expect)

shell used expect -c "expect script content" to complete the nest.

note:

expect script inside the "need preceded by an escape character.

Expect behind each statement with a semicolon ";."

vim expect_in_shell.sh

#!/usr/bin/bash

HOSTS="hostname1 hostname2"  
  
for host in $HOSTS do expect -c " set timeout 5; spawn ssh root@${host}; expect { \"*assword\" { send \"123456\r\" } \"yes/no\" { send \"yes\r\"; exp_continue } } ; expect \"*]#\" {send \"uname -a\r\" } ; send \"exit\r\" //退出远程登录 expect eof " done 

expect ssh login script with arguments

vim login_arg.exp

#!/usr/bin/expect -f
set ip [lindex $argv 0]   //第一个参数赋值给变量ip
set password [lindex $argv 1] //第二个参数复制给变量password set timeout -1 spawn ssh root@$ip expect { "password" {send "$password\r";} "yes/no" {send "yes\r";exp_continue} } interact //停留在远程shell 

Run with no arguments login_arg.exp

chmod +x login_arg.exp
./login_arg.exp 127.0.0.1 123456

Guess you like

Origin www.cnblogs.com/alpha1981/p/10966906.html