3

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.

  • sshpass is also an option – Naitree Apr 30 '15 at 23:57
  • @Naitree please can you let me know where and how to use this sshpass command in my script? – PriB May 2 '15 at 5:59
  • 1
    If security is not a concern, just prepend sshpass -p password before your sftp command. That should do it. – Naitree May 2 '15 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 May 5 '15 at 4:32
  • 2
    Sorry, you have to install something or use ssh key. – Naitree May 5 '15 at 5:17
3

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.

  • 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 May 2 '15 at 5:52
  • 2
    Ssh key pairs are better for prod, because they are more secure. You should talk to your sysadmi. – Sobrique May 2 '15 at 6:21
1

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
  • I just noticed this was a question from a year ago... – ajlowndes Jun 29 '16 at 7:07
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.

  • How is this different to an answer already given? – roaima May 3 '17 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 May 3 '17 at 17:19
-1

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 user@server.com:/path/

I hope that it work

  • 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 May 2 '15 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 May 2 '15 at 6:22
-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
  • That looks suspiciously like FTP. – roaima May 2 '17 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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