Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/allow sitewise keys #4

Merged
merged 2 commits into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exposq.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"net/http"

"./sshttp"
"github.com/emirozer/exposq/Godeps/_workspace/src/github.com/codegangsta/negroni"
"github.com/emirozer/exposq/sshttp"
)

func main() {
Expand Down
68 changes: 29 additions & 39 deletions sshttp/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@ package sshttp

import (
"bytes"
"encoding/json"
"fmt"
"github.com/emirozer/exposq/Godeps/_workspace/src/golang.org/x/crypto/ssh"
"io/ioutil"
"log"
"os"
"strings"
"time"
)

json "github.com/emirozer/exposq/Godeps/_workspace/src/github.com/bitly/go-simplejson"
type jsonOptions struct {
Key string `json:"key"`
Targets []*target `json:"targets"`
}

"github.com/emirozer/exposq/Godeps/_workspace/src/golang.org/x/crypto/ssh"
)
type target struct {
User string `json:"user"`
Ip string `json:"ip"`
Key string `json:"key"`
}

// func responsible of parsing the target.json file
func readTarget() ([][]string, string) {

target_list := make([][]string, 0)
t, err := os.Open("targets.json")
defer t.Close()
func readTarget() []*target {
byt, err := ioutil.ReadFile("targets.json")
if err != nil {
panic(err)
}

js, err := json.NewFromReader(t)
var opts jsonOptions
err = json.Unmarshal(byt, &opts)

if err != nil {
log.Fatal(err)
panic(err)
}
key, _ := js.Get("key").String()

targets := js.Get("targets")
temp_t, _ := js.Get("targets").Array()

for k, _ := range temp_t {
temp_slice := make([]string, 0)
user, _ := targets.GetIndex(k).Get("user").String()
ip, _ := targets.GetIndex(k).Get("ip").String()

temp_slice = append(temp_slice, user)
temp_slice = append(temp_slice, ip)
target_list = append(target_list, temp_slice)
// set key to default if needed
for _, t := range opts.Targets {
if len(t.Key) == 0 {
t.Key = opts.Key
}
}

return target_list, key
return opts.Targets
}

// func that is responsible of setting the session and communicating
Expand Down Expand Up @@ -103,27 +103,17 @@ func sshDispatch(cmd string, user string, ip string, key string, messages chan<-

// Returns the result that has been gathered from target machines
func MainSshHandler(cmd string) string {
var target_list [][]string
var key string
var user string
var ip string
var sout string
var sout_l []string

target_list, key = readTarget()

messages := make(chan string, len(target_list))

for _, j := range target_list {

user = j[0]
ip = j[1]

go sshDispatch(cmd, user, ip, key, messages)
targets := readTarget()
messages := make(chan string, len(targets))

for _, t := range targets {
go sshDispatch(cmd, t.User, t.Ip, t.Key, messages)
}

for len(sout_l) < len(target_list) {
for len(sout_l) < len(targets) {

select {
case <-time.After(time.Second * 20):
Expand Down