Teja
Teja

Reputation: 13544

Automating sftp using IBM AIX(UNIX) shell script

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

Upvotes: 0

Views: 30979

Answers (2)

pedz
pedz

Reputation: 2349

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

Upvotes: 0

Gergo Erdosi
Gergo Erdosi

Reputation: 42073

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?

Upvotes: 1

Related Questions