My problem is the interaction with exec.command. I would like to automate RunAs on windows.
I want via this application launch other applications (eg Ccleaner, antivirus eset online, etc ...) on computers of my clients. So I create a adminsys account and I want to launch these various applications automatically with this account.
cmd := exec.Command("runas", user, nameProgram)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
stdin, err := cmd.StdinPipe()
if err != nil {
fmt.Println(err)
}
defer stdin.Close()
err = cmd.Start()
if err != nil {
fmt.Println(err)
return
}
time.Sleep(3 * time.Second)
io.WriteString(stdin, password)
err = cmd.Wait()
if err != nil {
fmt.Println(err)
return
}
This does not work!
The errors with runas.
Erreur de RUNAS : Impossible d’exécuter - C:\program.exe
1326 : Le nom d’utilisateur ou le mot de passe est incorrect.
It looks like he does not recognize the password. This works when I do it directly in the command prompt
I'm not running windows, and the code you have in your question works for me. So, the following may not solve your problem, but this is an alternative for passing string to a command.
I created this basic command named pass
in order to assert the correctness of the password:
func main() {
fmt.Print("Enter your password please: ")
reader := bufio.NewReader(os.Stdin)
password, err := reader.ReadString('
')
if err != nil {
fmt.Println("An error occurred while reading line")
}
// Remove the line break
password = password[:len(password)-1]
if password == "Secret pass" {
fmt.Println("Success !")
} else {
fmt.Println("Failure !")
}
}
Now, instead of working with pipe, set the Stdin
of the command to a new reader:
func main() {
cmd := exec.Command("pass")
cmd.Stdin = strings.NewReader("Secret pass!
")
cmd.Stdout = os.Stdout
_ = cmd.Run()
// Failure
cmd = exec.Command("pass")
cmd.Stdin = strings.NewReader("Secret pass
")
cmd.Stdout = os.Stdout
_ = cmd.Run()
// Success
}
Hope this helps.