Env subprocess call

I am writing a python script as below:

#!/usr/bin/python import os, subprocess env = os.environ.copy() env['PASSWD'] = "hello jian" retcode = subprocess.call("smbclient -L //10.60.1.11 -U Mikejian"),env=env) print retcode 

how I execute it, it works fine, get retcode is 0, you know, I pass the password as an environment variable, so python will not ask me about it.

this is normal.

Now I want to move this code to MoinMoin (wiki engine) code. which is in the User class, as a function below

 def _validateSMBPassword(self, username, password): debug("call _validateSMBPassword %s:%s " % (username, password)) import subprocess, os, shlex cmd = "sudo smbclient -L //%(server)s -U %(user)s" cmd = cmd % { 'server' : "10.60.1.11", 'user' : "Mikejian", } env = os.environ.copy() env['PASSWD'] = "hello jian" retcode = subprocess.call("smbclient -L //10.60.1.11 -U Mikejian",env=env,shell=True) debug("retcode:%d" % retcode) if retcode == 0: return True, True return False, False 

in this case, I always got retcode equal to 1, not 0, it seemed that env did not pass the PASSWD environment variable.

Could you tell me why? and any solution?

+4
source share
1 answer

Can I shamelessly suggest pysmbclient ? This is a dirty hack that I wrote to replace some shell scripts at work, but it works fine for me.

 >>> smb = smbclient.SambaClient(server="MYSERVER", share="MYSHARE", ... username='foo', password='bar', domain='baz') ... >>> print smb.listdir("/") [u'file1.txt', u'file2.txt'] >>> f = smb.open('/file1.txt') >>> data = f.read() >>> f.close() >>> smb.rename(u'/file1.txt', u'/file1.old') 
0
source

Source: https://habr.com/ru/post/1339957/


All Articles