0

I have written a bash script that downloads several movies from different directories in a remote cluster. My problem is that the bash file asks me my password in the remote cluster for downloading "each" of the movies.

I am wondering if there is a way to keying in the password only once and download all the movies. I really appreciate it if someone can help. My bash script is:

##! /bin/bash
direc1='foldername1 foldernameg foldername7 foldernames'
for AR     in $direc1       ;do
movname="$AR".avi
for ART     in $movname       ;do
DatFile1="$AR"
DatFile2="$ART"
echo "#######################--NEW--####--DOWNLOAD--###############################"
scp -p -r cluster/directory/$DatFile1/$DatFile2 /home/folder3       

done
done

The output is like this:

#######################--NEW--####--DOWNLOAD--###############################
your password: 
foldername1.avi                                                                                                        100% 7580KB   1.5MB/s   00:04    
#######################--NEW--####--DOWNLOAD--###############################
your password: 
foldernameg.avi                                                                                                        100% 7580KB   1.1MB/s   00:02   
#######################--NEW--####--DOWNLOAD--###############################
your password: 
foldername7.avi                                                                                                        100% 7580KB   1.7MB/s   00:05    
#######################--NEW--####--DOWNLOAD--###############################
your password: 
foldernames.avi                                                                                                        100% 7580KB   1.9MB/s   00:07 

In the above output when the message " your password:" appears, I need to key in my password to be able to download each movie.

1
  • Yes, you can use key-based authentication. I'll write an answer – Stewart Oct 31 '20 at 12:05
1

scp uses ssh. Therefore, as long as you have ssh public-key login enabled in the sshd config, your ssh public key has no password, and your public key was added to the authorized keys, then you can use scp without a password.

  1. Generate an ssh key on your client (i.e. where you are running your scp script). Leave the password empty for passwordless login.
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/stew/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/stew/.ssh/id_rsa
Your public key has been saved in /home/stew/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:CB8ztwpXrT7lWQj/3W+8IAoHwqTelEBva9/aA7u2w3w stew@stewbian
The key's randomart image is:
+---[RSA 3072]----+
|   .             |
|  . .    .       |
|   ..++ + .      |
|    *ooB = .     |
|   ..B+.S + .    |
|  . +oo+oo + . . |
|   . .+o=oo......|
|       *=E. . . +|
|      .+=o.    oo|
+----[SHA256]-----+
$
  1. Ensure the ssh server (remote machine) accepts public keys
$ cat /etc/ssh/sshd_config | grep PubkeyAuthentication
#PubkeyAuthentication yes

If it shows PubkeyAuthentication no, then change the file and systemctl restart sshd.

  1. Add your public key to the ~/.ssh/authorized_keys of the remote server. This can be done with ssh-copy-id <user>@<server> or by manually appending the contents of ~/.ssh/id_rsa.pub which we made in step 1 from your local machine to ~/.ssh/authorized_keys of the server.
0

key-based authentication is the best way to go. If the remote side is not configured for this, this kind of problem is what is for:

#!/usr/bin/env expect
set timeout -1

# enter your password once
stty -echo
send_user -- "Enter your password for the cluster: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)

set folders {
    foldername1
    foldernameg
    foldername7
    foldernames
}

foreach f $folders
    spawn scp -p -r cluster/directory/$f/$f.avi /home/folder3
    expect {
        "your password:" { send "$pass\r"; exp_continue }
        eof
    }
}

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.