Jump to content

Crazy expect script behavio(u)r


Recommended Posts

I have this expect script that I generated with autoexpect and then modified. I'd like to be able to pass as many files as I want to it and have it still work right. It is supposed to scp each file in the argument list to my desktop server. I really don't want to have to make a new scp connection for each file, but here's the weird behavior. If I write it like this:

#!/usr/bin/expect -f

set num [expr $argc-1]
set timeout -1
spawn scp -P 23 [lindex $argv 0] [lindex $argv 1] omar@192.168.1.101:/home/omar
match_max 100000
expect -exact "omar@192.168.1.101's password: "
send -- "<<my password>>\r"

expect eof

It works as expected and I can pass two files to it like this

 

myscript.exp file1 file2

 

But what if I want to pass any number of files?

#!/usr/bin/expect -f

set num [expr $argc-1]
set timeout -1
spawn scp -P 23 [lrange $argv 0 $num] omar@192.168.1.101:/home/omar
match_max 100000
expect -exact "omar@192.168.1.101's password: "
send -- "<<my password>>\r"

expect eof

 

And try to call it like this:

myscript.exp file1 file2 file3

I get an error

file1 file2 file3: No such file or directory

 

So it's still treating as a list or one long string. Is there a way I can pass an unlimited number of files on the commandline and do this? I hope this makes sense. TIA

Edited by Steve Scrimpshire
Link to comment
Share on other sites

P.S. Yes, I have tried using key-based authentication so I don't have to use an expect script, but I can't seem to get it to work:

debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/omar/.ssh/id_rsa.pub
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug2: we did not send a packet, disable method
debug3: authmethod_lookup keyboard-interactive
debug3: remaining preferred: password

Edited by Steve Scrimpshire
Link to comment
Share on other sites

I worked around it like this:

#!/bin/bash

E_BADARGS=65

if [ ! -n "$1" ]
then
 echo "Usage: `basename $0` file1 file2 etc."
 exit $E_BADARGS
fi

echo "spawn scp -P 23 $* omar@192.168.1.101:/home/omar
match_max 100000
expect -exact \"omar@192.168.1.101's password: \"
send -- \"<<my password>>\\r\"

expect eof" > scp2.exp

/usr/bin/expect scp2.exp
rm -f scp2.exp

 

but that seems like a horribly innefficient way of doing it.

 

A little different, so I can pass my password as the first parameter and not have it stored in a file anywhere:

#!/bin/bash

E_BADARGS=65

if [ ! -n "$2" ]
then
 echo "Usage: `basename $0` password file1 file2 etc."
 exit $E_BADARGS
fi

blech=$1
shift

echo "spawn scp -P 23 $* omar@192.168.1.101:/home/omar
match_max 100000
expect -exact \"omar@192.168.1.101's password: \"
send -- \"$blech\\r\"

expect eof" > scp2.exp
                                                                             
/usr/bin/expect scp2.exp                                                      
rm -f scp2.exp

 

(If anyone is wondering, yes, I intentionally have ssh running on port 23)

Edited by Steve Scrimpshire
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...