LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-23-2017, 06:54 AM   #1
AnkitRauthan
LQ Newbie
 
Registered: Oct 2017
Posts: 6

Rep: Reputation: Disabled
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

Last edited by AnkitRauthan; 10-23-2017 at 07:14 AM.
 
Old 10-23-2017, 07:18 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,607
Blog Entries: 3

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
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.
 
1 members found this post helpful.
Old 10-23-2017, 07:42 AM   #3
AnkitRauthan
LQ Newbie
 
Registered: Oct 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
@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

Last edited by AnkitRauthan; 10-23-2017 at 07:43 AM.
 
Old 10-23-2017, 07:50 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,607
Blog Entries: 3

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
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.
 
1 members found this post helpful.
Old 10-23-2017, 08:04 AM   #5
AnkitRauthan
LQ Newbie
 
Registered: Oct 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
@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
 
Old 10-23-2017, 08:18 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,607
Blog Entries: 3

Rep: Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851Reputation: 2851
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.
 
1 members found this post helpful.
Old 10-24-2017, 05:14 PM   #7
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 756

Rep: Reputation: 144Reputation: 144
Quote:
Originally Posted by Turbocapitalist View Post
... 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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I was logged in as root without providing password lordadamson Slackware 6 12-28-2013 06:32 AM
[SOLVED] SFTP file upload bash script issue. moodah Programming 1 10-07-2010 10:55 PM
issue in using private key file in bash script manya Programming 3 08-24-2009 01:49 AM
Providing a fellow member with updates to an issue with sound jasmine.aura LinuxQuestions.org Member Intro 0 11-04-2007 10:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration