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))
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., 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