简体   繁体   English

非交互地使用pexpect和passwd更改密码?

[英]changing password using pexpect and passwd non-interactivly?

I am trying to learn python to change users password non-interactively but nothing seems to be working. 我正在尝试学习python以非交互方式更改用户密码,但是似乎没有任何效果。
pexepect module of python seems promising, so I am just trying to use it. python的pexepect模块似乎很有前途,所以我只是尝试使用它。
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. 编辑:我将通行证更改为pa,但不是很多,密码也没有更改。

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

You can't use pass as a variable name. 您不能将pass用作变量名。 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. 编辑:pexpect正在等待字符串"password: "但是正如您可以从错误消息中看出的那样, passwd输出"New Password: " (注意大写字母p)。

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

Instead of passwd.expect("password: ") you need to use passwd.expect("Password: ") . 代替passwd.expect("password: ")您需要使用passwd.expect("Password: ")

您还可以使用此选项使搜索不区分大小写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM