1

I am unable to pass server password with subprocess.Popen in Django. Here's my full views.py:

from django.shortcuts import render_to_response
from django.template import RequestContext
import subprocess

def upload_file(request):
    '''This function produces the form which allows user to input session_name, their remote host name, username 
    and password of the server. User can either save, load or cancel the form. Load will execute couple Linux commands
    that will list the files in their remote host and server.'''

    if request.method == 'POST':    
        # session_name = request.POST['session']
        url = request.POST['hostname']
        username = request.POST['username']
        global password
        password = request.POST['password']
        global source
        source = str(username) + "@" + str(url)

        command = subprocess.Popen(['rsync', '--list-only', source],
                           stdout=subprocess.PIPE, 
                           env={'RSYNC_PASSWORD': password}).communicate()[0]

        result1 = subprocess.Popen(['ls', '/home/zurelsoft/R'], stdout=subprocess.PIPE).communicate()[0]
        result = ''.join(result1)
        return render_to_response('thanks.html', {'res':result, 'res1':command}, context_instance=RequestContext(request))

    else:
        pass
    return render_to_response('form.html', {'form': 'form'},  context_instance=RequestContext(request))  

Here's the form that I take user input from:

<fieldset>
            <legend>Session</legend>
                <label for="input-one" class="float"><strong>Session Name:</strong></label><br />
                <input class="inp-text" name="session" id="sess" type="text" size="30" /><br />

                <label for="input-two" class="float"><strong>RemoteHost:</strong></label><br />
                <input class="inp-text" name="hostname"  id="host" type="text" size="30" />

                <label for="input-three" class="float"><strong>Username:</strong></label><br />
                <input class="inp-text" name="username"  id="user" type="text" size="30" />

                <label for="input-four" class="float"><strong>Password:</strong></label><br />
                <input class="inp-text" name="password"  id="pass" type="password" size="30" />
        </fieldset>

What am I doing wrong? Password is not passed from environment_variable.

Joyfulgrind
  • 2,762
  • 8
  • 34
  • 41

1 Answers1

1

Your code (as far as setting the password in RSYNC_PASSWORD) is correct.

My conclusion is that you are trying to connect to a server with a different module in Rsync like ssh. In the case of ssh the RSYNC_PASSWORD variable does not work.

Perhaps it's a good idea to use fabric for the command execution? It can handle a few things for you although it won't solve your password problem. If you are connecting through ssh than I would recommend setting up private key authentication.

See this question for more info on passwordless rsync through ssh: How to automate rsync without asking for password prompt

Community
  • 1
  • 1
Wolph
  • 78,177
  • 11
  • 137
  • 148
  • Thank you for your answer. As you can see, I am not using ssh to connect to the server. Rsync connects to me to the server and transfers the file. How can I solve my problem? – Joyfulgrind Nov 20 '12 at 08:39
  • @sachitad: do you have an rsync server running on the remote server? If not, than it's highly likely that you are indeed using ssh. By default rsync uses ssh when connecting to remote systems. – Wolph Nov 21 '12 at 00:45
  • I got that. What might be the solution to fix my problem? – Joyfulgrind Nov 21 '12 at 03:55
  • Yes, rsync is avaiable on both local and remote machines. – Joyfulgrind Nov 21 '12 at 03:57
  • @sachitad: there are 2 possible solutions. 1. use public/private key authentication so you don't need a password. 2. set up a rsyncd server so you don't need a password or can pass the password like this. I would recommend setting up a public/private key (http://www.ece.uci.edu/~chou/ssh-key.html) – Wolph Nov 21 '12 at 16:32
  • While setting up public/private key, I can only set it up on the server where files will be transferred beacase the client will enter their password in a form. Is that possible? – Joyfulgrind Nov 22 '12 at 03:34
  • After setting up rsync server, can I pass password like I did in my code? – Joyfulgrind Nov 22 '12 at 05:36
  • @sachitad: with public/private key you will need to have the public key on the remote server and a passwordless private key on the connecting server. If you would be using a password for key authentication than you would have the same problem. – Wolph Nov 22 '12 at 16:06
  • @sachitad: alternatively... fabric has the ssh code wrapped so you might be able to use that: http://stackoverflow.com/questions/2339735/fabric-password – Wolph Nov 22 '12 at 16:10