Skip to content

Commit 42aa0c0

Browse files
authored
Merge pull request #22 from DiscoRiver/add-python-script
Implement a script interface
2 parents 215f6d9 + 7125338 commit 42aa0c0

File tree

19 files changed

+498
-195
lines changed

19 files changed

+498
-195
lines changed

_examples/bulk_return/main.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/discoriver/massh"
6+
"golang.org/x/crypto/ssh"
7+
"time"
8+
)
9+
10+
func main() {
11+
j := &massh.Job{
12+
Command: "echo \"Hello, World\"",
13+
}
14+
15+
sshc := &ssh.ClientConfig{
16+
// Fake credentials
17+
User: "u01",
18+
Auth: []ssh.AuthMethod{ssh.Password("password")},
19+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
20+
Timeout: time.Duration(2) * time.Second,
21+
}
22+
23+
cfg := massh.NewConfig()
24+
cfg.SSHConfig = sshc
25+
cfg.Job = j
26+
cfg.WorkerPool = 10
27+
cfg.SetHosts([]string{"192.168.1.118", "192.168.1.123"})
28+
29+
res, err := cfg.Run()
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
for i := range res {
35+
if res[i].Error != nil {
36+
fmt.Printf("%s: %s\n", res[i].Host, res[i].Error)
37+
} else {
38+
fmt.Printf("%s: %s", res[i].Host, res[i].Output)
39+
}
40+
}
41+
}

_examples/example_bulk_return/command.go

-76
This file was deleted.

_examples/example_bulk_return/main.go

-58
This file was deleted.

_examples/example_jobstack_streaming/main.go renamed to _examples/jobstack_streaming/main.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ func main() {
2929
Timeout: time.Duration(2) * time.Second,
3030
}
3131

32-
cfg := &massh.Config{
33-
// In this example I was testing with two working hosts, and two non-existent IPs.
34-
SSHConfig: sshc,
35-
JobStack: &[]massh.Job{j, j2, j3},
36-
WorkerPool: 10,
37-
}
38-
cfg.SetHosts([]string{"192.168.1.119", "192.168.1.120", "192.168.1.129", "192.168.1.212"})
32+
cfg := massh.NewConfig()
33+
cfg.SSHConfig = sshc
34+
cfg.JobStack = &[]massh.Job{j, j2, j3}
35+
cfg.WorkerPool = 10
36+
cfg.SetHosts([]string{"192.168.1.118"})
37+
3938

4039
resChan := make(chan massh.Result)
4140

_examples/python_script/main.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/discoriver/massh"
6+
"golang.org/x/crypto/ssh"
7+
"time"
8+
)
9+
10+
func main() {
11+
j := &massh.Job{
12+
Command: "echo \"Hello, World\"",
13+
}
14+
15+
sshc := &ssh.ClientConfig{
16+
// Fake credentials
17+
User: "u01",
18+
Auth: []ssh.AuthMethod{ssh.Password("password")},
19+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
20+
Timeout: time.Duration(2) * time.Second,
21+
}
22+
23+
cfg := massh.NewConfig()
24+
cfg.SSHConfig = sshc
25+
cfg.Job = j
26+
cfg.WorkerPool = 10
27+
cfg.SetHosts([]string{"192.168.1.118", "192.168.1.123"})
28+
29+
err := cfg.Job.SetScript("script.py", "")
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
res, err := cfg.Run()
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
for i := range res {
40+
if res[i].Error != nil {
41+
fmt.Printf("%s: %s\n", res[i].Host, res[i].Error)
42+
} else {
43+
fmt.Printf("%s: %s", res[i].Host, res[i].Output)
44+
}
45+
}
46+
}

_examples/python_script/script.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
sys.stdout.write("Hello world, from python script." + '\n')

_examples/shell_script/main.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/discoriver/massh"
6+
"golang.org/x/crypto/ssh"
7+
"time"
8+
)
9+
10+
func main() {
11+
j := &massh.Job{
12+
Command: "echo \"Hello, World\"",
13+
}
14+
15+
sshc := &ssh.ClientConfig{
16+
// Fake credentials
17+
User: "u01",
18+
Auth: []ssh.AuthMethod{ssh.Password("password")},
19+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
20+
Timeout: time.Duration(2) * time.Second,
21+
}
22+
23+
cfg := massh.NewConfig()
24+
cfg.SSHConfig = sshc
25+
cfg.Job = j
26+
cfg.WorkerPool = 10
27+
cfg.SetHosts([]string{"192.168.1.118", "192.168.1.123"})
28+
29+
err := cfg.Job.SetScript("script.sh", "")
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
res, err := cfg.Run()
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
for i := range res {
40+
if res[i].Error != nil {
41+
fmt.Printf("%s: %s\n", res[i].Host, res[i].Error)
42+
} else {
43+
fmt.Printf("%s: %s", res[i].Host, res[i].Output)
44+
}
45+
}
46+
}

_examples/shell_script/script.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
echo "Hello World, from shell script"

_examples/example_streaming/main.go renamed to _examples/streaming/main.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ func main() {
2121
Timeout: time.Duration(2) * time.Second,
2222
}
2323

24-
cfg := &massh.Config{
25-
// In this example I was testing with two working hosts, and two non-existent IPs.
26-
SSHConfig: sshc,
27-
Job: j,
28-
WorkerPool: 10,
29-
}
30-
cfg.SetHosts([]string{"192.168.1.119", "192.168.1.120", "192.168.1.129", "192.168.1.212"})
24+
cfg := massh.NewConfig()
25+
cfg.SSHConfig = sshc
26+
cfg.Job = j
27+
cfg.WorkerPool = 10
28+
cfg.SetHosts([]string{"192.168.1.118", "192.168.1.119", "192.168.1.120", "192.168.1.129", "192.168.1.212"})
3129

3230
resChan := make(chan massh.Result)
3331

0 commit comments

Comments
 (0)