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.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?