Back to the main page

Expect script

Expect is tool for writing interactive scripts to be used with applications like telnet, ftp and such. It is an extension to the Tcl scripting language.

Example 1

In this example I have the script to query disks from StorEdge 6120 (old storage supports only telnet). It's just a quick script, can be added many features for checking if disk is okay, etc (maybe do some when find more time).

#!/opt/csw/bin/expect
# debugging: 0=no 1=yes
exp_internal 0
# spawn process output: 0=no 1=yes
log_user 0

# some help
# $expect_out(0,string) keeps only matched characters
# $expect_out(buffer) keeps matched characters + ones that come earlier

puts "\n Check disks in StorEdge 6120  ..... \n"

# variables
set user root
set passwd root-passwd
set cmd "fru stat"
set tmpfile /var/tmp/expect_result

# start process
spawn telnet sds9
# provide username
expect "Login:"
send "${user}\r"
# provide password
expect "Password:"
send "${passwd}\r"
# run command
expect "sds9*"
send "${cmd}\r"
# use regexp for expecting string
expect -re "DISK.*LOOP"
#puts "Only matced charactes \n"
set result $expect_out(0,string)

# put result to a file, tmpfile_id is file ID
set tmpfile_id [open $tmpfile w]
puts $tmpfile_id $result
close $tmpfile_id

if [file exists "$tmpfile"] {system cat $tmpfile | grep -v LOOP}
And the script output is like :
{host}/var/tmp> expect disk_6120.exp

 Check disks in StorEdge 6120  .....

DISK    STATUS   STATE       ROLE        PORT1      PORT2      TEMP  VOLUME
------  -------  ----------  ----------  ---------  ---------  ----  ------
u1d01   ready    enabled     data disk   ready      ready      29    vol9-1
u1d02   ready    enabled     data disk   ready      ready      29    vol9-1
u1d03   ready    enabled     data disk   ready      ready      30    vol9-1
u1d04   ready    enabled     data disk   ready      ready      27    vol9-1
u1d05   ready    enabled     data disk   ready      ready      29    vol9-1
u1d06   ready    enabled     data disk   ready      ready      29    vol9-1
u1d07   ready    enabled     standby     ready      ready      30    vol9-1
u1d08   ready    enabled     data disk   ready      ready      29    vol9-2
u1d09   ready    enabled     data disk   ready      ready      30    vol9-2
u1d10   ready    enabled     data disk   ready      ready      29    vol9-2
u1d11   ready    enabled     data disk   ready      ready      30    vol9-2
u1d12   ready    enabled     data disk   ready      ready      29    vol9-2
u1d13   ready    enabled     data disk   ready      ready      28    vol9-2
u1d14   ready    enabled     standby     ready      ready      31    vol9-2
u2d01   ready    enabled     data disk   ready      ready      29    vol10-1
u2d02   ready    enabled     data disk   ready      ready      28    vol10-1
u2d03   ready    enabled     data disk   ready      ready      31    vol10-1
u2d04   ready    enabled     data disk   ready      ready      30    vol10-1
u2d05   ready    enabled     data disk   ready      ready      29    vol10-1
u2d06   ready    enabled     data disk   ready      ready      28    vol10-1
u2d07   ready    enabled     unassigned  ready      ready      25    -
u2d08   ready    enabled     data disk   ready      ready      26    vol10-2
u2d09   ready    enabled     data disk   ready      ready      24    vol10-2
u2d10   ready    enabled     data disk   ready      ready      22    vol10-2
u2d11   ready    enabled     data disk   ready      ready      22    vol10-2
u2d12   ready    enabled     data disk   ready      ready      23    vol10-2
u2d13   ready    enabled     data disk   ready      ready      25    vol10-2
u2d14   ready    enabled     standby     ready      ready      23    vol10-2

Example 2

Here we check access to serial console of HP Integrity server, it must allow SSH.

#!/usr/bin/expect
# debugging: 0=no 1=yes
#exp_internal 1
exp_internal 0
# spawn process output: 0=no 1=yes
log_user 0

# some help
# $expect_out(0,string) keeps only matched characters
# $expect_out(buffer) keeps matched characters + ones that come earlier

# input arguments
set host [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]

# check for arguments
if {[llength $argv] != 3 } {
        puts "\n Usage: scriptname host user password \n"
        puts "\n Example: concheck-hpux.exp g7u1111 g7u1111-c xxxxxxx \n"
        puts "Note: the script works only if console allows SSH"
        exit 1
}

puts "\n ############ Serial console check for $host ########### \n"

# temp files
set tmpfileilo /tmp/ilo-output.txt
set tmpfile /tmp/console-output.txt

# start process
#spawn telnet ${user}@${host}-c
spawn ssh -o StrictHostKeyChecking=no ${user}@${host}-c
expect "password:"
send "${passwd}\r"
expect "hpiLO->"

# get ilo output
set result_ilo $expect_out(buffer)
set tmpfileilo_id [open $tmpfileilo w]
puts $tmpfileilo_id $result_ilo
close $tmpfileilo_id
if [file exists "$tmpfileilo"] {system cat $tmpfileilo}
if [file exists "$tmpfileilo"] {system rm $tmpfileilo}

# go to Console
send "co\r"
expect "Console Login"

# get console output
set result_con $expect_out(buffer)
set tmpfile_id [open $tmpfile w]
puts $tmpfile_id $result_con
close $tmpfile_id
if [file exists "$tmpfile"] {system cat $tmpfile}
if [file exists "$tmpfile"] {system rm $tmpfile}

exit 0
Result looks like:
{host}/tmp>  expect concheck-hpux.exp 

 Usage: scriptname host user password
 Example: concheck-hpux.exp g7u1111 g7u1111-c xxxxxxx
Note: the script works only if console allows SSH

{host}/tmp> expect concheck-hpux.exp g7u1111 g7u1111-c con_passwd

############ Serial console check for g7u1111 ###########

         Hewlett-Packard Integrated Lights-Out 3 for Integrity
  (C) Copyright 1999-2013 Hewlett-Packard Development Company, L.P.
                    MP Host Name: g7u1111
                 iLO MP Firmware Revision 01.55.02

   MP MAIN MENU:

         CO: Console
        VFP: Virtual Front Panel
         CM: Command Menu
         CL: Console Log
         SL: Show Event Logs
         HE: Main Help Menu
          X: Exit Connection

[g7u1111] hpiLO->
 co

        [Use Ctrl-B or ESC-( to return to MP main menu.]

- - - - - - - - - - Prior Console Output - - - - - - - - - -

Console Login
Back to the main page