2

Here is my problem: I would like to perform scp command after ssh to remote server without typing any passwords. I describe my steps. My first steps were:

ssh-keygen -t rsa and

local@host$ ssh-copy-id -i /root/.ssh/id_rsa.pub remote@host on local machine

Then I did the same on remote machine:

ssh-keygen -t rsa and

remote@host$ ssh-copy-id -i /root/.ssh/id_rsa.pub local@host

After all described above I could do ssh on both machines without typing any password. I could perform scp command on remote machine like scp /home/remote/info.txt.gz local@host:/root/ and everything worked perfectly.

Then I tried to make a script with some actions described below and the last step in my script was scp command that did not work as I expected.

#!/bin/bash
remote_user=$1
remote_host=$2
local_user=$3
local_host=$4

echo "Testing connection to ${host}..."
ssh -n -o NumberOfPasswordPrompts=0 ${remote_user}@${remote_host}
if [ $? -ne 0 ]; then
    echo "FATAL: You don't have passwordless ssh working."
    echo "Try running ssh-keygen"
    exit 1
fi
echo "Okey. Starting the process."
ssh ${remote_user}@${remote_host} netstat -tulpn > /home/${remote_user}/info.txt;uptime |awk '{ print $3 }' >> /home/${remote_user}/info.txt;
if [ $? -ne 0 ]; then
    echo "An error occurred."
else
    echo "File is ready for gzipping!"
fi
gzip /home/${remote_user}/info.txt
if [ $? -ne 0 ]; then
    echo "file was not archived"
else
    echo "Archive is ready!"
fi
echo "Starting copy archive from ${remote_host} to ${local_host}"
scp /home/${remote_user}/info.txt.gz ${local_user}@${local_host}:/root/
if [ $? -ne 0 ]; then
    echo "Error while transferring!"
else
    echo "Copy has been transferred successfully!"
fi

Scp command asked me for password o_O.

When I did all steps in script manually everything worked perfectly but in script scp demanded password. I read a lot through stackexchange and found this answer Using an already established SSH channel. This answer requires Open SSH but my problem can be resolved manually via SSH as I said but in script it did not work. What am I going to do to make scp works without password?

9
  • 1
    TL;DR. Is your SSH key encrypted (ex: do you type in a password when using your key?). Are you using ssh-agent (or PuTTY Agent on Windows)? Nov 30, 2015 at 16:19
  • 1
    You know you're executing that scp from the first host right, not from remote_host The only command you're execute on the remote host is netstat -tulpn everything else is on your first host Nov 30, 2015 at 16:21
  • Try adding the -vv flag on the scp command to see if it's even attempting to do key-based auth.
    – David King
    Nov 30, 2015 at 16:53
  • Criveti Mihai, yes I am using Putty Agent on Windows.
    – fuser
    Nov 30, 2015 at 19:11
  • First line of your script should be #!/bin/bash. Did you do all actions as root - or some as another user? I think your scp line last parameter should be ${remote_user}@${remote_host}:/root/ - assuming you really want to save the .gz file at remote's /root which seems a strange location.
    – gogoud
    Dec 1, 2015 at 11:58

2 Answers 2

1

If below scp command is being run on local_host,then you are trying to scp as local_user to local_host as local_user - ofcourse this would prompt for password, as you had only password less login for remote <-> local users only - not local <-> local

scp /home/${remote_user}/info.txt.gz ${local_user}@${local_host}:/root/
0

If you have already setup ssh keys, then look into transffering files using sftp. Single command, you don't need to go by the way you are currently trying to do it i.e. ssh then scp, plain sftp would work.

3
  • I tried to use what you said, mtk. Something like this sftp ${local_user}@${local_host} <<EOT put info.txt.tgz quit EOT inspite of scp command but it asked for password again
    – fuser
    Dec 2, 2015 at 6:55
  • @fuser some step would be missing. check this post to setup passwordless sftp stackoverflow.com/a/15808596/1135954
    – mtk
    Dec 2, 2015 at 12:04
  • Why do I need to set up passwordless sftp If I have already had passwordless ssh?
    – fuser
    Dec 2, 2015 at 12:30

You must log in to answer this question.

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