Difference between revisions of "Expect"

From TBP Wiki
Jump to: navigation, search
(Created page with "<strong>Expect</strong> is a program that talks to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and...")
 
Line 26: Line 26:
  
 
These both have to be set to executable by the owner of the files or by global with <code>chmod</code> and placed somewhere they are able to execute it from, e.g. /bin/.
 
These both have to be set to executable by the owner of the files or by global with <code>chmod</code> and placed somewhere they are able to execute it from, e.g. /bin/.
 +
 +
Autoexpect is a built-in script with expect that auto-generates an expect script from watching a session, basically autoexpect watches you interacting with another program and creates an Expect script that reproduces your interactions.
 +
 +
just type <code>autoexpect</code> and a new session will start. autoexpect will then begin recording your interactions until you stop it.
 +
 +
This has the downside of recording full output and putting them into an expect field. This can be avoided by using the <code> - p </code> flag which will put auto expect into prompt mode. In this mode, autoexpect will only look for the the last line of program output - which is usually the prompt. This handles the date problem (see above) and most others.

Revision as of 10:14, 10 May 2019

Expect is a program that talks to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. An interpreted language provides branching and high-level control structures to direct the dialogue. In addition, the user can take control and interact directly when desired, afterward returning control to the script.

ssh

Expect can be used to do most things. Setting up ssh to auto connect using a command, such as "jump", will automatically connect you to a secure shell if set up properly.

The first script is a basic bash script which, then, calls to the expect script:

   #!/bin/sh
   jumper PASSWORD 12.345.67.89 USERNAME

This bash script calls to the jumper expect script:

   #!/usr/bin/expect -f
   # set Variables
   set password [lrange $argv 0 0]
   set ipaddr [lrange $argv 1 1]
   set user [lrange $argv 2 2]
   # now connect to remote UNIX box (ipaddr) with given script to execute
   spawn ssh $user@$ipaddr
   match_max 100000
   # Look for passwod prompt
   expect "*?assword:*"
   # Send password aka $password
   send -- "$password\r"
   interact

These both have to be set to executable by the owner of the files or by global with chmod and placed somewhere they are able to execute it from, e.g. /bin/.

Autoexpect is a built-in script with expect that auto-generates an expect script from watching a session, basically autoexpect watches you interacting with another program and creates an Expect script that reproduces your interactions.

just type autoexpect and a new session will start. autoexpect will then begin recording your interactions until you stop it.

This has the downside of recording full output and putting them into an expect field. This can be avoided by using the - p flag which will put auto expect into prompt mode. In this mode, autoexpect will only look for the the last line of program output - which is usually the prompt. This handles the date problem (see above) and most others.