Skip to content

Commit

Permalink
utilize cobra
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsullivan committed Jun 21, 2017
1 parent cd07029 commit 59584ff
Show file tree
Hide file tree
Showing 197 changed files with 47,015 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/_exe
99 changes: 98 additions & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ clean:
rm -rf _exe/

build:
godep go build -x -o _exe/befehl github.com/sgsullivan/befehl/cmd/befehl
godep go build -x -o _exe/befehl executor/befehl_main.go

test:
godep go test -v
Expand Down
22 changes: 18 additions & 4 deletions main.go → befehl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"github.com/fatih/color"
"github.com/howeyc/gopass"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
Expand All @@ -17,6 +18,8 @@ import (
"time"
)

var Config *viper.Viper

type Queue struct {
count int64
}
Expand All @@ -25,7 +28,8 @@ var store struct {
sshKey ssh.Signer
}

func Fire(targets *string, payload *string, routines *int) {
func Fire(targets *string, payload *string, routines *int, passedViper *viper.Viper) {
Config = passedViper
bytePayload := readFile(payload)
populateSshKey()
fireTorpedos(bytePayload, targets, routines)
Expand All @@ -38,6 +42,9 @@ func populateSshKey() {
}

privKeyFile := os.Getenv("HOME") + "/.ssh/id_rsa"
if Config.GetString("auth.privatekeyfile") != "" {
privKeyFile = Config.GetString("auth.privatekeyfile")
}
rawKey := readFile(&privKeyFile)
privKeyBytes, _ := pem.Decode(rawKey)

Expand Down Expand Up @@ -98,8 +105,12 @@ func fireTorpedos(payload []byte, targets *string, routines *int) {
wg.Add(hostCnt)
var sem = make(chan int, *routines)

sshEntryUser := "root"
if Config.GetString("auth.sshuser") != "" {
sshEntryUser = Config.GetString("auth.sshuser")
}
sshConfig := &ssh.ClientConfig{
User: "root",
User: sshEntryUser,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(store.sshKey),
},
Expand Down Expand Up @@ -179,8 +190,11 @@ func runPayload(wg *sync.WaitGroup, host string, payload []byte, sshConfig *ssh.
}

func logPayloadRun(host string, output string) {
logDir := os.Getenv("HOME") + "/befehl/logs/"
logFile := logDir + host
logDir := os.Getenv("HOME") + "/befehl/logs"
if Config.GetString("general.logdir") != "" {
logDir = Config.GetString("general.logdir")
}
logFile := logDir + "/" + host
if !pathExists(logDir) {
if err := os.MkdirAll(logDir, os.FileMode(0700)); err != nil {
panic(fmt.Sprintf("Failed creating [%s]: %s\n", logDir, err))
Expand Down
31 changes: 0 additions & 31 deletions cmd/befehl/main.go

This file was deleted.

57 changes: 57 additions & 0 deletions cmd/execute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"fmt"
"github.com/sgsullivan/befehl"
"github.com/spf13/cobra"
)

var payload string
var routines int
var hostsList string

var executeCmd = &cobra.Command{
Use: "execute",
Short: "Execute the given payload against the given hosts list",
Long: `Executes the given payload on each host in the hosts list. Hosts in the hosts
list should be separated by a new line. You can control how many payloads run concurrently by
passing the routines flag.
By default befehl will use the private key in $HOME/.ssh/id_rsa. This can be overrode by
specifying auth.privatekeyfile in ~/.befehl.[toml|json|yaml].
By default befehl will write the output of each payload for each host in $HOME/befehl/logs. This
can be overrode by specifying general.logdir in ~/.befehl.[toml|json|yaml].
By default befehl will attempt to ssh as root. This can be overrode by specifying auth.sshuser
in ~/.befehl.[toml|json|yaml].
Heres an example specifying all of the above mentioned options:
[general]
logdir = "/home/ssullivan/log-special"
[auth]
privatekeyfile = "/home/ssullivan/alt/id_rsa"
sshuser = "eingeben"
`,
Run: func(cmd *cobra.Command, args []string) {
for _, value := range []string{payload, hostsList} {
if value == "" {
panic("Missing payload, or hosts; see --help")
}
if routines == 0 {
fmt.Printf("--routines not given, defaulting to 30..\n")
routines = 30
}
}
befehl.Fire(&hostsList, &payload, &routines, Config)
},
}

func init() {
RootCmd.AddCommand(executeCmd)
executeCmd.Flags().StringVarP(&payload, "payload", "", "", "file location to the payload, which contains the commands to execute on the remote hosts")
executeCmd.Flags().StringVarP(&hostsList, "hosts", "", "", "file location to hosts list, which contains all hosts (separated by newline) to run the payload on")
executeCmd.Flags().IntVarP(&routines, "routines", "", 0, "maximum number of payloads that will run at once (defaults to 30)")
}
42 changes: 42 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)

var Config *viper.Viper

var RootCmd = &cobra.Command{
Use: "befehl",
Short: "ausführen willkürliche Befehle über ssh in Masse",
Long: `Gegeben ein Gastgeberliste, ausführen Nutzlast über ssh Auf jedem Host.
Dieses Werkzeug sollte mit Vorsicht verwendet werden; gegeben das Macht angeboten.`,
}

func Execute() {
if err := RootCmd.Execute(); err != nil {
panic(err)
}
}

func init() {
cobra.OnInitialize(initConfig)
}

func initConfig() {
thisViper := viper.New()
thisViper.SetConfigName(".befehl")
configDir := os.Getenv("HOME")
if os.Getenv("BEFEHL_CONFIG_DIR") != "" {
configDir = os.Getenv("BEFEHL_CONFIG_DIR")
}
thisViper.AddConfigPath(configDir)
if err := thisViper.ReadInConfig(); err != nil {
fmt.Printf("Failed reading config (using defaults) [%s]: [%s]\n", thisViper.ConfigFileUsed(), err)
}
Config = thisViper
}
7 changes: 7 additions & 0 deletions executor/befehl_main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/sgsullivan/befehl/cmd"

func main() {
cmd.Execute()
}
5 changes: 5 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 59584ff

Please sign in to comment.