1

I've been trying to communicate with a program (smbpasswd) using Python's subprocess module without much success. I can communicate with a different program, (e.g. grep) without any trouble, so the problem is specific to smbpasswd. To illustrate the problem, the following code works:

>>> p = Popen(["grep", "n"], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
>>> stdout = p.communicate(input='one\ntwo\n')[0]
>>> print(stdout)
one

However, the following does not:

>>> p = Popen(["smbpasswd", "-r", server, "-U", user], stdout=PIPE, stdin=PIPE, stderr=PIPE)
>>> stdout = p.communicate(input='old_password')[0]
>>> print(stdout)

The difference is that grep waits for user input after calling the initial "grep f", while smbpasswd prompts the user to input their old password ("Old SMB password:" is displayed if you run the command in a terminal) before looking for input.

This is, however, where my knowledge ends. Any help is greatly appreciated!

vyegorov
  • 21,787
  • 7
  • 59
  • 73
Donagh
  • 165
  • 2
  • 2
  • 6
  • Related: http://stackoverflow.com/questions/12009/piping-password-to-smbpasswd – ChristopheD May 11 '12 at 16:14
  • Utilities that work with passwords typically do not use `stdin`, they open a direct connection to a terminal for security reasons. Take a look at expect. – geekosaur May 11 '12 at 16:14

2 Answers2

1

You can use pexpect to achieve what I want. I am pretty sure smbpassword opens like su -c a PTY and you can't use subprocess to communicate through a PTY.

dav1d
  • 5,917
  • 1
  • 33
  • 52
0

You need to tell smbpasswd to use stdin for the password prompt. From the help text:

When run by root:
    smbpasswd [options] [username]
otherwise:
    smbpasswd [options]

options:
  -s                   use stdin for password prompt
JoeFish
  • 3,009
  • 1
  • 18
  • 23