Skip to content

Commit

Permalink
Merge pull request #22 from DiscoRiver/add-python-script
Browse files Browse the repository at this point in the history
Implement a script interface
  • Loading branch information
DiscoRiver authored Dec 12, 2021
2 parents 215f6d9 + 7125338 commit 42aa0c0
Show file tree
Hide file tree
Showing 19 changed files with 498 additions and 195 deletions.
41 changes: 41 additions & 0 deletions _examples/bulk_return/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
"github.com/discoriver/massh"
"golang.org/x/crypto/ssh"
"time"
)

func main() {
j := &massh.Job{
Command: "echo \"Hello, World\"",
}

sshc := &ssh.ClientConfig{
// Fake credentials
User: "u01",
Auth: []ssh.AuthMethod{ssh.Password("password")},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: time.Duration(2) * time.Second,
}

cfg := massh.NewConfig()
cfg.SSHConfig = sshc
cfg.Job = j
cfg.WorkerPool = 10
cfg.SetHosts([]string{"192.168.1.118", "192.168.1.123"})

res, err := cfg.Run()
if err != nil {
panic(err)
}

for i := range res {
if res[i].Error != nil {
fmt.Printf("%s: %s\n", res[i].Host, res[i].Error)
} else {
fmt.Printf("%s: %s", res[i].Host, res[i].Output)
}
}
}
76 changes: 0 additions & 76 deletions _examples/example_bulk_return/command.go

This file was deleted.

58 changes: 0 additions & 58 deletions _examples/example_bulk_return/main.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ func main() {
Timeout: time.Duration(2) * time.Second,
}

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


resChan := make(chan massh.Result)

Expand Down
46 changes: 46 additions & 0 deletions _examples/python_script/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"github.com/discoriver/massh"
"golang.org/x/crypto/ssh"
"time"
)

func main() {
j := &massh.Job{
Command: "echo \"Hello, World\"",
}

sshc := &ssh.ClientConfig{
// Fake credentials
User: "u01",
Auth: []ssh.AuthMethod{ssh.Password("password")},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: time.Duration(2) * time.Second,
}

cfg := massh.NewConfig()
cfg.SSHConfig = sshc
cfg.Job = j
cfg.WorkerPool = 10
cfg.SetHosts([]string{"192.168.1.118", "192.168.1.123"})

err := cfg.Job.SetScript("script.py", "")
if err != nil {
panic(err)
}

res, err := cfg.Run()
if err != nil {
panic(err)
}

for i := range res {
if res[i].Error != nil {
fmt.Printf("%s: %s\n", res[i].Host, res[i].Error)
} else {
fmt.Printf("%s: %s", res[i].Host, res[i].Output)
}
}
}
3 changes: 3 additions & 0 deletions _examples/python_script/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env python3
import sys
sys.stdout.write("Hello world, from python script." + '\n')
46 changes: 46 additions & 0 deletions _examples/shell_script/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"github.com/discoriver/massh"
"golang.org/x/crypto/ssh"
"time"
)

func main() {
j := &massh.Job{
Command: "echo \"Hello, World\"",
}

sshc := &ssh.ClientConfig{
// Fake credentials
User: "u01",
Auth: []ssh.AuthMethod{ssh.Password("password")},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: time.Duration(2) * time.Second,
}

cfg := massh.NewConfig()
cfg.SSHConfig = sshc
cfg.Job = j
cfg.WorkerPool = 10
cfg.SetHosts([]string{"192.168.1.118", "192.168.1.123"})

err := cfg.Job.SetScript("script.sh", "")
if err != nil {
panic(err)
}

res, err := cfg.Run()
if err != nil {
panic(err)
}

for i := range res {
if res[i].Error != nil {
fmt.Printf("%s: %s\n", res[i].Host, res[i].Error)
} else {
fmt.Printf("%s: %s", res[i].Host, res[i].Output)
}
}
}
2 changes: 2 additions & 0 deletions _examples/shell_script/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
echo "Hello World, from shell script"
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ func main() {
Timeout: time.Duration(2) * time.Second,
}

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

resChan := make(chan massh.Result)

Expand Down
Loading

0 comments on commit 42aa0c0

Please sign in to comment.