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
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

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.

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