changing password using pexpect and passwd non-interactivly?










0















I am trying to learn python to change users password non-interactively but nothing seems to be working.
pexepect module of python seems promising, so I am just trying to use it.

This tutorial is nice but it does not work.

There's lots of code on Internet regarding this but none of them seems to work.

And nor does my code:



#!/usr/bin/python
import pexpect
import time
def ChangePassword(user, pass):
passwd = pexpect.spawn("/usr/bin/passwd %s" % user)

for x in xrange(2):
# wait for password: to come out of passwd's stdout
passwd.expect("password: ")
# send pass to passwd's stdin
passwd.sendline(pass)
time.sleep(0.1)

ChangePassword('rajesh', 'bar') # changes user "foo"'s password to "bar"


Error:



bash-3.00# ./solpas7.py
File "./solpas7.py", line 4
def ChangePassword(user, pass):
^
SyntaxError: invalid syntax


EDIT : i changed pass to pa but not i am getting a lot and the password does not changes.



bash-3.00# ./solpas7.py
Traceback (most recent call last):
File "./solpas7.py", line 14, in ?
ChangePassword('rajesh', 'bar') # changes user "foo"'s password to "bar"
File "./solpas7.py", line 9, in ChangePassword
passwd.expect("password: ")
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1311, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1325, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize )
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1409, in expect_loop
raise TIMEOUT (str(e) + 'n' + str(self))
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
<pexpect.spawn object at 0x80e306c>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/passwd
args: ['/usr/bin/passwd', 'rajesh']
searcher: searcher_re:
0: re.compile("password: ")
buffer (last 100 chars): New Password:
before (last 100 chars): New Password:
after: pexpect.TIMEOUT
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 4683
child_fd: 3
closed: False
timeout: 30
delimiter: pexpect.EOF
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1









share|improve this question
























  • pass is a reserved keyword ;) Using something else will at least remove the syntax error.

    – akaIDIOT
    Jan 30 '13 at 11:41















0















I am trying to learn python to change users password non-interactively but nothing seems to be working.
pexepect module of python seems promising, so I am just trying to use it.

This tutorial is nice but it does not work.

There's lots of code on Internet regarding this but none of them seems to work.

And nor does my code:



#!/usr/bin/python
import pexpect
import time
def ChangePassword(user, pass):
passwd = pexpect.spawn("/usr/bin/passwd %s" % user)

for x in xrange(2):
# wait for password: to come out of passwd's stdout
passwd.expect("password: ")
# send pass to passwd's stdin
passwd.sendline(pass)
time.sleep(0.1)

ChangePassword('rajesh', 'bar') # changes user "foo"'s password to "bar"


Error:



bash-3.00# ./solpas7.py
File "./solpas7.py", line 4
def ChangePassword(user, pass):
^
SyntaxError: invalid syntax


EDIT : i changed pass to pa but not i am getting a lot and the password does not changes.



bash-3.00# ./solpas7.py
Traceback (most recent call last):
File "./solpas7.py", line 14, in ?
ChangePassword('rajesh', 'bar') # changes user "foo"'s password to "bar"
File "./solpas7.py", line 9, in ChangePassword
passwd.expect("password: ")
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1311, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1325, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize )
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1409, in expect_loop
raise TIMEOUT (str(e) + 'n' + str(self))
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
<pexpect.spawn object at 0x80e306c>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/passwd
args: ['/usr/bin/passwd', 'rajesh']
searcher: searcher_re:
0: re.compile("password: ")
buffer (last 100 chars): New Password:
before (last 100 chars): New Password:
after: pexpect.TIMEOUT
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 4683
child_fd: 3
closed: False
timeout: 30
delimiter: pexpect.EOF
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1









share|improve this question
























  • pass is a reserved keyword ;) Using something else will at least remove the syntax error.

    – akaIDIOT
    Jan 30 '13 at 11:41













0












0








0








I am trying to learn python to change users password non-interactively but nothing seems to be working.
pexepect module of python seems promising, so I am just trying to use it.

This tutorial is nice but it does not work.

There's lots of code on Internet regarding this but none of them seems to work.

And nor does my code:



#!/usr/bin/python
import pexpect
import time
def ChangePassword(user, pass):
passwd = pexpect.spawn("/usr/bin/passwd %s" % user)

for x in xrange(2):
# wait for password: to come out of passwd's stdout
passwd.expect("password: ")
# send pass to passwd's stdin
passwd.sendline(pass)
time.sleep(0.1)

ChangePassword('rajesh', 'bar') # changes user "foo"'s password to "bar"


Error:



bash-3.00# ./solpas7.py
File "./solpas7.py", line 4
def ChangePassword(user, pass):
^
SyntaxError: invalid syntax


EDIT : i changed pass to pa but not i am getting a lot and the password does not changes.



