Trying to parse expect_out(buffer)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to parse expect_out(buffer)
# 1  
Old 10-31-2014
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:

Code:
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:

Code:
#!/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.
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for code.

Last edited by Don Cragun; 10-31-2014 at 07:45 PM.. Reason: Add CODE tags.
# 2  
Old 10-31-2014
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"}
# 3  
Old 11-02-2014
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

Code:
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).
# 4  
Old 11-02-2014
Try this:

Code:
#!/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

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 11-03-2014
EXCELLENT!! WORKS GREAT!!
Thank you, Chubler XL!!SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Jumbled output in expect_out(buffer)

I have a code like this : set ipv6_acl_max_chars test_acl_max_chars123456a789%s%d2345678ww134rt789qa23456789012345%c89012%a56789012x4r67890test_acl_max_chars1234567890.01234aabcdob34567aBC0 spawn telnet $myip expect "Login:" { send "admin\r" } expect "Password:" {send "admin\r" }... (0 Replies)
Discussion started by: ylucki
0 Replies

2. Programming

Flushing expect_out(buffer)

Can some one tell me how to flush expect_out(buffer)? below is my code expect -re {.*} {} expect "swpackages>*" send -i $con "trial.bat \r" set outcome $expect_out(buffer) expect "*continue*" set prevreport $expect_out(buffer) send "\r \r"; problem is :- I am getting "pre" stuffs... (0 Replies)
Discussion started by: cityprince143
0 Replies

3. Shell Programming and Scripting

Expect_out help!!!

I am trying to read a file via SSH connect and store it to expect_out(buffer). I am a virgin to expect. Help is really appreciated. Wasted almost a day :-( Code is as follows expect "system32>" send "type output.csv"; send "\r"; expect "system32>" set outcome $expect_out(buffer)... (2 Replies)
Discussion started by: cityprince143
2 Replies

4. Shell Programming and Scripting

Expect_out(buffer) works but it doesn't get all lines

Hello "expect" experts I am new at Expect. I have searched for a little while how to capture multiple lines with Expect and I am almost succeeded on that but I don't get all the lines of a command's output that the script executes on a server. Here is how my script works in a nutshell - ... (6 Replies)
Discussion started by: capacho6666
6 Replies

5. Shell Programming and Scripting

Parsing expect_out using regex in expect script

Hi, I am trying to write an expect script. Being a newbie in expect, maybee this is a silly doubt but i am stuck here. So essentially , i want the o/p of one router command to be captured . Its something like this Stats Input Rx : 1234 Input Bytes : 3456 My expect script looks ... (5 Replies)
Discussion started by: ashy_g
5 Replies

6. Programming

[SOLVED] Flushing expect_out(buffer) inside a loop

Greetings, Having an issue with the expect_out(buffer). in a foreach loop through some switches I am grabbing some arp table information and writing it out to output files (1 each for each switch looped through). The first iteration works fine. the second iteration of the loop writes the... (0 Replies)
Discussion started by: SuperSix4
0 Replies

7. Shell Programming and Scripting

Problems with expect_out command

expect "#" send "terminal-length 0\r" expect "#" send "show mp cpu\r" expect "#" send "show mp memory\r" expect "#" while {1} { expect "#" send "clear counters\r" sleep 30 ... (0 Replies)
Discussion started by: roh_20022002
0 Replies

8. Shell Programming and Scripting

expect_out buffer no such variable running script background

I am trying to use send and receive using expect. the expect_out(buffer) is working fine while it is running it as foreground. But the same script when it is ran as background, the expect_out(buffer) errored out. Is there any factor influence when we run script in foreground and in background? ... (0 Replies)
Discussion started by: shellscripter
0 Replies

9. Shell Programming and Scripting

expect_out(buffer) empty

I have only some info into my buffer, but after a rssi command I see the folowing lines expected into buffer but not present : rssi=-106 rssi=-109 I see in my buffer only the first part of the output, here you are a part of script : #!/usr/bin/expect -f #global expect_out match_max 10000000... (1 Reply)
Discussion started by: ugobale
1 Replies

10. Shell Programming and Scripting

Help with expect expect_out

This is my simple expect scritpt: #!/usr/bin/expect -f match_max 100000 set timeout -1 spawn telnet $IP expect "#" send -- "shell\r" expect "*Ready*" send -- "init\r" expect "*Ready*" send -- "readsensor \r" expect -- "*" <<< Output of this is a 2 digit number set val... (5 Replies)
Discussion started by: expect_user
5 Replies
Login or Register to Ask a Question