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
 
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
 
save output into file

Posted by astreet (astreet), 2 February 2007
Hi,

i want to send a command to remote machine and save the output of the command to local. how can i do that with expect script?

my script as below
----------------------------------------------------------
set output [open "outputfile.txt" "w"]
expect -re "(\\\$ |# )" { send "ls -ltr /tmp\r" }
               expect -re "\r\n(.*)\r\n"
               set outcome $expect_out(0,string)
               puts $output $outcome
close $output
------------------------------------------------------------

by right, the ouput for ls -ltr /tmp is 30 lines
but the outputfile.txt only capture up to 15 lines.

Something wrong with script?
grateful for reply.

Posted by admin (Graham Ellis), 2 February 2007
Your expect string is woolly - it's matched in the middle of the response so you've only got part of your response into the file.   You need to look for something to identify the end of the ls report

Posted by astreet (astreet), 2 February 2007
yup...
may i know what should i do with my script?
or you can provide a simple script which serve the same purpose.

Posted by admin (Graham Ellis), 3 February 2007
The "problem" with expect is it sits on top of everything esle ... so that any example I provide would be OS and shell specific and might not work for you. What you need to do is to run ls in the way that expect does, then look for an identifying end string.   Actually, I think your clue may be in

expect -re "(\\\$ |# )"

as that's the prompt string ... why not simply wait for that a second time rather than waiting for

expect -re "\r\n(.*)\r\n"

?

Posted by astreet (astreet), 5 February 2007
i just can capture # if i wait for
expect -re "(\\\$ |# )"

I am able to capture all the output if i code like below
------------------------------------------------------------------
set output [open "outputfile.txt" "w"]
     expect -re "(\\\$ |# )" { send "ls -ltr /tmp\r" }
     expect -re "\r\n(.*)\r\n"
     set outcome $expect_out(0,string)
     expect -re "\r\n(.*)\r\n"
     set outcome $expect_out(0,string)
     expect -re "\r\n(.*)\r\n"
     set outcome $expect_out(0,string)
     expect -re "\r\n(.*)\r\n"
     set outcome $expect_out(0,string)
     puts $output $outcome
close $output
----------------------------------------------------------------

how come i need to expect so many time then only can capture all output??

Posted by admin (Graham Ellis), 6 February 2007
on 02/05/07 at 06:04:32, astreet wrote:
how come i need to expect so many time then only can capture all output??


Because the string you are matching occurs many times and so you're only matching on the input stream as far as it's been generated to each point. Please - look for a unique sequence at the end ad recode to look for that. If my suggestion made previously didn't work, look for an alternative that does.   It will depend on what shell you're running and how you have your environment variables, such as your prompt, set.


Posted by astreet (astreet), 6 February 2007
Finally my script can work for what i want.
Thanks for the clue from raham Ellis. ^^

----------------------------------------------------------
set output [open "outputfile.txt" "w"]
expect -re "(\\\$ |# )" { send "ls -ltr /tmp\r" }
     expect -re "\r\n(.*)\r(.*)(\\\$ |# )"
     set outcome $expect_out(1,string)
     send "\r"
     puts $output $outcome
close $output
------------------------------------------------------------

----------------------------
normal unix command
----------------------------
server $ ls -ltr
output
.
.
.
.
output
server $

----------------------------
expect script
----------------------------
server $ ls -ltr\r
\n
output(.*)
.
.
.
.
output\r
server(.*) $



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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho