Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Writing capture output to a file

Posted by aliriazi (Al), 15 December 2007
Hi,
I am new to this language and found  a lots of useful informations here at this forum.
I am trying to write the output from running some commands to a router into a file. I am connecting to router using the following code:

spawn telnet xx.xx.xx.xx
set telnet $spawn_id
expect -nocase "password: "

and then
using the following code to read commands from a file and sending to the router

if {[catch {set fd [open "unconfig_switch_topology01.tcl"]} errmsg]} {
     error "Unable to open file 'unconfig_switch_topology01.tcl' for reading\n $errmsg"
}

while {[gets $fd command] != -1} {
                 exp_send "$command\r"
     }

now I like to be able to send the result from the screen to a file for debugging. I have read few posting in this forum and none were close to my case. I know that I am missing something simple here however I am not sure what!!!

Thanks in advance for all the help.

Al

Posted by admin (Graham Ellis), 15 December 2007
Hi, Al

Are you using the expect command at all?  With a script such as this, I would expect (pun not intended, but hard to avoid) to see that command used to say that your programs should pause up until the router has responded to each command  you send in in turn, rather than relying on buffers.

Once you're expecting the responses, you'll find that the data that's got back is in a variable called expect_out(buffer) which you can then puts to a file opened by the open command.

This is the sort of thing:

Code:
#!/usr/bin/expect

log_user 0
set running 1
spawn telnet www.wellho.net 80
expect  {'^]'.}

send "GET /index.html HTTP/1.0\n\n"

set fh [open capture.txt w]

while  $running {
       expect {
               "\n" { puts $fh "$expect_out(buffer)"}
               eof {set running 0}
               timeout {set running 0}
               }
       }

close $fh
puts done


Posted by aliriazi (Al), 17 December 2007
Hi Graham ,
Thanks a lot for your help. it did help me to save the buffer into a file however, before I used to get to prompt back once the commands were executed and reached the end of the file , now I gets stuck @
Metro_10G_2#
At the router prompt. The way i know is that I change the value of log_user to 1 instead of 0. My code look like:

if {[catch {set fd [open "unconfig_router_topology01.tcl"]} errmsg]} {
     error "Unable to open file 'unconfig_switch_topology01.tcl' for reading\n $errmsg"
}
while {[gets $fd command] != -1} {

                 exp_send "$command\r"
     }
close $fd

set fh [open capture.txt w]

while  $running {
       expect {
               "\n" { puts $fh "$expect_out(buffer)"}
               eof {set running 0}
               timeout {set running 0}
               }
       }

close $fh
puts done


