// RemoteWinRmCommand runs the remote command on a windows machine func RemoteWinRmCommand(user string, password string, host string, port string, cmd string) error { client, err := winrm.NewClient(&winrm.Endpoint{Host: "localhost", Port: 5985, HTTPS: false, Insecure: false}, user, password) if err != nil { fmt.Println(err) } run, err := client.RunWithInput(cmd, os.Stdout, os.Stderr, os.Stdin) if err != nil { fmt.Println(err) } fmt.Println(run) return nil }
// CloseShell closes the given WinRM Shell terminating any processes created within it func CloseShell(user string, password string, host string, port string, shellID string) error { portNumber, err := parsePortNumber(port) if err != nil { return err } client, err := winrm.NewClient(&winrm.Endpoint{Host: host, Port: portNumber, HTTPS: false, Insecure: false}, user, password) if err != nil { return fmt.Errorf("Could not create WinRM client: %s", err) } log.Info("Closing shell %s", shellID) shell := client.NewShell(shellID) return shell.Close() }
func main() { var ( hostname string user string pass string cmd string port int https bool insecure bool cacert string ) flag.StringVar(&hostname, "hostname", "localhost", "winrm host") flag.StringVar(&user, "username", "vagrant", "winrm admin username") flag.StringVar(&pass, "password", "vagrant", "winrm admin password") flag.IntVar(&port, "port", 5985, "winrm port") flag.BoolVar(&https, "https", false, "use https") flag.BoolVar(&insecure, "insecure", false, "skip SSL validation") flag.StringVar(&cacert, "cacert", "", "CA certificate to use") flag.Parse() var certBytes []byte var err error if cacert != "" { certBytes, err = ioutil.ReadFile(cacert) if err != nil { fmt.Println(err) os.Exit(1) } } else { certBytes = nil } cmd = flag.Arg(0) client, err := winrm.NewClient(&winrm.Endpoint{Host: hostname, Port: port, HTTPS: https, Insecure: insecure, CACert: &certBytes}, user, pass) if err != nil { fmt.Println(err) os.Exit(1) } exitCode, err := client.RunWithInput(cmd, os.Stdout, os.Stderr, os.Stdin) if err != nil { fmt.Println(err) os.Exit(1) } os.Exit(exitCode) }
func Win(command string, port int, user string, hostsfile string, https bool, insecure bool, password string, cacert string, host string) { var certBytes []byte var err error if cacert != "" { certBytes, err = ioutil.ReadFile(cacert) if err != nil { fmt.Println(err) os.Exit(1) } } else { certBytes = nil } argsStr := strings.Split(command, ",") argsList := make([]string, 0) for _, item := range argsStr { argsList = append(argsList, item) } if host != "" { for _, item := range argsList { if err != nil { fmt.Println(item) fmt.Println(err) } else { client, err := winrm.NewClient(&winrm.Endpoint{Host: host, Port: port, HTTPS: https, Insecure: insecure, CACert: &certBytes}, user, password) if err != nil { fmt.Println(err) } fmt.Println() fmt.Println(host, item) run, err := client.RunWithInput(item, os.Stdout, os.Stderr, os.Stdin) if err != nil { fmt.Println(err) } fmt.Println(run) } } } else { file, err := os.Open(hostsfile) defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { for _, item := range argsList { if err != nil { fmt.Println(item) fmt.Println(err) } else { client, err := winrm.NewClient(&winrm.Endpoint{Host: scanner.Text(), Port: port, HTTPS: https, Insecure: insecure, CACert: &certBytes}, user, password) if err != nil { fmt.Println(err) } fmt.Println() fmt.Println(scanner.Text(), item) run, err := client.RunWithInput(item, os.Stdout, os.Stderr, os.Stdin) if err != nil { fmt.Println(err) } fmt.Println(run) } } } if err = scanner.Err(); err != nil { fmt.Println(err) } } }
// RemoteWinRmCommand runs the remote command on a windows machine func RemoteWinRmCommand(user string, password string, host string, port string, commandText string, c *client.Client, rc *api.ReplicationController, hostName string) error { portNumber, err := parsePortNumber(port) if err != nil { return err } log.Info("Connecting to windows host over WinRM on host %s and port %d with user %s with command `%s`", host, portNumber, user, commandText) client, err := winrm.NewClient(&winrm.Endpoint{Host: host, Port: portNumber, HTTPS: false, Insecure: false}, user, password) if err != nil { return fmt.Errorf("Could not create WinRM client: %s", err) } isBash := false isBashShellText := os.Getenv(ansible.EnvIsBashShell) if len(isBashShellText) > 0 && strings.ToLower(isBashShellText) == "true" { isBash = true } if rc.ObjectMeta.Annotations != nil && !isBash { oldShellID := rc.ObjectMeta.Annotations[ansible.WinRMShellAnnotationPrefix+hostName] if len(oldShellID) > 0 { // lets close the previously running shell on this machine log.Info("Closing the old WinRM Shell %s", oldShellID) shell := client.NewShell(oldShellID) err = shell.Close() if err != nil { log.Warn("Failed to close shell %s. Error: %s", oldShellID, err) } } } shell, err := client.CreateShell() if err != nil { return fmt.Errorf("Impossible to create WinRM shell: %s", err) } defer shell.Close() shellID := shell.ShellId log.Info("Created WinRM Shell %s", shellID) if rc != nil && c != nil && !isBash { rc.ObjectMeta.Annotations[ansible.WinRMShellAnnotationPrefix+hostName] = shellID _, err = c.ReplicationControllers(rc.ObjectMeta.Namespace).UpdateStatus(rc) if err != nil { return err } } var cmd *winrm.Command cmd, err = shell.Execute(commandText) if err != nil { return fmt.Errorf("Impossible to create Command %s\n", err) } go io.Copy(cmd.Stdin, os.Stdin) go io.Copy(os.Stdout, cmd.Stdout) go io.Copy(os.Stderr, cmd.Stderr) cmd.Wait() exitCode := cmd.ExitCode() if exitCode > 0 { return fmt.Errorf("Failed to run command '%s' got exit code %d", commandText, exitCode) } // TODO // return cmd.Error() return nil }