bash-3.00# ./solpas7.py
Traceback (most recent call last):
File "./solpas7.py", line 14, in ?
ChangePassword('rajesh', 'bar') # changes user "foo"'s password to "bar"
File "./solpas7.py", line 9, in ChangePassword
passwd.expect("password: ")
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1311, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1325, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize )
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1409, in expect_loop
raise TIMEOUT (str(e) + 'n' + str(self))
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
<pexpect.spawn object at 0x80e306c>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/passwd
args: ['/usr/bin/passwd', 'rajesh']
searcher: searcher_re:
0: re.compile("password: ")
buffer (last 100 chars): New Password:
before (last 100 chars): New Password:
after: pexpect.TIMEOUT
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 4683
child_fd: 3
closed: False
timeout: 30
delimiter: pexpect.EOF
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1









share|improve this question
















I am trying to learn python to change users password non-interactively but nothing seems to be working.
pexepect module of python seems promising, so I am just trying to use it.

This tutorial is nice but it does not work.

There's lots of code on Internet regarding this but none of them seems to work.

And nor does my code:



#!/usr/bin/python
import pexpect
import time
def ChangePassword(user, pass):
passwd = pexpect.spawn("/usr/bin/passwd %s" % user)

for x in xrange(2):
# wait for password: to come out of passwd's stdout
passwd.expect("password: ")
# send pass to passwd's stdin
passwd.sendline(pass)
time.sleep(0.1)

ChangePassword('rajesh', 'bar') # changes user "foo"'s password to "bar"


Error:



bash-3.00# ./solpas7.py
File "./solpas7.py", line 4
def ChangePassword(user, pass):
^
SyntaxError: invalid syntax


EDIT : i changed pass to pa but not i am getting a lot and the password does not changes.



bash-3.00# ./solpas7.py
Traceback (most recent call last):
File "./solpas7.py", line 14, in ?
ChangePassword('rajesh', 'bar') # changes user "foo"'s password to "bar"
File "./solpas7.py", line 9, in ChangePassword
passwd.expect("password: ")
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1311, in expect
return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1325, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize )
File "/usr/lib/python2.4/site-packages/pexpect.py", line 1409, in expect_loop
raise TIMEOUT (str(e) + 'n' + str(self))
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
<pexpect.spawn object at 0x80e306c>
version: 2.3 ($Revision: 399 $)
command: /usr/bin/passwd
args: ['/usr/bin/passwd', 'rajesh']
searcher: searcher_re:
0: re.compile("password: ")
buffer (last 100 chars): New Password:
before (last 100 chars): New Password:
after: pexpect.TIMEOUT
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 4683
child_fd: 3
closed: False
timeout: 30
delimiter: pexpect.EOF
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 30 '13 at 11:50







munish

















asked Jan 30 '13 at 11:38









munishmunish

1,578124171




1,578124171












  • pass is a reserved keyword ;) Using something else will at least remove the syntax error.

    – akaIDIOT
    Jan 30 '13 at 11:41

















  • pass is a reserved keyword ;) Using something else will at least remove the syntax error.

    – akaIDIOT
    Jan 30 '13 at 11:41
















pass is a reserved keyword ;) Using something else will at least remove the syntax error.

– akaIDIOT
Jan 30 '13 at 11:41





pass is a reserved keyword ;) Using something else will at least remove the syntax error.

– akaIDIOT
Jan 30 '13 at 11:41












2 Answers
2






active

oldest

votes


















3














You can't use pass as a variable name. It's a reserved keyword.



Edit:
pexpect is waiting for the string "password: " but as you can tell from the error message passwd outputs "New Password: " (note the capital p) on your system.



buffer (last 100 chars): New Password:
before (last 100 chars): New Password:


Instead of passwd.expect("password: ") you need to use passwd.expect("Password: ").






