-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (66 loc) · 1.67 KB
/
main.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
package main
import (
"runtime"
"bufio"
"flag"
"io"
"log"
"strconv"
"os"
"os/exec"
)
const (
gopocLogfile string = "gopoc-%v.log"
gopocLogPrefix string = "gopoc: "
gopocScript string = "./gopoctester.sh"
gopocPowershell string = "gopoctester.ps1"
)
var (
sleepDurationSec int
logFile *os.File
logger *log.Logger
multiWriter io.Writer
)
func init() {
flag.IntVar(&sleepDurationSec, "sleepDurationSec", 5, "Set the number of seconds to sleep during cmd execution")
var logFileError error
logFile, logFileError = os.Create("gopoc.log")
if logFileError != nil {
panic(logFileError)
}
multiWriter = io.MultiWriter(logFile, os.Stdout)
logger = log.New(multiWriter, gopocLogPrefix, log.Ldate|log.Ltime|log.Lshortfile)
logger.SetOutput(multiWriter)
}
func main() {
flag.Parse()
defer logFile.Close() // close logFile after main() exits
logger.Printf("Go POC")
if runtime.GOOS == "windows" {
runCommand("powershell.exe", "-executionpolicy", "bypass", "-File", gopocPowershell, strconv.Itoa(sleepDurationSec))
} else {
runCommand(gopocScript, strconv.Itoa(sleepDurationSec))
}
logger.Printf("Done!")
}
func runCommand(name string, args ...string) {
logger.Printf("Executing Cmd:")
logger.Printf("\tname: %s", name)
logger.Printf("\targs: %s", args)
cmd := exec.Command(name, args...)
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
logger.Printf(err.Error())
panic(err)
}
multiReader := io.MultiReader(stdout, stderr)
in := bufio.NewScanner(multiReader)
for in.Scan() {
logger.Printf(in.Text())
}
if err := in.Err(); err != nil {
logger.Printf(err.Error())
}
cmd.Wait()
}