Connecting Tech Pros Worldwide Help | Site Map

Making a bash script only prompt for password once?

Newbie
 
Join Date: Apr 2009
Posts: 2
#1: Apr 6 '09
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/* awesomewebsite49.com:websites/scripts
echo $PW | ssh awesomewebsite49.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.
Newbie
 
Join Date: Apr 2009
Posts: 2
#2: Apr 6 '09

re: Making a bash script only prompt for password once?


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:
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2. echo -n Password:
  3. read -s PW
  4. 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'
  5.  
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...
Expand|Select|Wrap|Line Numbers
  1. building file list ... 
  2. rsync: link_stat "/Users/bollweevil/django-site/*" failed: No such file or directory (2)
  3. 0 files to consider
  4.  
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:
Expand|Select|Wrap|Line Numbers
  1. 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
  2. building file list ... 
  3. 45 files to consider
  4.  
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.
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,256
#3: Apr 7 '09

re: Making a bash script only prompt for password once?


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

Thanks
raghu
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#4: Apr 7 '09

re: Making a bash script only prompt for password once?


You can automate ssh as well rsync...
All you need to do is exchange secure key at both machines..
Follow the link:
http://www.astro.umd.edu/~teuben/sirtf/ssh.html
Reply


Similar Unix / Linux / BSD bytes