share|improve this answer

























  • this is what i did just now bash-3.00# passwd rajesh New Password: Re-enter new Password: passwd: password successfully changed for rajesh

    – munish
    Jan 30 '13 at 13:44











  • is it looking for the string New Password: and Re-enter new Password:`

    – munish
    Jan 30 '13 at 13:45











  • no i just used the usual passwd command on command line

    – munish
    Jan 30 '13 at 13:46











  • See my answer. I just updated it.

    – lukad
    Jan 30 '13 at 13:46












  • ,thanks a lot, it works. i need to learn a lot more

    – munish
    Jan 30 '13 at 13:49


















0














You can also use this to make your search case insensitive:



passwd.expect('(?i)password:') 





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14603104%2fchanging-password-using-pexpect-and-passwd-non-interactivly%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You can't use pass as a variable name. It's a reserved keyword.



    Edit:
    pexpect is waiting for the string "password: " but as you can tell from the error message passwd outputs "New Password: " (note the capital p) on your system.



    buffer (last 100 chars): New Password:
    before (last 100 chars): New Password:


    Instead of passwd.expect("password: ") you need to use passwd.expect("Password: ").






    share|improve this answer

























    • this is what i did just now bash-3.00# passwd rajesh New Password: Re-enter new Password: passwd: password successfully changed for rajesh

      – munish
      Jan 30 '13 at 13:44











    • is it looking for the string New Password: and Re-enter new Password:`

      – munish
      Jan 30 '13 at 13:45











    • no i just used the usual passwd command on command line

      – munish
      Jan 30 '13 at 13:46











    • See my answer. I just updated it.

      – lukad
      Jan 30 '13 at 13:46












    • ,thanks a lot, it works. i need to learn a lot more

      – munish
      Jan 30 '13 at 13:49















    3














    You can't use pass as a variable name. It's a reserved keyword.



    Edit:
    pexpect is waiting for the string "password: " but as you can tell from the error message passwd outputs "New Password: " (note the capital p) on your system.



    buffer (last 100 chars): New Password:
    before (last 100 chars): New Password:


    Instead of passwd.expect("password: ") you need to use passwd.expect("Password: ").






    share|improve this answer

























    • this is what i did just now bash-3.00# passwd rajesh New Password: Re-enter new Password: passwd: password successfully changed for rajesh

      – munish
      Jan 30 '13 at 13:44











    • is it looking for the string New Password: and Re-enter new Password:`

      – munish
      Jan 30 '13 at 13:45











    • no i just used the usual passwd command on command line

      – munish
      Jan 30 '13 at 13:46











    • See my answer. I just updated it.

      – lukad
      Jan 30 '13 at 13:46












    • ,thanks a lot, it works. i need to learn a lot more

      – munish
      Jan 30 '13 at 13:49













    3












    3








    3







    You can't use pass as a variable name. It's a reserved keyword.



    Edit:
    pexpect is waiting for the string "password: " but as you can tell from the error message passwd outputs "New Password: " (note the capital p) on your system.



    buffer (last 100 chars): New Password:
    before (last 100 chars): New Password:


    Instead of passwd.expect("password: ") you need to use passwd.expect("Password: ").






    share|improve this answer















    You can't use pass as a variable name. It's a reserved keyword.



    Edit:
    pexpect is waiting for the string "password: " but as you can tell from the error message passwd outputs "New Password: " (note the capital p) on your system.



    buffer (last 100 chars): New Password:
    before (last 100 chars): New Password:


    Instead of passwd.expect("password: ") you need to use passwd.expect("Password: ").







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 30 '13 at 13:47

























    answered Jan 30 '13 at 11:43









    lukadlukad

    10.4k32944




    10.4k32944












    • this is what i did just now bash-3.00# passwd rajesh New Password: Re-enter new Password: passwd: password successfully changed for rajesh

      – munish
      Jan 30 '13 at 13:44











    • is it looking for the string New Password: and Re-enter new Password:`

      – munish
      Jan 30 '13 at 13:45











    • no i just used the usual passwd command on command line

      – munish
      Jan 30 '13 at 13:46











    • See my answer. I just updated it.

      – lukad
      Jan 30 '13 at 13:46












    • ,thanks a lot, it works. i need to learn a lot more

      – munish
      Jan 30 '13 at 13:49

















    • this is what i did just now bash-3.00# passwd rajesh New Password: Re-enter new Password: passwd: password successfully changed for rajesh

      – munish
      Jan 30 '13 at 13:44











    • is it looking for the string New Password: and Re-enter new Password:`

      – munish
      Jan 30 '13 at 13:45











    • no i just used the usual passwd command on command line

      – munish
      Jan 30 '13 at 13:46











    • See my answer. I just updated it.

      – lukad
      Jan 30 '13 at 13:46












    • ,thanks a lot, it works. i need to learn a lot more

      – munish
      Jan 30 '13 at 13:49
















    this is what i did just now bash-3.00# passwd rajesh New Password: Re-enter new Password: passwd: password successfully changed for rajesh

    – munish
    Jan 30 '13 at 13:44





    this is what i did just now bash-3.00# passwd rajesh New Password: Re-enter new Password: passwd: password successfully changed for rajesh

    – munish
    Jan 30 '13 at 13:44













    is it looking for the string New Password: and Re-enter new Password:`

    – munish
    Jan 30 '13 at 13:45





    is it looking for the string New Password: and Re-enter new Password:`

    – munish
    Jan 30 '13 at 13:45













    no i just used the usual passwd command on command line

    – munish
    Jan 30 '13 at 13:46





    no i just used the usual passwd command on command line

    – munish
    Jan 30 '13 at 13:46













    See my answer. I just updated it.

    – lukad
    Jan 30 '13 at 13:46






    See my answer. I just updated it.

    – lukad
    Jan 30 '13 at 13:46














    ,thanks a lot, it works. i need to learn a lot more

    – munish
    Jan 30 '13 at 13:49





    ,thanks a lot, it works. i need to learn a lot more

    – munish
    Jan 30 '13 at 13:49













    0














    You can also use this to make your search case insensitive:



    passwd.expect('(?i)password:') 





    share|improve this answer



























      0














      You can also use this to make your search case insensitive:



      passwd.expect('(?i)password:') 





      share|improve this answer

























        0












        0








        0







        You can also use this to make your search case insensitive:



        passwd.expect('(?i)password:') 





        share|improve this answer













        You can also use this to make your search case insensitive:



        passwd.expect('(?i)password:') 






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 18:11









        D.FitzD.Fitz

        34




        34



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14603104%2fchanging-password-using-pexpect-and-passwd-non-interactivly%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo