gpt4 book ai didi

linux - 为 Linux "adduser"命令创建 Expect 脚本

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:55 25 4
gpt4 key购买 nike

我是 Expect 的新手,我认为我应该首先在我的本地机器上试用 expect 来尝试一下。

为此,我想尝试针对“adduser”命令运行 Expect。

我无法让它正常工作,似乎一切都在正常运行,我能够到达最后一个提示,它确实退出了,但用户仍未创建。

我做错了什么?

#/usr/bin/expect -f 
spawn adduser testuser

expect "Enter new UNIX password"
send "testpassword\r"

expect "Retype new UNIX password"
send "testpassword\r"

expect " Full Name []: "
send "\r"

expect " Room Number []: "
send "\r"

expect " Work Phone []: "
send "\r"

expect " Home Phone []: "
send "\r"

expect " Other []: "
send "\r"

expect "Is the information correct? \[Y/n\] "
send "\r"

最佳答案

将以下内容保存为 'automateUser' ... chmod 775 ... 然后在命令行上。

sudo expect -d automateUser

#!/usr/bin/expect

spawn adduser testuser

expect "Enter new UNIX password" { send "testpass\r" }
expect "Retype new UNIX password" { send "testpass\r" }
expect "Full Name" { send "\r" }
expect "Room Number" { send "\r" }
expect "Work Phone" { send "\r" }
expect "Home Phone" { send "\r" }
expect "Other" { send "\r" }
expect "correct?" { send "y\r" }

interact

如果您在之后键入 less/etc/passwd,您应该会在列表中看到您的测试用户。

  • 添加 -d 开关可以使 expect 的行为更可预测,并且非常适合调试
  • expect-s 可以是精确的或松散的(例如,完全匹配与“包含”)
  • 某些操作可能需要 root ... 比如 adduser

针对 Ubuntu 16.04 和 Expect 版本 5.45 进行了测试。

关于linux - 为 Linux "adduser"命令创建 Expect 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39609285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com