scp automation

hi there ,

i want to use the scp to transfer the file from one machine to another machine non-stop. i have put it in a .sh file. but everytime i run it and it prompts me to input password.

pc3@pc3-desktop:~/Documents$ ./sample3.sh
pc-main@192.168.1.117's password:
screenshot.jpg 100% 113KB 113.4KB/s 00:00
pc-main@192.168.1.117's password:
screenshot.jpg 100% 113KB 113.4KB/s 00:00
pc-main@192.168.1.117's password:
screenshot.jpg 100% 113KB 113.4KB/s 00:00
pc-main@192.168.1.117's password:

and this is my code :

sample3.sh
#!/bin/bash
for (( ; ; ))
do
scp /home/pc3/Desktop/javacode/screenshot.jpg pc-main@192.168.1.117:/home/pc-main/Documents
done

is there anyway to make it can keep transfer file without the need to keep input password ? thanks in advance for helping . :slight_smile:

for doing that you need to place public key authentication between two machines.

  1. Generate private/public key pair
  2. Copy the public keys into .ssh of server from which you want to copy files.
    check this post for details:
    scp without prompting for password

thanks for your quick reply :slight_smile:

i run the script from ubuntu to ubuntu is no problem at all. but when i use it on windows (i have install cygwin) to ubuntu, it give me this result

windows@windows-PC /home/Windows
$ ./sample3.sh
./sample3.sh: line 2: syntax error near unexpected token `$'\r''
'/sample3.sh: line 2: `for (( ; ; ))

can anyone please tell me what is the problem there ? thanks

that because in your file line ending has CR LF, you just need LF. To fix this run this on your script

awk '{ sub("\r$", ""); print }' your_current_script.sh > newscript.sh
1 Like

thanks 47shailesh

based on the link u given to me , the link is want to copy file from a remote machine. but i want to copy a file to a remote machine. is it the same way to do that ? sorry for my ignorance and thanks in advance for reply.