Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to automate data transfer using sftp.

I have a hacky expect script that grabs the data file. Unfortunately, partway through transfer the file is unexpectedly truncated. When I tried doing the same command by hand it always transfers the whole file.

The first line of my expect script is:

spawn  sftp -o ConnectTimeout=120 username@boxAddress

I was under the impression that putting "-o ConnectTimeout=120" would prevent the connection from timing out if it stalled for less than two minutes.

What should I do to get the script to download the whole file?

Another, much less important question: I would prefer to authenticate using keys (but this isn't essential as I'm only grabbing data from the external server). I don't think I could put a key on this external server (because it is not under my control), but (when I used FileZilla to connect to it) I noticed that FileZilla accepted a key from it. How could I setup my script to ask for and use this key?

share|improve this question
up vote 0 down vote accepted

You don't need an expect script just to grab some files over SFTP; the same can be done from command line:

sftp username@boxAddress:/path/to/datafile

will fetch datafile to the current directory.

To answer the second question (which should've been posted separately), you are confusing two different kinds of keys.

  • When FileZilla or sftp accept a key from the server, it is the "host key", used for verifying the server's identity.
  • When you use a key for authentication, it is the user authentication key, used for verifying your identity.

Usually, you do not need any additional control over the server to enable the latter; simply append your public key to ~/.ssh/authorized_keys. This can be done in any way, including SFTP get and put.

share|improve this answer
I DO need some kind of script (doesn't have to be expect) for automating the sftp transfer. Currently it is done manually (by someone who uses FileZilla) and I want to make it automatic. – Dan S Feb 14 '12 at 16:53
@DanShriver: What I was trying to say is that you don't need to wrap interactive-mode sftp within expect; you can just call a single non-interactive sftp command – grawity Feb 14 '12 at 17:40
That's a slight problem: to do non-interactive I have to use the keys. I have a username / pwd pair for an account but it isn't me, so I can't even check their /home/username/.ssh folder to see if they have a key. I guess I need to look into creating an account for me on the box I'm trying to get data from. – Dan S Feb 14 '12 at 18:36
@DanShriver: You "cannot check" because of technical or policy reasons? – grawity Feb 14 '12 at 18:46
When I do "ls" on that directory I get "permission denied". I can view the parent /home/<username> directory though. – Dan S Feb 14 '12 at 18:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.