expect -re {# $}


exit


Thanks in advance,

Ali

Posted by admin (Graham Ellis), 17 December 2007
If you additionally expect the router prompt, then it will also come back to you when you receive that.  Along the lines of ...

Code:
while  $running {
  expect {
     "\n" { puts $fh "$expect_out(buffer)"}
     "Metro_10G_2#" {set running 0}
     eof {set running 0}
     timeout {set running 0}
     }
  }


Posted by aliriazi (Al), 17 December 2007
Hi Graham ,
Thanks for the reply. What if I don't want to see the router prompt.
Metro_10G_2#

Before adding the code that you suggested:
while  $running {  
  expect {  
"\n" { puts $fh "$expect_out(buffer)"}  
 eof {set running 0}  
timeout {set running 0}  
}
 
  }  

I used to see the prompt like:
Metro_10G_2(config-if)#no switchport
Metro_10G_2(config-if)#shut
Metro_10G_2(config-if)#exit
Metro_10G_2(config)#exit
Metro_10G_2#
Metro_10G_2#
C:\Tcl\bin>

However, Now the courser  stops at
Metro_10G_2#_

I added the line that you mentioned ("Metro_10G_2#" {set running 0}. I believe that is if I want to see the router prompt. right?
I tried to add the 'exit' at the end of the file that I am parsing from which include all the configuration commands

and the result was:
Metro_10G_2(config-if)#no
Metro_10G_2(config-if)#sh
Metro_10G_2(config-if)#en
Metro_10G_2#exit


Connection to host lost.

I still don't see the "C:\Tcl\bin>"

Any suggestion?

BTW: I am using TCL for Windows XP. I am not sure if that has any effect on the result.

Thanks,

Al.


Posted by admin (Graham Ellis), 18 December 2007
on 12/17/07 at 21:46:25, Al wrote:
I still don't see the "C:\Tcl\bin>"

Any suggestion?




Without wishing to appear cheeky or twee in my answer, I would suggest that you work it our from first principles - what you send, what you expect back - and choreograph it like that.   Much as I would like to do the job for you, if I answer one question in a specific way all I'll do is raise another one in your mind and we'll go on for days - but if you can take it back to basics I'm sure it will come easily.    If you feel it would be worth popping by one evening over Christmas and going through it with me, happy to do that for free .... and we have accommodation available at the hotel if you wanted to stop overnight - though I would have to charge for that.


Posted by aliriazi (Al), 19 December 2007
Thanks Graham,

Posted by dharam (dharam), 6 February 2008
Iam unable to get the message from device console to my Tcl shell after executing the command
device# show device
device#show port config

Please do suggest me how to go ahead with this CLI automation testing,
Below is the script what is created.What is to be added and edited in the follwing script to get my need done


#!/usr/bin/expect
                                                                               
package require Expect
                                                                               
spawn telnet 192.168.6.183
set telnet $spawn_id
expect "device login:"
exp_send  "admin\r"
expect "password:"
exp_send "breci\r"
                                                                               
expect "Login incorrect"
expect " login:"
exp_send "admi\r"
expect "password:"
exp_send "breci\r"
expect "Login incorrect"
                                                                               
expect " login:"
exp_send "admin\r"
expect "password:"
exp_send "brecis\r"
interact


Posted by admin (Graham Ellis), 6 February 2008
Why are you intentionally sending the wrong password twice?

Posted by dharam (dharam), 7 February 2008
to check different possiblities of device not accepting  login name and passwords other than the correct password.

Posted by admin (Graham Ellis), 7 February 2008
on 02/06/08 at 14:37:36, dharam wrote:
Iam unable to get the message from device console to my Tcl shell after executing the command
device# show device
device#show port config

Please do suggest me how to go ahead with this CLI automation testing,


Replace the interact with further expectand send commands??   This answer seems too obvious so I don't think I have understood the question, though


Posted by dharam (dharam), 7 February 2008
Graham,

As u told i removed interact command from the script and continued with executing the show device, show arl /...... command from the script using spawn command, but now the problem iam facing is how to capture the display content of "#show device<enter>", "#show arl all<enter> " from the device console to my tcl script, to go ahead with testing the information displaying in the console after executing the CLI commands is correct or not.

Can u please do tell

Thanks in advance

#!/usr/bin/expect
                                                                               
package require Expect
                                                                               
spawn telnet 192.168.6.183
set telnet $spawn_id
expect "mogra login:"
exp_send  "admin\r"
expect "password:"
exp_send "breci\r"
#expect "Iris#"
expect "Login incorrect"
expect "mogra login:"
exp_send "admi\r"
expect "password:"
exp_send "breci\r"
expect "Login incorrect"
                                                                               
expect "mogra login:"
exp_send "admin\r"
expect "password:"
exp_send "brecis\r"

#NOW TRYING TO CAPTURE AND TEST THE #show device, #show arl #all, #show port ..... AND SO ON COMMANDS EXECUTION FROM
#THE DEVICE CONSOLE

spawn show device
set device $spawn_id

spawn show arl all
set arl $spawn_id

Posted by admin (Graham Ellis), 8 February 2008
I don't think you need to spawn separate processes - your "shows" want to carry on within the same ssh session, and also in series (not in parallel) so you want to send and expect (as per my previous post) and not spawn some more.

Posted by dharam (dharam), 8 February 2008
From the below snippet of the code iam expecting some content to be written to the file "capture.txt" but nothing is written and also please explain me what while loop is doing other than putting content of expect_out(buffer) to filehandler2, i mean i dint get what  "eof" and  "timeout" is doing in while loop.

Please tell asap,

log_user 1
set filehandler [open capture.txt w+]

#AFTER LOGIN TO LAYER TWO DEVICE
expect "#"
exp_send "show arl all\r"
while 1 {
       expect {
               "\n" {puts $filehandler2 "$expect_out(buffer)"}
               eof {set running 0}
               timeout {set running 0}
              }
       }


Posted by dharam (dharam), 8 February 2008
Graham,
Thanks .
I understood, its write within same telnet session i have to send command and expect output for that, But now after continuing iam unable to get the capture content in a file after executing command like "show device" and "show arl all" etc, please tell me the solution,,,


From the below snippet of the code iam expecting some content to be written to the file "capture.txt" but nothing is written and also please explain me what while loop is doing other than putting content of expect_out(buffer) to filehandler2, i mean i dint get what  "eof" and  "timeout" is doing in while loop.

Please tell asap,

log_user 1
set filehandler [open capture.txt w+]

#AFTER LOGIN TO LAYER TWO DEVICE
expect "#"
exp_send "show arl all\r"
while 1 {
  expect {
     "\n" {puts $filehandler2 "$expect_out(buffer)"}
     eof {set running 0}
     timeout {set running 0}
    }
  }

Posted by admin (Graham Ellis), 9 February 2008
Your sample code shows a file being opened on a file handle called filehandler but then you're writing to a different one - filehandler2.  You need to have the same name.

The setting of running is probably to do with exiting the loop - I would have expected to see the 1 in the conditional to read {$running} and to have an extra set running 1 before your loop ... but I think you have posted half-edited code with some redundant code (and an infinite loop!) left in.




Posted by dharam (dharam), 12 February 2008
Below is the  script  which iam using for CLI automation of network device from telnet session. I this as i told earlier iam facing problem of not able to write the device information after executing the commands "show device", "show arl all" like so. Am i doing anything wrong in dis or else wat to do for the script to continue with cli automating of device commands.

Please make to continue in this regards,

#!/usr/bin/expect
                                                                               
package require Expect
                                                                               
log_user 1
set filehandler1 [open capture1.txt a+]
set filehandler2 [open capture2.txt a+]
#set running 1
                                                                               
spawn telnet 192.168.6.142
set telnet $spawn_id
expect "device login:"
exp_send  "admin\r"
expect "password:"
exp_send "xxxxxx\r"

#set timeout 30

expect "#"
exp_send "show device\r"
while  1 {
       expect {
               "\n" { puts $filehandler1 "$expect_out(buffer)"}
               eof {set running 1}
               timeout {set running 1}
               }
        }
close $filehandler1
set timeout 30

expect "#"
exp_send "show mgmt config\r"
while 1 {
       expect {
               "\n" {puts $filehandler2 "$expect_out(buffer)"}
               eof {set running 1}
               timeout {set running 1}  
 }
       }
close $filehandler2
                                                                               



Posted by dharam (dharam), 12 February 2008
Hi Graham,

The problem iam facing in continuing with scripting is how t write the contents of a expect_out(buffer) to a file,

as u told i removed the infinite loop(while 1 { }) also. The code i have done i have sent u in previous post to u.

Please tell me how to go about with this..

Posted by dharam (dharam), 12 February 2008
Graham,

Below is the program, which has been edited a bit compare to previous programs, But in this information gets print out in running script after executing the commands like "show arl all", "show version",  "show snmp community" etc... But still not possible to extract the contents to respective files after execution o f command. What is happening is some junk values like expect_out(buffer) is there in some files and some files are fully blank,

So now please guide me how to extract the information from expect_out(buffer) to the respective files...
----------------PROGRAM---------------------------------------------------------

#!/usr/bin/expect
                                                                                         
package require Expect
                                                                                         
log_user 1

set filehandler1 [open capture1.txt a+]
set filehandler2 [open capture2.txt a+]
set filehandler3 [open capture3.txt a+]
set filehandler4 [open capture4.txt a+]
set filehandler5 [open capture5.txt a+]
set filehandler6 [open capture6.txt a+]
                                                                                         
spawn telnet 192.168.6.184
set telnet $spawn_id

expect "device login:"
exp_send  "admin\r"
expect "password:"
exp_send "brecis\r"
                                                                                         
expect "#"
exp_send "show arl all\r"
                                                                                         
expect {
               puts {$filehandler1 $expect_out(buffer)}
               
           }
close $filehandler1

expect "#"
exp_send "show vlan\r"
expect {
               puts $filehandler2 $expect_out(buffer)
          }
close $filehandler2

expect "#"
exp_send "show version\r"
expect {
               puts $filehandler3 expect_out(buffer)
          }
close $filehandler3
                                                                                         
expect "#"
exp_send "show group-port\r"
expect {
           puts $filehandler4 expect_out(buffer)
          }
close $filehandler4
                                                                                         
expect "#"
exp_send "show snmp community\r"
expect {
           puts $filehandler5 expect_out(buffer)
          }
close $filehandler5
                                                                                         
expect "#"
exp_send "show snmp community\r"
expect {
               puts $filehandler6 expect_out(buffer)
           }
close $filehandler6
                                                                                         


Posted by admin (Graham Ellis), 13 February 2008
Where you write expect_out(buffer) you get exactly that text written to the file, but where you write $expect_out(buffer) (same thing with a $) you get the contents of the variable of that name.  I think your problem is just that you have left a few $s off!

Posted by dharam (dharam), 13 February 2008
Graham,

Ya, i corrected the code with $ included wherever i missed it, but   now all respective files for storing executed command details are still empty, not getting . Please tell how to get the contents of expect_out(buffer) written to a file



#!/usr/bin/expect
                                                                               
package require Expect
                                                                               
log_user 1
set filehandler1 [open capture1.txt a+]
set filehandler2 [open capture2.txt a+]
set filehandler3 [open capture3.txt a+]
set filehandler4 [open capture4.txt a+]
set filehandler5 [open capture5.txt a+]
set filehandler6 [open capture6.txt a+]
#set running 1
                                                                               
spawn telnet 192.168.6.184
set telnet $spawn_id
expect "device login:"
exp_send  "admin\r"
expect "password:"
exp_send "******\r"
                                                                               
expect "#"
exp_send "show arl all\r"
expect {
               puts $filehandler1 $expect_out(buffer)
               
            }
close $filehandler1

expect "#"
exp_send "show ntp configuration\r"
       expect {
               puts $filehandler2 $expect_out(buffer)
                  }
close $filehandler2

expect "#"
exp_send "show version\r"
       expect {
               puts $filehandler3 $expect_out(buffer)
                  }
close $filehandler3
                                                                               
expect "#"
exp_send "show group-port\r"
       expect {
               puts $filehandler4 $expect_out(buffer)
                  }
close $filehandler4
                                                                               
expect "#"
exp_send "show snmp community\r"
       expect {
               puts $filehandler5 $expect_out(buffer)
                  }
close $filehandler5

expect "#"
exp_send "show snmp community\r"
       expect {
               puts $filehandler6 $expect_out(buffer)
                  }
close $filehandler6
                                                                               



Posted by dharam (dharam), 13 February 2008
Graham,

Please provide me the solution for how to get the expect_out(buffer) content written in the file.
The scripting i have done is sent to u in the previous post.

Please try to give solution for dis problem asap.

Posted by admin (Graham Ellis), 13 February 2008
It looks to me as if you are already writing the variable correctly to the file ... but it is empty.   Your expect statements are not wating for anything.

I note your request for help "asap".   I can reassure you that I do not intentionally delay answers on the forum - posts are answered "asap" even without a prompt.   However, I am busy and must prioritise customers over people who are enjoying free support.

Posted by dharam (dharam), 14 February 2008
Graham,
Once i send the commands using "exp_send" or "send" like for example:
send "show snmp community\r"

I agree that detail information of executed command;
show  snmp community    ,,, will be in expect_out(buffer)

But iam not getting how to continue after for writing expect_out(buffer) content into a file and also what to wait for from the network device after executing this command on the device,

so i did with expect {  
                                puts $filehandler3 $expect_out(buffer)
                                }

Please tell how to continue with this writing of expect_out(buffer) content to a file.

Iam Sorry for stressing you for the solution.



Posted by admin (Graham Ellis), 14 February 2008
You've not stressed me, Dharam, but I do feel that you're not going to get an effective anser to all your questions via the forum medium - I've written an explanation which is ueseful to other too - so I've put in on my blog here.   If you come to this post once it's gone from the main blog it is also copies here.

And the course I'm suggesting is this one

Posted by dharam (dharam), 18 February 2008
Graham,

Rite now iam doing my academic project, so in  this regards  i need to complete my project by the end of march, and iam doing my academic stuffs in India,
dis is my uncomfortabilty in attending the course...

Posted by admin (Graham Ellis), 18 February 2008
If you're asking the questions as part of your study, you'll do far better to ask your tutor / mentor what he / she has in mind.   I have to be reluctant to help students on academic programs, as I am liable to suggest practical solutions which don't actually fit with the course and tutor's teaching objectives.

Posted by dharam (dharam), 19 February 2008
Graham,

Iam doing dis CLI Automation on Layer-2 device, Switch in the industry Which iam doing as a company work and the same work i will be showing as my academic project. Its not the teacher/mentor is coming into picture to restrict me from going ahead with this work.

in that regards, u can help me it wont be a problem



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho