Making a bash script only prompt for password once?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bollweevil
    New Member
    • Apr 2009
    • 2

    Making a bash script only prompt for password once?

    Hello Everyone,

    I do Django web development on my Mac at home, and then I rsync the files with the Ubuntu web server. I want to write one single bash shell script that rsyncs the files and restarts Apache on the server.

    Here is the script I have so far, sync.sh:

    #!/bin/bash
    echo -n Password:
    read -s PW
    rsync --compress --times --perms --links --recursive --delete --include-from=incls.txt --exclude "*" /User/bollweevil/django-site/* awesomewebsite4 9.com:websites/scripts
    echo $PW | ssh awesomewebsite4 9.com sudo /etc/init.d/apache2 reload

    This script works, but it is kind of annoying because it prompts me for the SSH password twice. It prompts me for the SUDO password only once (lines 2 and 3 prompt, line 5 uses it), but it prompts me for the SSH password twice. I tried this:

    echo -n SSH Password:
    read -s SSH_PW
    echo $SSH_PW | rsync ...

    But it didn't work, it just prompted me for the SSH password again. It didn't print the $SSH_PW in cleartext to the screen.

    How can I make this work? How can I make it prompt me for the SSH password only once? In this script, it saves me a tiny bit of work. In a more complex script, it could save me a huge amount of work.

    Also, is this safe? I am never writing down the password in cleartext, but it is the root password for the web server, and it does exist as cleartext inside that variable. That variable would never do something stupid like reveal its contents during a traceback, would it?

    Thanks.
  • bollweevil
    New Member
    • Apr 2009
    • 2

    #2
    How to use "expect&qu ot; in a bash shell script?

    Hello Everyone,

    I wish I could edit my earlier post, but I can't. The earlier post asks how to make a bash script fill in the password when prompted. I found something that does exactly that: expect.

    Expect successfully fills in the password, BUT it breaks something else. Here is the new and improved code, in sync.sh:
    Code:
    #!/bin/bash
    echo -n Password:
    read -s PW
    expect -c 'spawn rsync --verbose --stats --progress --compress --times --perms --links --recursive --delete --include-from=incls.txt --exclude "*" /Users/bollweevil/django-site/* awesomewebsite49.com:websites/scripts' -c 'expect password:' -c 'send '$PW'\n' -c 'expect eof'
    So, let's invoke ./sync.sh
    It almost works. "rsync" gets called, it prompts for a password, then "expect" feeds it a password, and the remote server likes the password and accepts it. rsync starts to display its status. And then...
    Code:
    building file list ... 
    rsync: link_stat "/Users/bollweevil/django-site/*" failed: No such file or directory (2)
    0 files to consider
    What? What's wrong? Let's copy the "rsync ..." line out of the "spawn ..." statement, just a completely direct copy, and then paste it right into the shell:
    Code:
    new-host: bollweevil$ rsync --verbose --stats --progress --compress --times --perms --links --recursive --delete --include-from=incls.txt --exclude "*" /Users/bollweevil/django-site/* awesomewebsite49.com:websites/scripts
    building file list ... 
    45 files to consider
    What?! Why does it work perfectly now? I copied it exactly! Clearly, "expect" or "spawn" is doing crazy crap and messing things up.

    Please help.

    Thanks.
    Last edited by Nepomuk; Apr 19 '09, 09:10 AM. Reason: Please use [code] tags

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Try to use expect so it wont ask the password again.

      Thanks
      raghu

      Comment

      • ashitpro
        Recognized Expert Contributor
        • Aug 2007
        • 542

        #4
        You can automate ssh as well rsync...
        All you need to do is exchange secure key at both machines..
        Follow the link:

        Comment

        Working...