Re: [Hampshire] embedded expect

Top Page

Reply to this message
Author: Damian Brasher
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] embedded expect

> On Sat, Oct 13, 2007 at 07:37:23PM +0100, Damian Brasher wrote:
>>>> ./expect << EOF
>>>> spawn ssh root@$IP_ADD_A 'ls > junk'
>>>> expect "*?assword:*"
>>>> send -- "$ROOT_PASS_A\r"
>>>> send -- "\r"
>>>> expect eof
>>>>
> I use expect all the time to log in to ssh sites (with passwords or
> passkeys, as appropriate)
>
> The problem is probably the pattern for password - remember that expect
> expects to search within strings, so you don't need the '*' bits. you
> could use something like:
>
>     ROOT_PASS=fred
>     expect <<EOF
>     spawn ssh root@$IP_ADD_A ls > junk
>     expect 'assword:'
>     send -- "$ROOT_PASS\r"
>     expect eof
>     EOF

>
> The command doesn't need quotes round it - everything after the host
> name (including redirection symbols as above) are passed to the host, so
> the 'ls' command is run in the root user's home directory and the output
> redirected to 'junk' in that directory.
>
> I tend to run these login sections in a "while" loop - this allows for a
> range of responses to arrive, some optional. For example, when
> connecting to a range of hosts I recognise, so I know they are valid, I
> have:
>
> set logindone 0
> spawn ssh $targetuser@$targethost
> while 1 {
>         expect {
>                 "login:" {if $logindone==0 {set logindone 1
>                                 send "$targetuser\r"}}
>                 "sword:" {send "$targetpasswd\r"}
>                 "Do you want to choose a different one" {sleep 0.2;send
> "\r"}
>                 "Are you sure you want to continue connecting" {sleep
> 0.2;send "yes\r"}
>                 "\\$ $" {send "export DISPLAY=$hostip:0\r" ; break}
>                 "# $" {send "export DISPLAY=$hostip:0\r" ; break}
>                 "\] $" {send "export DISPLAY=$hostip:0\r" ; break}
>                 timeout {send_user "I have timed out logging in to
> $targethost as $targetuser\r" ; exit}
>         }
> }
> interact timeout 300 {send " \177"}

>
>
> The 'Do you want to choose..' bit is to get round over-zealour RedHat
> security options, and the 'Are you sure' is the ssh 'it's a new host'
> prompt. The next three lines cope with a number of prompt types,and the
> last bit is a clean timeout response. (The 'login' bit is there because
> I actually use the same fragment to support telnet too). Once it's
> logged in it goes into interactive mode so that I can do whatever I want
> on the session, but sends space-delete after 5 minutes of inactivity to
> keep the connection alive. I also have versions of the above that log
> in, then run a specified command under expect's control - this allows
> for commands which prompt for data.
>
> I too tried to write expect scripts from man pages etc, but was very
> glad when I finally took the plunge and bought the O'Reilly "Exploring
> Expect", written by expect's author, Don Libes. It's not only very
> useful, it's also quite a good read!
>
> Simon
>


That's great, thanks for the above - I will study this a bit later on.

Damian

--
Damian Brasher
www.diap.org.uk "Aim to protect mission critical data: 'You can't afford
to lose...'"
All mail scanned by clam-av http://www.clamav.net/