【问题标题】:Set OS X password using expect使用期望设置 OS X 密码
【发布时间】:2015-10-23 19:20:58
【问题描述】:

我正在尝试编写一个脚本来将 OS X 帐户的密码更新为一个轮换的、集中存储的值。作为学习使用 tclcurl 的前奏,我只想让这个原型脚本工作:

#!/usr/bin/expect

set mgrpassword "newpassword"  # this will become a tclcurl command later

spawn passwd manager
expect "New password:"
send "$mgrpassword\n"
expect "Retype new password:"
send "$mgrpassword\n"

puts "\nManager password changed."

exit 0

它运行没有错误,但它什么也不做;经理帐户的密码保持不变。我用 \r 和 \n 都试过了,但这没有任何区别。谁能看到我做错了什么或我省略了哪些步骤?

(它将始终以管理员权限运行;这就是为什么没有 'expect "Old password:"' 行。)

【问题讨论】:

    标签: macos shell expect change-password


    【解决方案1】:

    只需在最后添加一个expect 语句,如下所示,

    send "$mgrpassword\r"
    expect eof
    

    基本上,Expect 将使用两个可行的命令,例如 sendexpect。如果使用send,那么之后必须使用expect(在大多数情况下)。 (反之亦然不是强制性的)

    这是因为如果不这样做,我们将错过生成过程中发生的事情,因为Expect 会假设您只需要发送一个字符串值,而不期待会话中的任何其他内容。

    您的脚本也可以用以下方式编写,这使得使用exp_continue 时更加健壮。这将使Expect 再次运行。

    set mgrpassword "newpassword"
    spawn passwd manager
    expect {
            timeout { puts "Timeout happened";exit 0}
            "password:" {send "$mgrpassword \r";exp_continue}
            eof {puts "Manager password changed"; exit 1}
    }
    

    【讨论】:

      【解决方案2】:

      原来如此

      dscl . passwd /Users/manager [passwordstring] 
      

      比尝试将 passwd 与 expect 结合使用 很多 更好/更容易。希望这对其他人有帮助。

      【讨论】:

        猜你喜欢
        • 2011-09-22
        • 1970-01-01
        • 2011-10-31
        • 2020-03-08
        • 1970-01-01
        • 1970-01-01
        • 2010-10-02
        • 2011-09-27
        相关资源
        最近更新 更多