Example Expect Script

   T


he F5 ARX has many command line options and is very configurable.
However there are times we need to get a little fancy and automate some tasks or get something done that is creative. For example we have about 8gb for the partition that holds reports. For 90% of our customers this is fine, however we have a few customer who would like to save those reports off automatically to save space. This tip is going to walk you through how to install an “Expect script” to help you with that.

 

The ARX uses Expect which is a TCL extension which allows for fully interactive automated tasks. Expect is built on TCL but does not "require" TCL in order to run (as it's build into the library of expect). If you want to better understand the power of expect as a script system, you can read more about TCL for basic loop constructs, syntax, error handling. However, expect is based on TCL so if you read a lightweight book on expect you'll learn the nature of TCL as well. Find the manual for expect hosted at http://www.tcl.tk/man/expect5.31/

F5 Professional Services can assist you in creating customized scripts for your ARX solution and we ask customers to please reach out to us before trying scripting as this could destabilize your environment if something goes wrong. Please do not hesitate to reach out to your F5 account manager to talk about how we can help. The sample script is not supported by F5 and is only given as an example.


Copying an Expect script to the ARX

To copy an Expect script to the ARX you can use a variety of methods: 

FTP

     copy ftp://[user[:password]@]server/source‐file dir file

  “copy ftp://user:password@111.222.333.444/upload_reports.exp scripts upload_reports.exp”

SCP

     copy scp://[user@]server:source‐file directory file [accept‐host‐key]

  “copy scp://user@111.222.333.444:upload_reports.exp scripts upload_reports.exp”

      CIFS/NFS ( requires an an operational Namespace/Volume/Share )

     copy {nfs|cifs} namespace vol‐path source‐file directory file

  “copy cifs ns-namespace /volume/ upload_reports.exp scripts upload_reports.exp”


Example Expect Script upload_reports.exp

Just copy and paste this into notepad and save it as upload_reports.exp then use the above methods to get the script on your ARX. To execute the script at the command like type:

ARXSWITCH# expect upload_reports.exp
USAGE: expect upload_reports.exp <location=
ftp://host/path> [type=reportType] [output=reportOutput] [delete]
USAGE: you can specify only up to 3 parameters: expect upload_reports.exp <location=

ftp://host/path> [output=reportOutput] [delete]

USAGE: location can be: ftp,scp,smtp
USAGE: for available types : show report type ?
USAGE: outputs: rpt,xml,txt
USAGE: delete - will remove report after copy


Expect script upload reports to a remote location with the option of deleting them

################################# Start Copy #################################
#This script is not supported by F5 and is given only as an example.
#Please contact your local F5 Professional Services consultant before
#using this or any custom script.
#version 1.0
#type
#output
#delete

 

 

log_user 0
set timeout 6000
send "enable\n"
expect "#"

 

send "no term conf\n"
expect "#"

 

#get date
set mydate [ timestamp -format %Y%m%d ]

 

#get hostname
send "show hostname\n"
expect "#"
foreach line [split $expect_out(buffer) "\n"] {
                set myswitch [lindex $line 0]
                regsub -all "#" [lindex $line 0] "" myswitch
                continue
}

 


  set mylocation ""
  set mytype ""
  set myoutput rpt
  set mydelete ""
  set mytoken ""
  set mytoken2 ""

 

if { [lindex $argv 0] != "" } {
  regsub -all "=" [lindex $argv 0] " " tmp
  set mytoken ""
  set mytoken2 ""
  set mytoken [lindex $tmp 0]
  set mytoken2 [lindex $tmp 1]
  case $mytoken {
        location {
                set mylocation $mytoken2
        } type {
                set mytype "type $mytoken2"
        } output {
                set myoutput $mytoken2
        } delete {
                set mydelete "yes"
        }
}
}

 

if { [lindex $argv 1] != "" } {
  regsub -all "=" [lindex $argv 1] " " tmp
  set mytoken ""
  set mytoken2 ""
  set mytoken [lindex $tmp 0]
  set mytoken2 [lindex $tmp 1]
  case  $mytoken  {
        location {
                set mylocation $mytoken2
        } type {
                set mytype "type $mytoken2"
        } output {
                set myoutput $mytoken2
        } delete {
                set mydelete "yes"
        }
  }
}

 

if { [lindex $argv 2] != "" } {
  regsub -all "=" [lindex $argv 2] " " tmp
  set mytoken ""
  set mytoken2 ""
  set mytoken [lindex $tmp 0]
  set mytoken2 [lindex $tmp 1]
  case $mytoken {
        location {
                set mylocation $mytoken2
        } type {
                set mytype "type "$mytoken2
        } output {
                set myoutput $mytoken2
        } delete {
                set mydelete "yes"
        }
}
}

 

if { [lindex $argv 3] != "" } {
  regsub -all "=" [lindex $argv 3] " " tmp
  set mytoken ""
  set mytoken2 ""
  set mytoken [lindex $tmp 0]
  set mytoken2 [lindex $tmp 1]
  case $mytoken {
        location {
                set mylocation $mytoken2
        } type {
                set mytype "type $mytoken2"
        } output {
                set myoutput $mytoken2
        } delete {
                set mydelete "yes"
        }
}
}

if { $mylocation == "" } {
        puts "USAGE: expect upload_reports.exp \<location=
ftp://host/path> \[type=reportType\] \[output=reportOutput\] \[delete\]"
        puts "USAGE: you can specify only up to 3 parameters: expect upload_reports.exp \<location=

ftp://host/path> \[output=reportOutput\] \[delete\]\n"
        puts "USAGE: location can be: ftp,scp,smtp"
        puts "USAGE: for available types : show report type ?"
        puts "USAGE: outputs: rpt,xml,txt"
        puts "USAGE: delete - will remove report after copy"
        exit
}

send "show reports $mytype\n"
expect "#"
foreach line1 [split $expect_out(buffer) "\n"] {
        if { [llength $line1] == 10 } {
                set myreport [lindex $line1 0]
                set myreportout [string range $myreport 0 [expr [string length $myreport] -4]]
                send "copy reports $myreport $mylocation/$myreportout$myoutput\n"
                expect "#"
                if { $mydelete != "" } {
                        send "delete reports $myreport\n"
                        expect "#"
                }
        }
        continue
}
####End of Script

 

Published Feb 23, 2011
Version 1.0

Was this article helpful?

1 Comment

  • Great example and thanks for the tips. What I am trying to do is script the ARX to take snapshots via a windows host where I run another script which will validate some information on my netapp before issuing the ARX snap. I already have an expect script which I can upload to my arx and I think will work but is there any method to call the script from a windows host? If not, do you have other sugguestion?

     

     

    Thanks

     

    Michael