From: swagat.dasgupta on
hi everyone...

im working on a utility to automate sftp transactions between a server
and a client machine (get files into a local directory, rename them in
the ftp server, create log files, etc... ), using expect, supported
with some tcl script.

I cant figure out how to get a list of files, in various directories in
the ftp server.. basically a file containing the directory listing.

A command like

sftp> ls > filenames

doesn't work at the sftp prompt.

I need a list of files in the ftp server to create log files, for error
checking, etc.

Help..

Swagat

From: Larry W. Virden on
you might take a look at the code in tcllib for ftp, and see if perhaps
it could be adapted for your purposes. Another place to look - tclvfs,
which may have the ability to treat an ftp site as if it were a virtual
file system (I don't recall at this moment if ftp is one of the
supported protocols).

From: Michael A. Cleverly on
On Wed, 3 Aug 2005 swagat.dasgupta(a)gmail.com wrote:

> im working on a utility to automate sftp transactions between a server
> and a client machine (get files into a local directory, rename them in
> the ftp server, create log files, etc... ), using expect, supported
> with some tcl script.
>
> I cant figure out how to get a list of files, in various directories in
> the ftp server.. basically a file containing the directory listing.
>
> A command like
>
> sftp> ls > filenames
>
> doesn't work at the sftp prompt.
>
> I need a list of files in the ftp server to create log files, for error
> checking, etc.

First, as Larry has already mentioned, you might want to look at the ftp
package in tcllib. It is quite useful for these sorts of tasks.

Now since the sftp prompt is not a shell you can't use standard shell
redirection. What you want to do instead is to send the ls command:

expect "sftp>"
send "ls\r"

then "expect" the output and save it/use it as you see fit.

Something like (untested):

set ls_lines [list]
expect {
-re {\r([^\r]+)} {
lappend ls_lines $expect_out(buffer,1)
exp_continue
}

"sftp>" {
# back at a prompt, so we've got all the ls output by now
# ...
}

timeout {
# oops, the ftp server is being really slow...
# ...
}
}

Alternatively you could expect the pattern -re "(.*)sftp>" to get
everything until the next sftp> prompt (i.e., after all of the ls output
has been delivered). If you go this route, though, be sure to consider
whether you'll ever be talking to a slow server, or ls'ing a very large
directory. Look at the expect man page for information on setting the
timeout and match_max variables.

Michael
From: swagat.dasgupta on
Thanks a lot! Will try it out...