Issue in Providing Password in the bash file
Iam trying to copy my file from one server to another in cpanel cron job.I am new in linux so i dont know much about it.So far i have added a cron job and it runs a .sh file.The code for this file is:-
#!/bin/sh find srcdirectory -maxdepth 1 -mtime -1 -exec scp -r "{}" username@hostname:targetdirectory \; echo "done"; but i don't know how to provide password to it when asked,So it throws me error:- Permission denied, please try again. Permission denied, please try again. Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). lost connection How to provide password to it.As iam using find, it will ask for password for every found directory.Any Help Will be greatly Appreciated. Thanks |
Probably the better way would be to use rsync and let it worry about what's new and what's not. Either way, you need to be using keys for authentication. You can find a lot of tutorials on that, both good ones and bad ones, but mostly bad ones. Your choices at the moment for key types are RSA, ECDSA, and Ed25519. If you can, use Ed25519. If you have some weird backwards compatibility contraints, use RSA.
Set up: Code:
mkdir -m 700 /home/ankit/.ssh/ || chmod 700 /home/ankit/.ssh/ Code:
rsync -av -e 'ssh -i /home/ankit/.ssh/remote.key.ed25519' \ |
@Turbocapitalist,First Of All.Thanks for helping me out.But i have to use find as it is the requirement of a client,that only new files should be move.Otherwise I would've used automatic backup option of cpanel.And also problem with rsync is that whatever happens to the real file will also happen to the backup ones.So I've to use Find.
Second, I did a little research about it, and got to know about expect.The demo is:- #!/usr/bin/expect <<EOD spawn ssh username@hostname expect "password:" send "mypassword" interact EOD echo "done"; #!/bin/sh find srcdirectory -maxdepth 1 -mtime -1 -exec scp -r "{}" username@hostname:targetdirectory \; But it is still showing same error,I dont know that what is the main issue here,Can you help me with this please.Any Example will do. Thanks Best Regards Ankit Rauthan |
rsync only copies the changes. So that means only the new files. But if you say you are required to use scp then you would use it with keys like this:
Code:
find srcdirectory -maxdepth 1 -mtime -1 \ If you are using the -r option scp then you are not really copying just the new files and should really use rsync. |
@Turbocapitalist Thanks mate, this was awesome.One last question,Is password of the that username@hostname required anywhere,I mean:-
mkdir -m 700 /home/ankit/.ssh/ || chmod 700 /home/ankit/.ssh/ cd /home/ankit/.ssh/ ssh-keygen -f remotehost.key.ed25519 -t ed25519 -C "$USER from $(hostname)" ssh-copy-id -i /home/ankit/.ssh/remotehost.key.ed25519 username@remotehost.example.com ssh-add /home/ankit/.ssh/remotehost.key.ed25519 find srcdirectory -maxdepth 1 -mtime -1 \ -exec scp -i /home/ankit/.ssh/remotehost.key.ed25519 "{}" \ username@remotehost.example.com:/targetdirectory/ \; Where should the password go of username@hostname(target server).Or it is simply not required. Thanks Mate,this was awesome. Best Regards Ankit Rauthan |
No problem.
Once the SSH key is on the remote server you do not need the password to login any more. In fact it is considered good practice to turn off password authentication for SSH in most cases. Just remember that if you are automating this permanently that an agent needs to be running and that your script needs to point to the right agent using the SSH_AUTH_SOCK environment variable. |
Quote:
Quote:
|
All times are GMT -5. The time now is 05:10 PM. |