LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2011, 06:53 PM   #1
freakinweirdo
LQ Newbie
 
Registered: May 2005
Posts: 4

Rep: Reputation: 0
automatically pass a password to rsync for remote host.


Hi all,

I need to be able to use an rsync command in script that will be run by cron. And it needs to be able to pass a password to rsync so that the remote server it's connecting to will authenticate.

I cannot set up ssh keys between the two servers, it's not an option.
I cannot use any other language other than bash, it's my only option.
I know this is highly insecure, I have no other option.

So far I have this:
rsync --rsh="/usr/bin/ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PreferredAuthentications=password" -raxv /source/dir/* user@remotehost:/target/directory/.

This allows the script to ignore host verification and goes directly to the password prompt. I need the script to fill in this password prompt with the password that is stored in a variable.

I tried using expect, but I honestly don't know the syntax, it just keeps failing. A lot of the examples I'm finding online for expect starts off with a "spawn", which i don't have installed, and not sure if I have the ability to install it yet.


Any thoughts or tips that can help with this?


Thanks in advance.
 
Old 05-24-2011, 07:17 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
spawn is not an installed program, it is an Expect keyword. IMHO Expect is exactly the way to do it in your case.
 
Old 05-24-2011, 07:37 PM   #3
freakinweirdo
LQ Newbie
 
Registered: May 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TobiSGD View Post
spawn is not an installed program, it is an Expect keyword. IMHO Expect is exactly the way to do it in your case.
Yeah I'm seeing that now. However I'm still unable to get the syntax for expect right.

Created a file as below
Code:
spawn rsync --rsh="/usr/bin/ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PreferredAuthentications=password" -raxv /source/dir/* user@remotehost:/target/directory/.
expect assword
send blarg\n
and executing it like this:
Code:
expect -f tmp
spawn rsync --rsh="/usr/bin/ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PreferredAuthentications=password" -raxv /source/dir/* user@remotehost:/target/directory/.
Missing trailing-" in remote-shell command.
rsync error: syntax or usage error (code 1) at main.c(335) [sender=2.6.8]
send: spawn id exp6 not open
    while executing
"send blarg\n"
    (file "tmp" line 3)
I'm SURE I'm messing something up, I've seen a dozen ways to use execpt online, and can't get any to work. My brain is fried so I'll be revisiting this tomorrow.
 
Old 05-24-2011, 08:46 PM   #4
freakinweirdo
LQ Newbie
 
Registered: May 2005
Posts: 4

Original Poster
Rep: Reputation: 0
alright I couldn't leave it alone, but as luck would have it, I found http://rootprompt.org/article.php3?article=5760, which finally had a working example for me since I've never used expect.

with that, I wrote up this. I couldn't seem to execute it all as a 'string', so wrote it to a file, executed it, then removed it. It's dirty but it will do for now.

Code:
#!/bin/bash

pass=password
expect="""#!/usr/bin/expect -f 

set timeout  -1

spawn rsync -ravx user@host:from/* to/.
expect  \"password:\" { send \"$pass\\n\"}
expect \"#\"
"""

echo "$expect" > /tmp/temp.sh

chmod 777 /tmp/temp.sh

/tmp/temp.sh

echo "result is [$?]"

if [[ $result -eq 0 ]]; then
        echo "Success"
else
        echo "Failure"
fi

rm -f /tmp/temp.sh

I'd like to get it to execute with except -f, but I'll mess with that later, for now it actually works, so I'm not freaking out :-)
 
Old 05-25-2011, 02:19 PM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by freakinweirdo View Post
I cannot set up ssh keys between the two servers, it's not an option.
What about hostbased authentication? Or running rsync in daemon mode on the target machine and expect a password for connections, which you can set in the cron entry by just setting an environment variable. I do it this way like:
Code:
RSYNC_PASSWORD=DXd94hA9
0 3 * * * rsync -aq --delete Gq3fBtvk@10.0.2.1::my_server /backup/my_server
 
  


Reply



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
Can't SSH to remote machine: Connection closed by remote host Avatar Linux - Networking 35 10-23-2017 12:21 AM
telnet: Unable to connect to remote host: No route to host grob115 Linux - Networking 3 07-30-2010 10:18 AM
how to take backup automatically using rsync poswer Linux - Server 3 07-01-2009 11:15 AM
Logging into a remote host automatically through shell script patil_reddy Red Hat 1 06-13-2006 06:23 AM
Resolving <www.some remote host>.... failed: Host not found. koodoo Linux - Newbie 2 06-27-2005 08:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:05 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