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))
help with redirectioning output

Posted by pmnf (pmnf), 22 July 2004
I ppl im new to expect and to this forum. this as you can see my first post of many
My problem is the folowing.
i want to connect to a remote machine via telenet run a script name extract.sh grab the output of the script and store it in a file.

In this moment i am doing the folowing
#!/usr/bin/expect -f
#
log_file -a MY_OUTPUT
set user xpto
set password xpto
set site remote.address.pt
set VAR 1
set T 0
spawn telnet $site
while {$VAR>0} {
#puts $VAR
     expect {
           "login:" {
                 set rcvd $expect_out(buffer)
                 send "$user\r"
                 }
           "$user's Password:" {
                 send "pulso.2\r"
                 send "\n"
                 send  "cd nagios ;"
                 set rcvd $expect_out(buffer)
                 expect {
                       "bash#" {
                             send "./extract.sh\r"
                                   }                  
                       }
                             }
           
           }      
if {$T>3} exit
incr T
}
exit

############
this is working but i think this isnt very good programing.
what i really want to do is to run the program like this
$ program.excpt >> AFILE &
"AFILE : contains the result of the extract.sh
Any ideas


Posted by admin (Graham Ellis), 23 July 2004
I think you want to

a) start with

log_user 0

to turn off echo to stdout of all the text that comes back before you run your ./extract.sh

b) add an extra expect after your

send ./extract.sh

to cause expect to wait for the next shell prompt, then when you have it add a

puts $expect_out(buffer)

to send your output to stdout ... which your >> will cause to be redirected onto the end of the file at runtime.  You may need / want to puts an extra newline, datestamp, or header too so that you can easily analyse your logfile later on.

Graham

P.S.  Test it without the log_user line at first when you add the extra code in - otherwise if you have any bugs that cause the carefully choreographed exchange to fail, you won't know where they are.  

Posted by pmnf (pmnf), 23 July 2004
Thanks working allready



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.

Comment: "Please describe how you did the redirection using a code ..."
Visitor Ranking 5.0 (5=excellent, 1=poor)

Comment by Sathya (published 2011-08-18)
Please describe how you did the redirection using a code snippet. [#3969]

You can Add a comment or ranking or edit your own comments

Average page ranking - 5.0

© 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