1753776 Members
7170 Online
108799 Solutions
New Discussion юеВ

mp test expect script

 
rleon
Regular Advisor

mp test expect script

This is just a script that tests the mp password. The next one will be one that changes them ...

But I am having problems with this one.

First I have a script that reads the hosts list.

#!/usr/bin/sh

for host in `cat hosts`
do
./expect2.sh $host
done


#!/usr/local/bin/expect -f

set pwd mypassword

spawn telnet [lindex $argv 0]

# username is passed as 1st arg, password as 2nd

expect "*login:"
send "mpadmin\r"
expect "*password:"
send "$pwd\r"

expect -re "(.*)>" { send "X\n" }
expect -re "(.*)login:" { send "failed $argv\n" }

expect eof



If someone can show me how to combine the scripts that would be great.

After I have this one down .. I will be working on one that changes the mp password.

Thanks
4 REPLIES 4
rleon
Regular Advisor

Re: mp test expect script

I also get the following error ..

expect: spawn id exp7 not open
while executing
"expect eof"
(file "./expect2.sh" line 17)


but when i take out the expect eof .. the script does not work right.

Thanks
James R. Ferguson
Acclaimed Contributor

Re: mp test expect script

Hi:

First, your shell script isn't going to give you what you want --- a simple hostname. Instead, every whitespace delimited field of every line is going to be seen in the '$host' variable.

You could do:

#!/usr/bin/sh
grep -E "^[0-9]" /etc/hosts|while read HOST X
do
./expect2.sh ${HOST}
done

...which would find only lines beginning with a digit which should represent valid hosts.

As for "combining" the shell and the 'expect' script, why?

You would be far better served if you leave the 'expect' script as a functional, standalone script that is called however you need to call it. In this case you call it passing every hostname in an '/etc/hosts' file.

Regards!

...JRF...
rleon
Regular Advisor

Re: mp test expect script

Thanks for your response mr james but I am not ready the /etc/hosts file .. it is just a regular file named hosts with a list of all the hostnames.

My main issue is I get the following error ..

expect: spawn id exp7 not open
while executing
"expect eof"
(file "./expect2.sh" line 17)


but when i take out the expect eof .. the script does not work right.
James R. Ferguson
Acclaimed Contributor

Re: mp test expect script

Hi (again):

You need the 'expect eof' to wait for the last command to finish.

As for combining the shell and 'expect' scripts into one, you could use a here-document for the 'expect' script. You could create a temporary file with the here-document that constitutes the 'expect' script; 'chmod' it to be executable; call it; and when the encapsulating shell script completes, remove the temporary file that was created.

Regards!

...JRF...