在 Shell 脚本终端中实现自动化
我的天啊!我已经尝试了所有关于期望的事情。我想要做的是创建一个脚本,在终端中更改为 root 时自动输入密码。我的代码看起来像这样的
expect -i
spawn sudo pmset schedule sleep "{query}" expect { Password: send "password\r" }; interact;
exit
结果,
expect1.1> spawn sudo pmset schedule sleep "06/19/11 23:40:00" expect { Password: echo "lovegun\r" }; interact; exit
spawn sudo pmset schedule sleep 06/19/11 23:40:00 expect Password: send "password\r"
Password:
我被密码困住了: 有人可以看一下我的代码吗?这有什么问题呢。
expect -c '
proc abort {} {
puts "You have Authenticated\n"
exit 1
}
spawn sudo pmset schedule sleep "{query}"
expect {
Password: { send "password\n" }
default abort
}
puts "Finished OK\n"
'
终于我找到了答案
Oh my god! I have tried everything about expect. What I want to do is create a script to automate input password when changing to root in terminal. My code look like this
expect -i
spawn sudo pmset schedule sleep "{query}" expect { Password: send "password\r" }; interact;
exit
result
expect1.1> spawn sudo pmset schedule sleep "06/19/11 23:40:00" expect { Password: echo "lovegun\r" }; interact; exit
spawn sudo pmset schedule sleep 06/19/11 23:40:00 expect Password: send "password\r"
Password:
I am stuck in password:
Can somebody look at my code? What is wrong with this.
expect -c '
proc abort {} {
puts "You have Authenticated\n"
exit 1
}
spawn sudo pmset schedule sleep "{query}"
expect {
Password: { send "password\n" }
default abort
}
puts "Finished OK\n"
'
Finally I found the answer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要将密码隐藏在 Expect 脚本中,您可以 1) 更改 pmset 以运行 set-user-id 这样您就不必使用 sudo,或者 2) 给自己 NOPASSWD 访问 sudo 的权限,这样您就可以不需要输入密码。无论哪种方式都可以让您不必将密码存储在文件中。
If you're going to have your password buried in an expect script, you could either 1) change pmset to run set-user-id so you don't have to use sudo, or 2) give yourself NOPASSWD access to sudo so you aren't required to enter a password. Either way would keep you from having to store your password in a file.