Discussion:
Spawn sftp doesn't work in expect script
(too old to reply)
Sooraj S Padmanabhan
2014-08-18 08:27:26 UTC
Permalink
Hi,

Given below is the command that I use to connect to my SFTP server. When I issue the command, it just asks for the password.
sftp -oProxycommand="nc -x<:SquidServer_IP>:3128 -Xconnect <SFTPServer_IP> 2222" ***@localhost

I want to use expect script to auto login to SFTP server and print its contents. Given below is the script that I wrote.

#!/usr/bin/expect -f

set prompt sftp>
set timeout 25
set passwd "test"

if [catch {spawn sftp -oProxycommand="nc -x<:SquidServer_IP>:3128 -Xconnect <SFTPServer_IP> 2222" ***@localhost} error] {
send_user "$error"
send_user "spawn error"
} else {
expect "password:"
send "$passwd\r"
expect "$prompt"
send "$ls -lrt\r"
}

But it errors out with the following error.
spawn sftp -oProxycommand="nc -x<:SquidServer_IP>:3128 -Xconnect <SFTPServer_IP> 2222" ***@localhost
sftp: illegal option -- x
usage: sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config]
[-o ssh_option] [-P sftp_server_path] [-R num_requests]
[-S program] [-s subsystem | sftp_server] host
sftp [user@]host[:file ...]
sftp [user@]host[:dir[/]]
sftp -b batchfile [user@]host

When I'm doing it manually, it works fine. Could someone please help me out here?

Regards,
Sooraj
Schelte Bron
2014-08-18 15:40:19 UTC
Permalink
Post by Sooraj S Padmanabhan
When I'm doing it manually, it works fine. Could someone please help me out here?
When you do it manually you (probably) use bash and bash quoting
rules. When doing it from Tcl you need to use Tcl quoting rules:

spawn sftp "-oProxycommand=nc -x<:SquidServer_IP>:3128 -Xconnect
<SFTPServer_IP> 2222" ***@localhost

In Tcl you can't start quoting in the middle of a word.


Schelte.
Sooraj S Padmanabhan
2014-08-19 04:55:16 UTC
Permalink
Post by Schelte Bron
Post by Sooraj S Padmanabhan
When I'm doing it manually, it works fine. Could someone please
help me out here?
When you do it manually you (probably) use bash and bash quoting
spawn sftp "-oProxycommand=nc -x<:SquidServer_IP>:3128 -Xconnect
In Tcl you can't start quoting in the middle of a word.
Schelte.
Thank you Schelte for the reply. Could you please let me know if there is any way I can do this in expect script??

Regards,
Sooraj
Schelte Bron
2014-08-20 10:49:35 UTC
Permalink
Post by Sooraj S Padmanabhan
Thank you Schelte for the reply. Could you please let me know if
there is any way I can do this in expect script??
I already did:

spawn sftp "-oProxycommand=nc -x<:SquidServer_IP>:3128 -Xconnect
<SFTPServer_IP> 2222" ***@localhost


Schelte.
Sooraj S Padmanabhan
2014-08-21 06:13:02 UTC
Permalink
Thank you Schelte. It worked perfectly.

Rich
2014-08-20 21:57:57 UTC
Permalink
Hi,
to pass the literal quotes along into the spawn. Maybe that will make
a difference. The error message indicates that sftp is attempting to
interpret -x as a switch for it (sftp) which it is really an "nc"
switch instead.
Loading...