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

gif

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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -