2

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
}
sunshinekitty
  • 2,307
  • 6
  • 19
  • 31

1 Answers1

1

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • stty seems to work (http://stackoverflow.com/a/16569795/6309), but ttyname might not. – VonC Sep 08 '14 at 17:02
  • stty works for passing sudo and I assume getting in to sudosh, after this takes place I'd like for the program to exit and terminal to go in to the sudosh, still playing around with things to see if I can get an answer – sunshinekitty Sep 08 '14 at 17:29