Expect

From TBP Wiki
Revision as of 10:14, 10 May 2019 by Heath (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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/.