Training, Open Source computer languages

This is page http://www.wellho.net/forum/The-Tcl- ... guage/expect-reading-in-a-list.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
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))
expect reading in a list????

Posted by Dr._Byte (Dr._Byte), 18 November 2004
Help, I know that I cant do this, I have in the past but I cant for the life of me remember how and I know this should be trivial..... But I have a script that I need to take input from a file... i.e. I want to have a file with a list of ip's and have the expect script run against each of the ip's in the list. Can I do this in expect or am I going to have to write  aperl wrapper for it?

Thanks

William

Posted by admin (Graham Ellis), 18 November 2004
Here's some expect code to open a file, read line by line, extract information ....

Code:
set fhandle [open dnsdata r]

while {[gets $fhandle inline] >= 0} {
       switch -glob -- $inline {
       Name:*  {
               set name [lindex $inline 1]
               }
       Address:* {
               set address($name) [lindex $inline 1]
               }
       }
}
close $fhandle



all you need to do is alter the switch to something more appropriate  drop your expect code in the loop. No need to use any Perl, great language though it is  

Posted by Dr._Byte (Dr._Byte), 18 November 2004
Thanks for the information... I will give that a try... in the mean time I wrote a ksh script to call the expect script. If you are interested in the code for that I can post it here... Thanks again

William

Posted by admin (Graham Ellis), 18 November 2004
Yep, do post (the ksh script) .. thanks!

Posted by Dr._Byte (Dr._Byte), 18 November 2004
No problem at all.... here you go... very simple script I will comment it as well...

#!/bin/ksh
#useage testscript hostfile
cat $1 | while read host
do
./test.exp $host
done

useage is testscript.sh filecontainingIPaddresses   the script reads in the file that was put on the command line cats it and increments by 1 at each pass and the do then executes the expect script test.exp $host where $host is the IP address it read on each line of the hostfile. the expect script in this case just is a typical login shell script...

this is the test.exp file  

#!/usr/local/bin/expect -f
# Useage login site
if { $argc < 1 } {
       send_user "Usage: fdl site \n"
       exit
}


set site [lindex $argv 0]
set pass "testpass"


       spawn telnet $site
               expect {
                       {*Name  :*} {send "name\r"; expect {*assword:*}; send {pass}\r; expect {*>*}
            }
}
exit
               expect {
                       {*Name  :*} {send_user "ACCESS DENIED Invalid password";
exit}
}
exit


Very simple this will walk through the ip addresses you have set in the host file and attempt to telnet in to them... Using the username and password you assigned. What I was goign to use this for was to verify if I have changed my passwords to the systems I have access to. This can easily be adapted to log the results to a file or using expect go in and change the ones that still have the "old" password.


Thanks

William



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.

© 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