LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Issue in Providing Password in the bash file (https://www.linuxquestions.org/questions/linux-newbie-8/issue-in-providing-password-in-the-bash-file-4175616198/)

AnkitRauthan 10-23-2017 06:54 AM

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

Turbocapitalist 10-23-2017 07:18 AM

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/

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

Operation:

Code:

rsync -av -e 'ssh -i /home/ankit/.ssh/remote.key.ed25519' \
        /srcdirectory/  username@remotehost.example.com:/targetdirectory/

If you store the key in an agent using ssh-add then you can use a strong passphrase and still automate your task.

AnkitRauthan 10-23-2017 07:42 AM

@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

Turbocapitalist 10-23-2017 07:50 AM

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 \
        -exec scp -i /home/ankit/.ssh/remotehost.key.ed25519 "{}" \
        username@remotehost.example.com:/targetdirectory/ \;

Mind the slashes on the target directory.

If you are using the -r option scp then you are not really copying just the new files and should really use rsync.

AnkitRauthan 10-23-2017 08:04 AM

@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

Turbocapitalist 10-23-2017 08:18 AM

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.

KenJackson 10-24-2017 05:14 PM

Quote:

Originally Posted by Turbocapitalist (Post 5772934)
... an agent needs to be running and that your script needs to point to the right agent using the SSH_AUTH_SOCK environment variable.

My favorite way to accomplish that is with the keychain package. It's in the repository of most Linux distros.
Quote:

Keychain is a manager for OpenSSH, ssh.com, Sun SSH and GnuPG agents. It acts as a front-end to the agents, allowing you to easily have one long-running agent process per system, rather than per login session. This dramatically reduces the number of times you need to enter your passphrase from once per new login session to once every time your local machine is rebooted.


All times are GMT -5. The time now is 05:10 PM.