Scp command over ssh without password in script

7,238

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/
Share:
7,238

Related videos on Youtube

fuser
Author by

fuser

Updated on September 18, 2022

Comments

  • fuser
    fuser almost 2 years

    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?

    • Criveti Mihai
      Criveti Mihai over 8 years
      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)?
    • Eric Renouf
      Eric Renouf over 8 years
      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
    • David King
      David King over 8 years
      Try adding the -vv flag on the scp command to see if it's even attempting to do key-based auth.
    • fuser
      fuser over 8 years
      Criveti Mihai, yes I am using Putty Agent on Windows.
    • gogoud
      gogoud over 8 years
      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.
    • fuser
      fuser over 8 years
      I did all actions as root - FOR SURE! Such locations I had given only for example - because we can use in spite of them real host names and ip addresses.
    • fuser
      fuser over 8 years
      #!/bin/bash it is just my blot. Thanks! I have corrected it.
    • roaima
      roaima over 8 years
      Your code uses paths such as /home/${remote_user}/ on the local machine all over the place. Is that really intentional?
  • fuser
    fuser over 8 years
    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
  • ekoeppen
    ekoeppen over 8 years
    @fuser some step would be missing. check this post to setup passwordless sftp stackoverflow.com/a/15808596/1135954
  • fuser
    fuser over 8 years
    Why do I need to set up passwordless sftp If I have already had passwordless ssh?