Monday 17 October 2016

Primer on Expect scripting


This article is an elementary quick start towards learning expect scripting. Expect is a scripting language used to provide input to an interactive program non-interactively. The most well-known use of expect for UNIX administrators in feeding passwords on the command line non-interactively which can help speed up scripts by miles.

Expect is not installed in the OS by default but is available on the base repo in the OS ISO.

The following  expect commands are used when automating an interactive process over the shell:

  • send – to send the strings to the process
  • expect – wait for the specific string from the process
  • spawn – to start the command
  • timeout - time after which an expect script will exit.
  • set - declare variables for use in the code.


The path for the expect interpreter is /usr/bin/expect.

I'd just like to explore some avenues where us administrators make use of expect to make our life easier.

Common anatomy of an expect script:

#!/usr/bin/expect

##variable declaration (optional)##

##spawn <command> (starting the interactive session)##
##expect "password:" (expect a password prompt)##
##send "password\r" (pass the password string to the prompt non-interactively)##
##expect eof (end of script)##


Example 1: Change a user's password non-interactively.

Here's the code:

#!/usr/bin/expect

set timeout 10

set user [lindex $argv 0]

set password [lindex $argv 1]

spawn passwd $user

expect "password:"
send "$password\r"
expect "password:"
send "$password\r"

expect eof


The test looks like this:

# ./e3.sh testuser 123

spawn passwd testuser
Changing password for user testuser.
New password:
BAD PASSWORD: it is WAY too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.


Example 2: Login using expect & start an interactive session

#!/usr/bin/expect

set timeout 1

set login "test"
set addr "centdb"
set pw "123"

spawn ssh -t $login@$addr
expect "$login@$addr\'s password:"
send "$pw\r"

interact


Example 3: Run shell commands within an expect script

#!/usr/bin/expect

set timeout 1

set login "test"
set addr "centdb"
set pw "123"

spawn ssh -t $login@$addr
expect "$login@$addr\'s password:"
send "$pw\r"
expect "$"  # put here string from your server prompt
send "mkdir expect_testing\r"
expect "$" 
send "ls -l expect_testing\r"
expect "$" 

expect eof


Example 4: Using expect inside a bash shell script

#!/bin/bash

expect <<'EOF'

spawn passwd sa

expect "password:"
send "123\r"
expect "password:"
send "123\r"
expect eof

EOF

Example 5: Using expect within a bash shell script to initiate an ssh session

#!/bin/bash

##beginning expect block for entering password non-interactively##
/usr/bin/expect << EOL

set timeout 1
expect <<'EOF'

spawn ssh 192.168.87.129

expect "password:"
send "123\r"
expect "# "
send "uname -a;exit\r"
expect eof
EOF
##end of script##


Example 6: Running an expect one-liner using -c option (interactive session followed)

sa@buntu:~$ expect -c "spawn ssh sa@buntu; expect \"password:\";send \"123\r\";interact"


Example 7: Expect one-liner using -c option in a non-interactive session.

 expect -c "spawn ssh 192.168.87.129; expect \"password:\";send \"123\r\";expect \"# \"; send \"uname -a;exit\r\";expect eof"


The above examples are common uses of the expect script. 

I hope this helps someone somewhere save a lot of time spent entering passwords.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...