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
7
  • Please define "doesn't work". What is the expected behavior? What happens instead? Commented Jul 2, 2014 at 20:11
  • added the exception which i am receiving. Commented Jul 2, 2014 at 20:14
  • Do you have to have expect or will consider other options also? Commented Jul 11, 2014 at 16:31
  • I am good with other options as well anubhav... Commented Jul 11, 2014 at 20:40
  • Hi Anubhav... Any luck... I am still stuck with this problem.. :( Commented Jul 14, 2014 at 14:27

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

6 Comments

Yeah I tried that solution as well.. but it doesn't work...my machine doesnt support sshpass.
There are other options too, like expect or lftp.
Just tried using lftp and I received an error lftp: not found
You need to install it. Use your package manager to install, for example: apt-get install lftp or yum install lftp.
It would help if you told me what you are using then. From the tag I suppose you are on AIX. I don't know how AIX works, but looks like there is a package for lftp: perzl.org/aix/index.php?n=Main.Lftp
|
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 [email protected]" 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

2 Comments

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...
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.