dsxfa26482 2018-07-17 14:01
浏览 273
已采纳

使用os.exec执行'su'返回错误

As I understand it Go's exec function executes the command directly with the kernel, rather than creating a local a terminal session or something like that. Unfortunately, I need to use the su command in order to run a script, which involves running su then piping the root password in via stdin.

Yes I have to use su (business use case on devices with sudo disabled), and I also do need to be able to pipe the password in.

So far the code I have is as follows

rootRun := exec.Command("su", "-c", "whoami")

rootRun.Stderr = os.Stdout
rootRun.Stdout = os.Stdout

err = rootRun.Run()
check(err)

and the error is

su: must be run from a terminal
panic: exit status 1

The only way I can think to do this would be to somehow emulate a terminal on the machine with go (I dont know what packages could be used, please let me know if you know of one) or create some kind of local ssh connection that gives me a pty session to work with, but thats not great as the devices will sometimes have ssh disabled. Hopefully there is another way around this, please let me know.

  • 写回答

1条回答 默认 最新

  • dongzanxun2790 2018-07-17 14:53
    关注

    Try running it so that it's connected to a pseudo TTY.

    github.com/kr/pty and other packages may help with this.


    I must recap what others have written in their respective comments that this practice is utterly and irreversively broken.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试