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

使用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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题