Same code cause the different result.

Kushal Kumaran kushal.kumaran+python at gmail.com
Wed Apr 25 05:20:39 EDT 2012


On Wed, Apr 25, 2012 at 12:21 PM, 叶佑群 <ye.youqun at eisoo.com> wrote:
> Hi, all
>
>    I have code as:
>
>             pobj = subprocess.Popen (["smbpasswd", user], stdin
> =subprocess.PIPE)
>             password += "\n"
>             pobj.stdin.write (password)
>             pobj.stdin.write (password)
>
>     the command smbpasswd will change the samba user's password, In shell
> this will run as below:
>
>             [root at localhost ~]# smbpasswd mytest1
>             New SMB password:
>             Retype new SMB password:
>             [root at localhost ~]#
>
>     but in python code, it always prompt to wait input the password, it is
> seems that pobj.stdin.write () doesn't work. It is anything wrong with my
> code? I have another block code that runs as expected:
>

Maybe smbpasswd does not read the password from stdin.  Some programs
prefer to open the terminal /dev/tty directly and read from it.  You
will not be able to use subprocess directly with such programs.  You
have some options:

- find out if the smbpasswd has an option that will make it read the
password from stdin

- use an expect-like library, such as pexpect

-- 
regards,
kushal



More information about the Python-list mailing list