cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How to get file size over SFTP? And use private key with passphrase?

From: Gary V. Vaughan <curl-library_at_mlists.thewrittenword.com>
Date: Mon, 21 Sep 2009 17:59:15 +0000

[2nd resend, as the other 2 messages I resent after this one arrived
 at the list already]

[Resend after 3 hours or so... apologies if the original also arrives]

Hi Daniel,

Thanks for your answers.

On Sat, Sep 19, 2009 at 11:19:03PM +0200, Daniel Stenberg wrote:
> On Fri, 18 Sep 2009, Gary V. Vaughan wrote:
>
> > I'm writing a multi-protocol file download application using pycurl 7.19
> > built with libcurl from the curl-7.19.6 release. I want to use my own
> > progress bar, so I need to know the file size in advance of launching the
> > download proper...
>
> The progress callback informs about the file size, if known. You don't need to
> do any particular file size check of your own for that to happen.

In my case I'm using a single progress bar to show download of a group
of related files - an archive and a checksum file for example. When
I initialise the progressbar, of course it needs to know the total
number of bytes that will be fetched before it starts. And the
curl progress callback only gets the total size of the current file,
so I need some means to add up the sizes of the other files that
will also be shown on the same progressbar.

So, the only transport layer agnostic way to get the size of a remote
file is to start downloading it, but use a progress callback that
always aborts?

> > The above works only if I use a key with no-passphrase. Is it possible to
> > use a key that takes a passphrase?
>
> I believe so, yes.
>
> > pycurl.URL: 'sftp://example.com/~/a_26MB_binary_file',
> > pycurl.USERPWD: 'gary:key pass-phrase'
>
> USERPWD is for the remote auth, not for the key auth. CURLOPT_KEYPASSWD is
> what you want I believe.

Thanks. pycurl still calls this pycurl.SSLKEYPASSWD, but even so:

  $ cat pycurl
  import pycurl

  privatekey = '/home/gary/dsa_pass'

  options = {
     pycurl.URL: 'sftp://example.com/~/a_26MB_binary_file',
     pycurl.USERPWD: 'gaz:',
     pycurl.SSLKEYPASSWD: 'rsa key passphrase',
     pycurl.SSH_PRIVATE_KEYFILE: privatekey,
     pycurl.SSH_PUBLIC_KEYFILE: privatekey + '.pub',
     pycurl.WRITEDATA: open('/dev/null', 'wb'),
     pycurl.VERBOSE: 1,
  }

  c = pycurl.Curl()
  for (k, v) in options.items():
      c.setopt(k, v)
  try:
      c.perform()
  except pycurl.error, msg:
      print 'Error: %s' % msg

  $ ./pycurl
  * About to connect() to example.com port 22 (#0)
  * Trying 123.45.67.89... * connected
  * Connected to example.com (123.45.67.89) port 22 (#0)
  * SSH authentication methods available: publickey,password
  * Using ssh public key file /home/gary/.ssh/id_rsa.pub
  * Using ssh private key file /home/gary/.ssh/id_rsa
  * SSH public key authentication failed: Unable to initialize private key from file
  * Authentication failure
  * Closing connection #0
  Error: (67, 'Authentication failure')

Using the same key and passphrase for a manual scp connection works
just fine, so it must be a bug in one of: my code above, pycurl or
libcurl itself...

> > is it possible to get just the filesize over SFTP using pycurl?
>
> No, unfortunately at least not in that conveniant CURLOPT_NOBODY way most
> other protocols support.
>
> It'd be a great addition though.

Okay :(

Cheers,
    Gary

-- 
Gary V. Vaughan (gary_at_thewrittenword.com)
Received on 2009-09-21