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))
Reading arrays using EXPECT

Posted by ramkriz (ramkriz), 4 August 2008
I call a EXPECT script from my perl script with machine IP and a FIle. The script logins to the machine and exports the value. I have close to 10 machines and I have created 10 files and pass the corresponding files in command line.

I could like to do this with a sinle file. I will create a file as below

MAchine1
EXPORT VAR1=
EXPORT VER2=
EXPORT VAR3=
MAchine1

Machine2
EXPORT VAR1=
EXPORT VER2=
EXPORT VAR3=
MAchine2


Now I need to take the Info present between machine1 and store it an array and then pass it to expect script. I can take the values in an array but not sure how to use the values stored in array and use it in EXPECT script..
Any pointers will be helpful..

#!/bin/ksh
set klug { ${1+"$@"}
shift
shift
 exec /usr/bin/expect -f $0 ${1+"$@"}
}
set ip [lindex $argv 0 ]
set file [lindex $argv 1]

spawn ssh -o stricthostkeychecking=no root@$ip
expect -re "Password"
send "password\r"
set fp [open $file ]
    while {-1 != [gets $fp line]} {
        send  "$line\r"
    }
interact


Posted by admin (Graham Ellis), 6 August 2008
First suggestion - get the programs talking to each other first and then look to put the more detailed work in.   Your #! line is wrong - you appear to have an expect script being run (!) through the Korn shell interpretter which isn't going to work.

I don't see an array in your example ... where does that come in?  I do see you sending a series of commans to a remote machine which once you switch to expect from korn might work - although I would add in some expect commands to check the echo and ensure that you don't get buffer overflows.   Is this making sense to you?

Posted by ramkriz (ramkriz), 7 August 2008
Thanks Graham.In the code I have given i have not used the array. The ultimate aim is to take the Info present between the machine in the file(Machine name is passed as parameter and store it in an array). Now I have used seperate file for each machine.


I can read through the array and do what I need. The problem I face is not able to fetch what I need from file and store it an array..

If run the my script as "test machine1" It should fetch all the info present
between the keyword "machine1" from the file and store it an array. It could be great if you can help me in doing this..
File Format :

MAchine1
EXPORT VAR1=
EXPORT VER2=
EXPORT VAR3=
MAchine1

Machine2
EXPORT VAR1=
EXPORT VER2=
EXPORT VAR3=
MAchine2

Thanks


Posted by admin (Graham Ellis), 9 August 2008
As a first step, do you have the code already written to read the data from the file (but not yet store it into the array)?  If so, please post up that code and I'll be able to give you some specific help in how to put it into an array.   (If I try to help without having access to the code you have so far, my advise would be much more general and much less likely to be appropriate!)



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