dongtou2097 2014-09-08 16:51
浏览 319
已采纳

在Golang中使用Sudosh

my programs purpose is to do some tasks and then sudosh as a user all the way at the end.

However when running this with Go I get the sudosh: couldn't get your controlling terminal.

Is this because sudosh is looking for a controlling terminal and can't find it? Is this possible to do with Go? I am converting a Python script over to this Go program and it worked fine in Python.

import (
    "github.com/codeskyblue/go-sh"
    "fmt"
    "os"
)
c, _ := sh.Command("sudo", "sudosh", "bob").Output()
fmt.Println(c)
os.Exit(0)

sudosh: couldn't get your controlling terminal.

The Fix

import "os/exec"

func sudosh(name string) {
    c := exec.Command("/bin/sh", "-c", "sudo /path/to/sudosh " + name)
    c.Stdin = os.Stdin
    c.Stderr = os.Stderr
    c.Stdout = os.Stdout
    o := c.Run()
    o=o
}
  • 写回答

1条回答 默认 最新

  • dongxian7489 2014-09-08 16:58
    关注

    sudo2sh source shows that it is trying to call ttyname, which might not work in the context of a go-sh.Command calling exec.Command(cmd, args...).

    You can test by calling ttyname directly, or by trying and implement a TTYname() function as in this thread.
    You can also check the isatty function of go-termutil to debug the issue (as in this answer).

    The bottom line is: if ttyname fails in a exec.Command session, sudosh will always return that error message.

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

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: