0

I use linux based operating system. let's say, I have a server contain 10 files like tmp1 .... tmp10, and I want to copy three specific files to my local machine, let's say, tmp3, tmp7 and tmp10. In this case I have to use the "scp-command" three times, where I have to enter the password also three times. the problem is, that I have to do this process so often, so I "wrote" the script below. My PROBLEM is, that the script runs without any error message, but the files are not copied.

echo "insert path of source:" #prompt to enter the path of files you want to copy
read SOURCE                   # saving the path in the variable SOURCE
echo "insert path of target:" #prompt to enter the path, where you want to past the copied files 
read TARGET                   # saving the path in the variable SOURCE
echo "Insert the port"        # prompt to enter the port of the server
read port                     # saving the port in the variable PORT
echo "Password?"              # asking for password
read -s -a PASSWORD           # saving the password in the variable PASSWORD
x=(tmp1 tmp2)                 # An array contains the files i want to copy.
for i in "${x[@]}"            # A for-loop to copy each of the files in the array (x) from the SOURCE to the TARGET
do
  echo "the file $i"          # just to check if the array has been read.
#!/usr/bin/expect -f          # to read the expect-programm
  expect -c " 
            spawn /usr/bin/scp -P $prot $SOURCE/$i $TARGET
            expect { 
           "Password:" { send $PASSWORD\r\n; interact } 
           eof { exit } 
           }
            exit
            "
done                         # End of the for-loop
PASSWORD=0                   # To delete the variable PASSWORD

thank you in advance!!

Taktech
  • 455
  • 1
  • 8
  • 18
  • The proper fix is to set up public-key authentication with the remote host. – tripleee Sep 24 '13 at 07:54
  • You should probably be quoting the literal strings in the `expect` script; `send \"$PASSWORD\"` etc. – tripleee Sep 24 '13 at 07:55
  • 2
    Why can't you use scp user@host:"/path/tmp{3,7,10}" .? The trick is to quote the path so the expansion is done on the remote machine. That should give you the files you need. – user1613254 Sep 24 '13 at 07:58

3 Answers3

0

You can use as scp to copy more than one file. For example:

scp remote:tmp{3,7,10} local
Joni
  • 108,737
  • 14
  • 143
  • 193
0

scp is using ssh for the remote authentication. There are multiple approaches to handle your issue: - use ssh agent and set up key based logon - this is more secure. There's a howto on setting up ssh to use key based logon in the following link https://help.ubuntu.com/community/SSH/OpenSSH/Keys - use the pscp tools that allows you to pass the password to it directly using the -pw argument.

nbaroz
  • 126
  • 1
  • 5
0

First of all i suggest you use public key authentication, so you don't have to worry about passwords in your scripts. Use the ssh-copy-id script (at least in Ubuntu) to copy your key to the host you will work on.

I would, as @Joni already mentioned, copy multiple files with one call. Then, if you have to go on with passwords, I would let the scp command handle the password prompt.

Community
  • 1
  • 1
Ortwin Angermeier
  • 5,957
  • 2
  • 34
  • 34