-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathexample_test.go
230 lines (185 loc) · 5.72 KB
/
example_test.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//: Copyright Verizon Media
//: Licensed under the terms of the Apache 2.0 License. See LICENSE file in the project root for terms.
package vssh_test
import (
"context"
"fmt"
"log"
"net"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/yahoo/vssh"
)
// This example demonstrates the use of stream
func ExampleStream() {
vs := vssh.New().Start()
config, _ := vssh.GetConfigPEM("ubuntu", "aws.pem")
vs.AddClient("54.193.17.197:22", config, vssh.SetMaxSessions(4))
vs.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := "uname -a"
timeout, _ := time.ParseDuration("5s")
respChan := vs.Run(ctx, cmd, timeout)
resp := <-respChan
if err := resp.Err(); err != nil {
log.Fatal(err)
}
stream := resp.GetStream()
defer stream.Close()
for stream.ScanStdout() {
txt := stream.TextStdout()
fmt.Println(txt)
}
if err := stream.Err(); err != nil {
log.Fatal(err)
}
}
// This example demonstrates the use of GetText() for two hosts.
func ExampleResponse() {
// construct and start the vssh
vs := vssh.New().Start()
// create ssh configuration with user/pass
// you can create this configuration by golang ssh package
config := vssh.GetConfigUserPass("vssh", "vssh")
// add clients to vssh with one option: max session
// there are other options that you can add to this method
for _, addr := range []string{"54.193.17.197:22", "192.168.2.19:22"} {
vs.AddClient(addr, config, vssh.SetMaxSessions(4))
}
// wait until vssh connected to all the clients
vs.Wait()
// create a context with cancel
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// send the ping command to clients with 6 seconds timeout
cmd := "ping -c 4 192.168.55.10"
timeout, _ := time.ParseDuration("6s")
respChan := vs.Run(ctx, cmd, timeout)
// get the resp channel for each client
for resp := range respChan {
// in case of the connectivity issue to client
if err := resp.Err(); err != nil {
log.Println(err)
continue
}
// get the returned data from client
outTxt, errTxt, err := resp.GetText(vs)
// check the error like timeout but still
// we can have data on outTxt and errTxt
if err != nil {
log.Println(err)
}
// print command's stdout
fmt.Println(outTxt)
// print command's stderr
fmt.Println(errTxt)
// print exit status of the remote command
fmt.Println(resp.ExitStatus())
}
}
// This example demonstrates integration vSSH with AWS EC2
func Example_cloud() {
vs := vssh.New().Start()
config, _ := vssh.GetConfigPEM("ubuntu", "aws.pem")
// AWS EC2 Golang SDK
// Please check their website for more information
// https://docs.aws.amazon.com/sdk-for-go/
awsConfig := &aws.Config{
Region: aws.String("us-west-1"),
Credentials: credentials.NewStaticCredentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", ""),
}
sess, err := session.NewSession(awsConfig)
if err != nil {
fmt.Println("error creating new session:", err.Error())
log.Fatal(err)
}
ec2svc := ec2.New(sess)
params := &ec2.DescribeInstancesInput{
// filter running instances at us-west-1
Filters: []*ec2.Filter{
{
Name: aws.String("instance-state-name"),
Values: []*string{aws.String("running")},
},
},
}
resp, err := ec2svc.DescribeInstances(params)
if err != nil {
fmt.Println("there was an error listing instances in", err.Error())
log.Fatal(err.Error())
}
// iterate over the EC2 running instances and add to vssh
for idx := range resp.Reservations {
for _, inst := range resp.Reservations[idx].Instances {
labels := make(map[string]string)
for _, tag := range inst.Tags {
labels[*tag.Key] = *tag.Value
}
addr := net.JoinHostPort(*inst.PublicIpAddress, "22")
vs.AddClient(addr, config, vssh.SetLabels(labels))
}
}
vs.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := "uname -a"
timeout, _ := time.ParseDuration("5s")
respChan := vs.Run(ctx, cmd, timeout)
for resp := range respChan {
// in case of the connectivity issue to client
if err := resp.Err(); err != nil {
log.Println(err)
continue
}
// get the returned data from client
outTxt, errTxt, err := resp.GetText(vs)
// check the error like timeout but still
// we can have data on outTxt and errTxt
if err != nil {
log.Println(err)
}
// print command's stdout
fmt.Println(outTxt)
// print command's stderr
fmt.Println(errTxt)
// print exit status of the remote command
fmt.Println(resp.ExitStatus())
}
}
// This example demonstrates how to set limit the amount of returned data
func ExampleSetLimitReaderStdout() {
// construct and start the vssh
vs := vssh.New().Start()
// create ssh configuration
// you can create this configuration by golang ssh package
config, _ := vssh.GetConfigPEM("ubuntu", "aws.pem")
// add client to vssh with one option: max-sessions
// there are other options that you can add to this method
vs.AddClient("54.215.209.152:22", config, vssh.SetMaxSessions(2))
// wait until vssh connected to the client
vs.Wait()
// create a context with cancel
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
timeout, _ := time.ParseDuration("6s")
// run dmesg command with limit the amounth of returned data to 512 bytes
respChan := vs.Run(ctx, "dmesg", timeout, vssh.SetLimitReaderStdout(1024))
// get the resp
resp := <-respChan
// in case of the connectivity issue to client
if err := resp.Err(); err != nil {
log.Fatal(err)
}
outTxt, errTxt, err := resp.GetText(vs)
// check the error like timeout but still
// we can have data on outTxt and errTxt
if err != nil {
log.Println(err)
}
fmt.Println(outTxt)
fmt.Println(errTxt)
}