Trying to parse expect_out(buffer)

trying to telnet to a device, list the files, and delete them. I can get the script to telnet and log in OK, and even issue the command to list out the files. I can't figure out how to parse the expect_out(buffer) and extract the file name for use in the delete command.

the files list out like this:

dot15# show file contents config
file contents config
 number-of-files 13
 total-size      308200
FILENAME                            FOLDERNAME  SIZE   LAST MODIFICATION TIME
---------------------------------------------------------------------------------
startup-config.xml                  config      21042  Fri Oct 31 08:38:58 2014
startup-config.20141031.083858.xml  config      21262  Fri Oct 31 08:38:09 2014
startup-config.20141031.083809.xml  config      20652  Fri Oct 31 03:31:26 2014
startup-config.20141031.033126.xml  config      20466  Thu Oct 30 16:15:33 2014
startup-config.20141030.161533.xml  config      8895   Thu Oct 30 16:02:23 2014
startup-config.20141030.033124.xml  config      28211  Wed Oct 29 03:31:22 2014
startup-config.20141029.033122.xml  config      28211  Tue Oct 28 03:31:20 2014
startup-config.20141028.033120.xml  config      28211  Mon Oct 27 03:31:18 2014
startup-config.20141027.033118.xml  config      28211  Sun Oct 26 03:31:16 2014
startup-config.20141026.033116.xml  config      28211  Sat Oct 25 03:31:14 2014
startup-config.20141025.033114.xml  config      28211  Fri Oct 24 14:51:18 2014
running-config.restore.xml          config      28418  Tue Oct 21 15:56:39 2014
startup-config_dhcp_520.xml         config      18199  Thu Jul 31 12:51:53 2014

dot15# exit

I need to include the xml file name in the command to delete the file. Additionally, I need to issue a discrete command to delete the files one-at-a-time (the device does not support wild cards - can simply send delete *.xml)

my script thus far:

#!/usr/bin/expect -f
set ip "10.199.10."
set arg1 [lindex $argv 0]
append ip $arg1

#
# telnet to E5@ ip=arg1
spawn telnet $ip
set timeout 5
#login
expect "login:" { send "sysadmin\n" }
expect "word:" { send "sysadmin\n" }
set timeout 2
#
# list the config files
expect "#" {send "show file contents config\n"}
#
# delete files

(this is the missing puzzle piece)

send "exit\n"
expect EOF

I pass the last octet of the IP in as an argument as I run the script

thanks, in advance.

The output suggests that those xml files are located in the config folder.

Do you know the exact location of that config folder?

Are the following commands available? grep , cut , xargs and rm

Will the UNIX pipelines work? Test it like so:

expect "#" {send "show file contents config | grep xml\n"}

This is a proprietary front-end user interface riding on (what I believe to be) a Linux shell and, as such, the UI prevents access to the Linux command set.

The eventual command that I want to "send" is

send "delete file config $configfile"

There can be anywhere from one to ten files stored in the directory, so I need to parse the expect_out(buffer) for each file name, load that name into the variable configfile and then execute the above command, the repeat for as many files that are listed (I imagine using something like a foreach statement).

Try this:

#!/usr/bin/expect -f
set ip "10.199.10."
set arg1 [lindex $argv 0]
append ip $arg1

#
# telnet to E5@ ip=arg1
spawn telnet $ip
set timeout 5
#login
expect "login:" { send "sysadmin\n" }
expect "word:" { send "sysadmin\n" }
#
# list the config files
expect "#" { send "show file contents config\n" }

# extract result into outcome
expect {
    -re "\r\n------+\r\n(.*)#" { set outcome $expect_out(1,string) }
    timeout { puts "config list not found: timing out!!!\n"; }
}

# process each outcome line
foreach line [split $outcome "\n" ] {
   set found [regexp {^(\S+) +config .*} $line match configfile]
   if {$found == 1 } {
       send "delete file config $configfile\r\n"
       expect "#"
   }
}

send "exit\n"
expect EOF
1 Like

EXCELLENT!! WORKS GREAT!!
Thank you, Chubler XL!!:b::b::b: