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 makescp
works without password?
ssh-agent
(or PuTTY Agent on Windows)?scp
from the first host right, not fromremote_host
The only command you're execute on the remote host isnetstat -tulpn
everything else is on your first host-vv
flag on thescp
command to see if it's even attempting to do key-based auth.#!/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.