still trying to pass strings to the OS

Bart Nessux bart_nessux at hotmail.com
Wed Feb 25 15:30:39 EST 2004


Bart Nessux wrote:
> Cameron Laird wrote:
> 
>> In article <403A4B77.4090703 at hotmail.com>,
>> Bart Nessux  <bart_nessux at hotmail.com> wrote:
>>
>>> Diez B. Roggisch wrote:
>>>
>>>> Read this:
>>>>
>>>> http://pexpect.sourceforge.net/#faq
>>>>
>>>> Then you most probably want to use it :)
>>>>
>>>
>>> Thank you. That's exactly what I need.
>>>
>>
>>
>> If you can get your hands on a copy of the February 2004 issue of
>> *Sys Admin* Magazine <URL: http://www.samag.com/articles/2004/0402/ >,
>> you'll find a bit of encouragement there to pursue Pexpect.
> 
> 
> After downloading and installing pexpect... this works for me:
> 
> import pexpect
> 
> password = 'password'
> child = pexpect.spawn('/usr/bin/passwd root')
> child.expect_exact('New password:')
> child.sendline(password)
> child.expect_exact('Retype new password:')
> child.sendline(password)
> 
> Does this look safe? The documentation on the module was lacking. Seems 
> like I should close the process that was spawned. Any tips?
> 

This works 100% on Mac OSX 10.3.2... The time.sleeps had to be added to 
make it work consistently.

def set_pass():
    # Function to change root passwd on Mac 10.3 systems
    import pexpect
    import time
    set = r"password"
    child = pexpect.spawn("/usr/bin/passwd")
    child.expect_exact("New password:")
    child.sendline(set)
    time.sleep(0.1)
    child.expect_exact("Retype new password:")
    child.sendline(set)
    time.sleep(0.1)
    child.close()

set_pass()





More information about the Python-list mailing list