0

I am trying to automate my SFTP command using a UNIX shell script but for some reason it doesn't work. Here is my code as below. Please share your thoughts/insights.

#This ftp script will copy all the files from source directory into the local directory.
#!/bin/sh
HOST='sftp.xyz.com'
USER='ABC'
PASSWORD='123'
SRC_DIR='From_Src'
TRGT_DIR='/work/'
FILE='abc.txt'

sftp -u ${USER},${PASSWORD} sftp://${HOST} <<EOF
cd $SRC_DIR
lcd $TRGT_DIR
get $FILE
bye

EOF

echo "DONE"

When I try executing the above code I get the below error.

sftp: illegal option -- u
usage: sftp [-1246Cpqrv] [-B buffer_size] [-b batchfile] [-c cipher]
          [-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit]
          [-o ssh_option] [-P port] [-R num_requests] [-S program]
          [-s subsystem | sftp_server] host
       sftp [user@]host[:file ...]
       sftp [user@]host[:dir[/]]
       sftp -b batchfile [user@]host
Kenster
  • 23,465
  • 21
  • 80
  • 106
Teja
  • 13,214
  • 36
  • 93
  • 155

2 Answers2

1

There is no -u option for sftp, see the manual for available options. You can pass the username in this format:

sftp username@hostname

So in your case:

sftp sftp://${USER}@${HOST} <<EOF

This will prompt you the password though. If you don't want a password prompt, take a look at this topic: How to run the sftp command with a password from Bash script?

Community
  • 1
  • 1
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
0

First, learn how to set up keys so that you can ssh, scp, and sftp to a server without a password. Look at ssh-keygen. It is fairly easy. I bet there are how tos on this site. In brief, generate your keys with ssh-keygen. They are created in ~/.ssh. Then add your public key to ~/.ssh/authorized_keys on the destination host where ~ is the home directory of the user you want to log in as. i.e. "ABC" in your example.

Now, you can just do "sftp ABC@sftp.xyz.com" and you will be at the sftp prompt on sftp.xyz.com. From there, getting your script to work should be easy.

My real suggestion is blow off sftp and use scp. e.g.

scp /path/to/the/source_file user@host:/remote/path/file

Its that simple. No "cd" and other gunk to deal with. You are making this way harder than it really is.

Good luck

pedz
  • 2,271
  • 1
  • 17
  • 20
  • Thanks for the response pedz.. I tried looking on google and unix forums which have already provided solutions for automation of sfttp. But mine is little different.. I am trying to do sftp through AIX(UNIX) box which doesn't allow me to run the same script. I tried ssh-gen expect but no luck... – Teja Jul 05 '14 at 00:57
  • Your script would not work on any platform that I know of. Blow off your script. Get your keys set up. The rest will follow. I use AIX every day all day. Its not something weird and unique about AIX. AIX has the standard openssl and openssh of any other box. – pedz Jul 06 '14 at 20:03