Inputting a Password on a script for rsync to login to a remote server to complete rsync

23,670

Solution 1

I found my own answer doing some more digging into it, I'll post the answer to how this can be completely automated along with a couple helpful links that helped me come to this:

#!/bin/bash


subdir=`hostname`

rsync -ah -e 'sshpass -p "password" ssh -p 48180 -o 
StrictHostKeyChecking=no' /var/www/html/xgplayer/events 
[email protected]:/var/www/html/xgplayer/events/"$subdir" 

That Rsync command is all one command. The links that helped:

https://unix.stackexchange.com/questions/33271/how-to-avoid-ssh-asking-permission https://unix.stackexchange.com/questions/111526/rsync-without-prompt-for-password (The second, higher voted answer ended up being more helpful than the "best" answer)

Also, obviously swap user and password with your user and password where need be, not posting them here obviously.

Thanks to everyone who tried helping, you guys did help steer me in the right directions for the most part! :)

Solution 2

Use the option: --password-file=rsync_pass

rsync -ah -e 'ssh -p 48180' --password-file=rsync_pass /home/user/test [email protected]:/var/www/html/xgplayer/events/"$subdir" -y

The file rsync_pass contains the user password.

Share:
23,670

Related videos on Youtube

tommy61157
Author by

tommy61157

Just a potato roaming the interwebs trying to soak up information :P

Updated on September 18, 2022

Comments

  • tommy61157
    tommy61157 over 1 year

    I'm trying to make a .sh file that a cronjob will run occasionally that does an automatic rsync. This is how my file looks so far:

    #!/bin/bash
    
    subdir=`hostname`
    
    rsync -ah -e 'ssh -p 48180' /home/user/test 
    [email protected]:/var/www/html/xgplayer/events/"$subdir" -y
    

    That is one command, had to cut it in half because it was too long for one line.

    I have it set to create a subdirectory based off of the hostname of the machine (please tell me if that looks good too, I'm basing that off code used for a slightly different purpose) which that user does have access to. (I just called the users user for reasons, ignore that)

    What my question is that will that -y have it automatically agree to store an ssh key like I'm trying to have it do? Also, how do I have it automatically input a password through the command I'm trying to execute in the ssh when it tries to login as that user to rsync the files? I thought there was something like -- that could do that. Any help would be appreciated :)

    I'm running Ubuntu Server 16.04 LTS on both machines if that helps.

    Edit: Not to be super tricky, but whatever the solution is also has to be able to be put into a .sh file and run itself without user input, this is for a very specific task, sorry.

    Edit 2: Doing this as per request of someone helping me, so, I can't provide screenshots unfortunately, but it asks for a password for a user on the 10.3.10.110 whenever I run the rsync command I listed, and whenever I run the ssh-keygen on a new client trying to generate a key for that user at that machine. The application I'm trying to use this for requires that this be automated completely which is why this is a problem.

    Edit 2.5: Once I run the keygen and ssh-copy-id, it no longer prompts for a password at rsync, but this is going to be implemented across many machines, and for this project, I don't mind putting those in some kind of setup .sh file, but those commands require inputs which would need to be automated in order for me to use for my specific case.

    Edit 3: I'm commenting a lot because I don't have enough rep to chat directly with the people trying to help me, new to this site still.

    • tommy61157
      tommy61157 almost 6 years
      Would I run the ssh-copy-id on the machine that's not the 10.3.10.110 that's trying to get the files to the server on that IP? I do have full access to both machines is why I ask.
    • tommy61157
      tommy61157 almost 6 years
      Okay, forget my last question, editing this comment, I'll add a new comment once I've tested this. Just looked up what that command does so I have an understanding of what's going on. Original: Will that break anything if I do it? Sorry, I just don't fully know what's going on and this is for an important project is all.
    • tommy61157
      tommy61157 almost 6 years
      When doing that, I get an error saying Failed to open ID file '/home/user/.pub': no such file (to install the contents of '/home/user/.pub' anyway, look at the -f option)
    • tommy61157
      tommy61157 almost 6 years
      On the machine sending the files, just the 10.3.10.110, or both machines?
    • tommy61157
      tommy61157 almost 6 years
      Now the problem is when I use that command, it uses a LOT of inputs which I don't know how I could automate into a batch file later (all this has to be able to be automatically done by a .sh file with no user input too including putting in the password for the server, sorry, should've mentioned that, but that is partially why I asked this question too) I also have no clue if I put in the right inputs, I just hit enter when it asked for a directory and passphrase hoping it would go to the default directory with no passphrase.
    • tommy61157
      tommy61157 almost 6 years
      Is there not just an argument in ssh of some kind to where I could simply have it input the password that it'll need?
    • tommy61157
      tommy61157 almost 6 years
      I've actually used that link to get me to where I am pretty much, there's nothing on there that mentions anything about how to automate it to bypass the password as far as I can see, if I'm wrong, please correct me.
    • Terrance
      Terrance almost 6 years
      Where is it asking for a password? I am going to kill all my comments here and could you please update your question with examples of what you're running and where it is showing that it is asking for a password and other details.
    • tommy61157
      tommy61157 almost 6 years
      So, I listed all the details I could think of in Edits 2 and 2.5. This process has to be able to be made completely automatic for my specific use.
  • tommy61157
    tommy61157 almost 6 years
    Where does the password file go? The same directory you're running the command from or is there a directory in linux meant to store all the password files? Also, if it is a separate file, what should the file extension be or will it not have one?
  • MatsK
    MatsK almost 6 years
    As a good practice, I specify where the file is, for example /home/user/my-password-file. A file extension is not needed, that is windows thinking ;-)
  • tommy61157
    tommy61157 almost 6 years
    I couldn't quite get this option working, bash was complaining about how it wasn't an option or something weird like that, but I ended up coming up with my own solution, I upvoted your answer, but I have under 15 rep, so no dice unfortunately for it being displayed publicly. Thanks for helping to steer me in the right direction! :)
  • MatsK
    MatsK almost 6 years
    Sorry for leading you in the wrong direction, I see now that password-fileis to be used with the rsync daemon, not from a shell script or CLI.
  • tommy61157
    tommy61157 almost 6 years
    I guess I just got 15 rep, so I voted it up since it might help someone who might not mind doing that, it's not applicable to my situation, but might still be helpful to someone else, thanks for your input.