首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为Linux "adduser“命令创建Expect脚本

为Linux "adduser“命令创建Expect脚本
EN

Stack Overflow用户
提问于 2016-09-21 06:48:27
回答 3查看 840关注 0票数 1

说到期待,我是个新手,我想我会先在我的本地机器上试用Expect,这样我就会把脚弄湿了。

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

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

我做错了什么?

代码语言:javascript
复制
#/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"
EN

回答 3

Stack Overflow用户

发布于 2019-04-07 22:24:24

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

数独期待-d automateUser

代码语言:javascript
复制
#!/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

如果在之后键入了较少的/etc/passwd,则应该在列表中看到您的testuser。

  • 添加-d开关可以使expect的行为更加可预测,而且它对调试非常有用
  • expect-s可以是精确的,也可以是松散的(例如,精确匹配vs 'contains')
  • 某些操作可能需要根..。类似加载项

使用Expect版本5.45对Ubuntu16.04进行了测试。

票数 1
EN

Stack Overflow用户

发布于 2016-09-21 19:14:53

看起来所有这些都是错误的:expect " Something []: "

我会用expect -ex {Something []: }

  1. 它使用“精确”匹配,因为您不需要glob或regex模式的强大功能,而且
  2. 大括号确保括号不被解释为Tcl命令替换语法。
票数 0
EN

Stack Overflow用户

发布于 2022-03-08 10:21:28

expect "Is the information correct? \[Y/n\]是你的问题。

删除Y/n

expect "Is the information correct? send "y\r"

您在代码中丢失了y或n,如果不删除Y/N,也会出现错误。

我这样做(用我的覆盆子):

代码语言:javascript
复制
#!usr/bin/expect

spawn sudo adduser [lindex argv 0]

set password [lindex argv 1]

#depends what os ur are using

expect "xy something about new passwort:"

send "$password\r"

#depends what os ur are using

expect "blabla type it again blabla:"

send "$password\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?"

send "y\r"

这样,您就不需要将密码写入脚本中。

代码语言:javascript
复制
chmod +x 'yourscriptname'
代码语言:javascript
复制
execute 'yourscriptname' 'newusername' 'pwfornewuser'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39609285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档