Last active
June 20, 2016 21:50
-
-
Save tintoy/5aacf6c4de3cbf31082985f5ff8b38f7 to your computer and use it in GitHub Desktop.
WinRM from Go using Negotiate authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/masterzen/winrm" | |
"io" | |
"net/http" | |
"os" | |
"time" | |
ntlmssp "github.com/Azure/go-ntlmssp" | |
) | |
// If using non-SSL for WinRM, you must run: | |
// | |
// winrm set winrm/config/service '@{AllowUnencrypted="true"}' | |
const ( | |
host = "af-tf-vm1.westus.cloudapp.azure.com" | |
port = 5985 | |
username = "tintoy" | |
password = `goGetY0ur0wnP@$$w0rd.` | |
timeout = 10 * time.Second | |
) | |
func main() { | |
params := winrm.DefaultParameters | |
params.TransportDecorator = func(transport *http.Transport) http.RoundTripper { | |
return &ntlmssp.Negotiator{ | |
RoundTripper: transport, | |
} | |
} | |
fmt.Println(">> Configuring connection...") | |
endpoint := winrm.NewEndpoint(host, port, false, true, nil, nil, nil, timeout) | |
client, err := winrm.NewClientWithParameters(endpoint, username, password, params) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf(">> Connecting to '%s:%d'...\n", host, port) | |
shell, err := client.CreateShell() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(">> Connected. Running cmd.exe...") | |
var cmd *winrm.Command | |
cmd, err = shell.Execute("cmd.exe") | |
if err != nil { | |
panic(err) | |
} | |
go io.Copy(cmd.Stdin, os.Stdin) | |
go io.Copy(os.Stdout, cmd.Stdout) | |
go io.Copy(os.Stderr, cmd.Stderr) | |
fmt.Println(">> cmd.exe running; waiting for termination...") | |
cmd.Wait() | |
shell.Close() | |
fmt.Println("Done.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment