4

I have a script that is doing sftp some files from one server to another, like inside the script script.ksh I have the below lines;

sftp user@ip_address << EOF
cd path
put file1
bye
EOF

When I am running the script like;

./script.ksh

It is asking for password like below;

Password:

Here I have to pass the password manually as this is not a password less connection.

I want to make this script automated, that when the Password prompt will occur, then it should take the password on its own. Can anyone have any suggestion please.

5
  • sshpass is also an option
    – Naitree
    Commented Apr 30, 2015 at 23:57
  • @Naitree please can you let me know where and how to use this sshpass command in my script?
    – PriB
    Commented May 2, 2015 at 5:59
  • 1
    If security is not a concern, just prepend sshpass -p password before your sftp command. That should do it.
    – Naitree
    Commented May 2, 2015 at 6:12
  • @Naitree Thanks for your help, but I checked in my unix box sshpass is not installed, I also don't have a privilege to install it. Do you have any other idea other than sshpass? Thanks
    – PriB
    Commented May 5, 2015 at 4:32
  • 2
    Sorry, you have to install something or use ssh key.
    – Naitree
    Commented May 5, 2015 at 5:17

6 Answers 6

4

Try this:

#!/usr/bin/expect

spawn sftp USERNAME@ip_address:/path/to/folder
expect "Password:"
send "PASSWORD\n"
expect "sftp>"
send "put file1\n"
expect "sftp>"
send "bye\n"

or if you don't want to dedicate the whole script to expect:

#!/bin/sh

expect << 'EOS'
spawn sftp USERNAME@ip_address:/path/to/folder
expect "Password:"
send "PASSWORD\n"
expect "sftp>"
send "put file1\n"
expect "sftp>"
send "bye\n"
EOS
1
  • I just noticed this was a question from a year ago...
    – ajlowndes
    Commented Jun 29, 2016 at 7:07
2

By design ssh doesn't allow 'embedding' of passwords - that's because it has a mechanism for non-interactive auth using public-private key pairs.

So I would suggest you consider that as your first port of call. Usually it's as simple as:

  • run ssh-keygen on your client.
  • add the id_rsa.pub to ~/.ssh/authorized_keys on your server.

If that's not an option for some reason, then the fallback option is expect which allows you to do send a password to ssh.

2
  • Hey Sob, sorry that I can't make the server password less. As I am working in a production environment, so I don't have that privilege to do this. So you want me to use expect. Can you please show me how to use this in my script?
    – PriB
    Commented May 2, 2015 at 5:52
  • 2
    Ssh key pairs are better for prod, because they are more secure. You should talk to your sysadmi.
    – Sobrique
    Commented May 2, 2015 at 6:21
0

This is really easy. Especially if the /etc/hosts file contains the IP and name of the other server.

First, generate a public key that can be shared with the other computer.

ssh-keygen

Just press enter to the three questions.

Then, append your public key to the other server like so:

ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host

where remote-host can be either the name from your /etc/hosts file or the IP address.

It will prompt you to accept the other host's key - you answer yes (not just y) Then it will prompt you for the other server's password.

That's it. Your key has now been appended to the remote-host's .ssh/authorized_key file.

To use it, just run scp or ssh as normal and you will see that it no longer prompts you for the paswsword of remote-host.

2
  • How is this different to an answer already given? Commented May 3, 2017 at 13:48
  • The only answer close is @sobrique but the ssh-copy-id -i command was omitted. It requires doing more research to find out how to add the public key. My answer just plain shows the two commands that need to be run. Short and sweet, and it works. No need for expect.
    – SDsolar
    Commented May 3, 2017 at 17:19
0

Try using the lftp command:

lftp -u USERNAME,PASSWORD sftp://IP_ADDRESS << EOF
cd path
put file1
bye
EOF
-2
echo "ftping files to destination"
USER='(username)'
PASSWD='(********)'
sftp user@ip_address << EOF
quote USER $USER
quote PASS $PASSWD
cd path
put file1
bye
EOF
1
  • That looks suspiciously like FTP. Commented May 2, 2017 at 20:21
-3

I have think that you can use scp command

You should create a ssh key. How to generate a key

Now, you can copy the files in other server.

scp file [email protected]:/path/

I hope that it work

2
  • Is there any other method? because I can't make the connection password less. I don't have the permission to do that. I want that, while connecting it should ask password, and then automatically the script will take the password to login in.
    – PriB
    Commented May 2, 2015 at 5:57
  • 1
    If you don't have permission to do password less auth, what makes you think that bypassing that is a good idea?
    – Sobrique
    Commented May 2, 2015 at 6:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .