说到期待,我是个新手,我想我会先在我的本地机器上试用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"发布于 2019-04-07 22:24:24
将以下内容保存为“automateUser”。chmod 775。然后在命令行上。
数独期待-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如果在之后键入了较少的/etc/passwd,则应该在列表中看到您的testuser。
使用Expect版本5.45对Ubuntu16.04进行了测试。
发布于 2016-09-21 19:14:53
看起来所有这些都是错误的:expect " Something []: "
我会用expect -ex {Something []: }
发布于 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,也会出现错误。
我这样做(用我的覆盆子):
#!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"这样,您就不需要将密码写入脚本中。
chmod +x 'yourscriptname'execute 'yourscriptname' 'newusername' 'pwfornewuser'https://stackoverflow.com/questions/39609285
复制相似问题