在 Haskell 中运行 sudo 命令
我的笔记本电脑上安装了 ghc 6.12.3 和 Ubuntu 11.04。
我想要一个函数,它接受一些 shell 命令并在 Haskell 中以超级用户身份执行它们(如 sudo update-manager、sudo iwlist ....)。我知道 System.Process 模块有一些函数,如 createProcess、runInteractiveCommand。但有单个原始命令或单个 shell 命令,而不是像“sudo update-manager”这样的复合命令。我对这些执行“sudo ...”的函数的所有实验都失败了。我用来运行 haskell 函数的终端没有响应。
我还查看了 HSH 包。但在我看来,导出的函数也不适合 sudo 命令。
我的猜测是,执行“sudo update-manager”等命令需要两个进程。一个用于“sudo”,另一个用于“update-manager”。因此,我需要调用“createProcess”等函数两次,并以某种方式连接它们,以便“update-manager”的第二个进程从“sudo”的第一个进程获得超级用户权限。
预先感谢您的帮助!
I am having ghc 6.12.3 and Ubuntu 11.04 installed on my laptop.
I would like to have a function which take some shell commands and execute them as the superuser (like sudo update-manager, sudo iwlist ....) in Haskell. I know that the System.Process module have some functions like createProcess, runInteractiveCommand. But there are for a single raw command or a single shell command, not for compound commnads like "sudo update-manager". All my experiments on those functions to execute "sudo ..." failed. The terminal I used to run my haskell function had no response.
I also looked at HSH package. But it seems to me that functions exported there are not good for sudo commands either.
My guess is that executing commands like "sudo update-manager" requires two process. One is for "sudo" and the other one is for "update-manager". So I need to call functions like "createProcess" twice and somehow connect them so that the second process for "update-manager" get superuser privilege from the first process for "sudo".
Thanks in advance for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试从
System.Process
读取readProcess
像这样运行它:
这将使用选项
-S
和程序执行sudo
。从标准输入读取密码需要-S
。密码必须以换行符结尾,因此程序会添加一个换行符。Try
readProcess
fromSystem.Process
Run it like this:
This executes
sudo
with the options-S
and the program.-S
is needed to read the password from stdin. The password must finish with a newline, so the program adds one.回答最后一段。
sudo
是一个常规程序,没有任何魔法。它只是碰巧运行其他程序。你的 Haskell 程序也是如此。您的程序运行sudo
并且sudo
运行update-manager
所以不,您不应该创建两个进程。Answering the last paragraph.
sudo
is a regular program, no magic whatsoever. It just happens to run other programs. So does your Haskell program. Your program runssudo
andsudo
runsupdate-manager
So no, you should not create two processes.您尝试过 System.Process.system 吗?
这对我有用(GHC 7.0.3)。另外,对于一般的 Haskell 脚本(包括 sudo),您可以查看演示文稿 “实用 Haskell:使用类型编写脚本”,作者:Don Stewart。
Have you tried System.Process.system?
This works for me (GHC 7.0.3). Also, for scripting in Haskell in general (sudo included), you can have a look at a presentation "Practical Haskell: scripting with types" by Don Stewart.