-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathresponse.go
More file actions
140 lines (124 loc) · 3.81 KB
/
Copy pathresponse.go
File metadata and controls
140 lines (124 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package winrm
import (
"bytes"
"encoding/base64"
"encoding/xml"
"errors"
"io"
"io/ioutil"
)
type ResponseSelector struct {
Value string `xml:",innerxml"`
Name string `xml:"Name,attr"`
}
type ResponseSelectorSet struct {
Selector *ResponseSelector `xml"w:Selector"`
}
type ReferenceParameters struct {
ResourceURI string `xml"w:ResourceURI"`
SelectorSet *ResponseSelectorSet `xml"w:SelectorSet"`
}
type ResourceCreated struct {
Address string `xml"a:Address"`
ReferenceParameters *ReferenceParameters `xml"a:ReferenceParameters"`
}
type ResponseHeader struct {
Action string `xml"a:Action"`
MessageID string `xml"a:MessageID"`
To string `xml"a:To"`
RelatesTo string `xml"a:RelatesTo"`
}
type CommandResponse struct {
CommandId string `xml"rsp:CommandId"`
}
type ResponseShell struct {
// xmlnsRsp string `xml:"xmlns:rsp,attr"`
ShellId string `xml"rsp:ShellId`
ResourceUri string `xml"rsp:ResourceUri"`
Owner string `xml"rsp:Owner"`
ClientIP string `xml"rsp:ClientIP"`
IdleTimeOut string `xml"rsp:IdleTimeOut"`
InputStreams string `xml"rsp:InputStreams"`
OutputStreams string `xml"rsp:OutputStreams"`
ShellRunTime string `xml"rsp:OutputStreams"`
ShellInactivity string `xml"rsp:OutputStreams"`
}
type ResponseStream struct {
Value string `xml:",innerxml"`
Name string `xml:"Name,attr"`
End string `xml:"End,attr"`
}
type ResponseCommandState struct {
ExitCode int `xml"rsp:ExitCode"`
CommandId string `xml:"CommandId,attr"`
State string `xml:"State,attr"`
}
type ReceiveResponse struct {
Stream []ResponseStream `xml"rsp:Stream"`
CommandState *ResponseCommandState `xml"rsp:CommandState"`
}
type ResponseBody struct {
CommandResponse *CommandResponse `xml"rsp:CommandResponse"`
ResourceCreated *ResourceCreated `xml"x:ResourceCreated"`
Shell *ResponseShell `xml"rsp:Shell"`
ReceiveResponse *ReceiveResponse `xml"rsp:ReceiveResponse"`
}
type ResponseEnvelope struct {
XMLName xml.Name `xml"s:Envelope"`
// xmlnsS string `xml"xmlns:s,attr"`
// xmlnsA string `xml"xmlns:a,attr"`
// xmlnsX string `xml"xmlns:x,attr"`
// xmlnsW string `xml"xmlns:w,attr"`
// xmlnsRsp string `xml"xmlns:rsp,attr"`
// xmlnsP string `xml"xmlns:p,attr"`
// xmlnsLang string `xml"xmlns:lang,attr"`
Header *ResponseHeader `xml"s:Header"`
Body *ResponseBody `xml"s:Body"`
}
func GetObjectFromXML(XMLinput io.Reader) (ResponseEnvelope, error) {
b, err := ioutil.ReadAll(XMLinput)
var response ResponseEnvelope
if err != nil {
return response, err
} else {
xml.Unmarshal(b, &response)
}
x := ResponseEnvelope{}
if response == x {
return x, errors.New("Invalid server response")
}
return response, nil
}
var parseXML = GetObjectFromXML
func ParseCommandOutput(XMLinput io.Reader) (stdout, stderr string, exitcode int, err error) {
//fmt.Printf("%s\n\n\n", XMLinput)
object, err := parseXML(XMLinput)
//fmt.Printf("%s", object.Body.ReceiveResponse.Stream)
if err != nil {
return "", "", 0, errors.New("Error parsing XML")
}
var stdout_b bytes.Buffer
var stderr_b bytes.Buffer
for _, value := range object.Body.ReceiveResponse.Stream {
if value.End == "" && value.Name == "stdout" {
tmp, err := base64.StdEncoding.DecodeString(value.Value)
if err != nil {
return "", "", 0, errors.New("Error decoding stdout")
}
stdout_b.Write(tmp)
} else if value.End == "" && value.Name == "stderr" {
tmp, err := base64.StdEncoding.DecodeString(value.Value)
if err != nil {
return "", "", 0, errors.New("Error decoding stderr")
}
stderr_b.Write(tmp)
} else {
break
}
}
exitcode = object.Body.ReceiveResponse.CommandState.ExitCode
stdout = stdout_b.String()
stderr = stderr_b.String()
err = nil
return
}