go - Golang: How to create interactive command execution? -
my problem interaction exec.command. automate runas on windows.
i want via application launch other applications (eg ccleaner, antivirus eset online, etc ...) on computers of clients. create adminsys account , want launch these various applications automatically 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 not work!
the errors 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 not recognize password. works when directly in command prompt
i'm not running windows, , code have in question works me. so, following may not solve problem, alternative passing string command.
i created basic command named pass
in order assert correctness of password:
func main() { fmt.print("enter password please: ") reader := bufio.newreader(os.stdin) password, err := reader.readstring('\n') if err != nil { fmt.println("an error occurred while reading line") } // remove line break password = password[:len(password)-1] if password == "secret pass" { fmt.println("success !") } else { fmt.println("failure !") } }
now, instead of working pipe, set stdin
of command new reader:
func main() { cmd := exec.command("pass") cmd.stdin = strings.newreader("secret pass!\n") cmd.stdout = os.stdout _ = cmd.run() // failure cmd = exec.command("pass") cmd.stdin = strings.newreader("secret pass\n") cmd.stdout = os.stdout _ = cmd.run() // success }
hope helps.
Comments
Post a Comment