From 1163a41a9d206c3a8d51a2f8a32d6f4c3f800624 Mon Sep 17 00:00:00 2001 From: Daniel Ashton Date: Sat, 26 Sep 2020 15:36:22 +0200 Subject: [PATCH] initial release of jing-go --- README.md | 0 jing.go | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++ targets.txt | 7 +++ 3 files changed, 146 insertions(+) create mode 100644 README.md create mode 100644 jing.go create mode 100644 targets.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/jing.go b/jing.go new file mode 100644 index 0000000..e8bf690 --- /dev/null +++ b/jing.go @@ -0,0 +1,139 @@ +package main + +import ( + "bufio" + "fmt" + + //"io/ioutil" + "encoding/json" + "log" + "os" + + "github.com/go-ping/ping" + "github.com/pborman/getopt/v2" +) + +var ( + target = "127.0.0.1" + target_list = "" + sleep = 100 + privilege = false + count = 3 + timeout = 3000000000 + json_out = false + target_str = "" +) + +func init() { + + getopt.FlagLong(&target, "target", 't', "the target host to ping") + getopt.FlagLong(&target_list, "target_list", 'T', "define targetlist with hosts to ping") + getopt.FlagLong(&sleep, "sleep", 's', "sleep between packets") + getopt.FlagLong(&count, "count", 'c', "how many packets to send") + getopt.FlagLong(&privilege, "privilege", 'r', "admin/root privileges available") + getopt.FlagLong(&json_out, "json-output", 'J', "prefer json output") + // getopt.FlagLong(&timeout,"") +} + +type ping_args struct { + Target string + Target_list string + Timeout int + Count int + Privilege bool +} + +type ping_result struct { + Target string + Sent int + Recv int + Alive bool +} + +func pingme(target string, count int, timeout int) { + + var alive bool + pinger, err := ping.NewPinger(target) + + if err != nil { + panic(err) + + } + // fix this with time import and milliseconds + pinger.Timeout = 3000000000 + pinger.Count = 3 + + pinger.OnFinish = func(stats *ping.Statistics) { + if stats.PacketsRecv > 0 { + alive = true + } else { + alive = false + } + + if json_out == false { + fmt.Printf("%s (%s) %d/%d (Sent/Recv) Alive: %t\n", stats.Addr, stats.IPAddr, stats.PacketsSent, stats.PacketsRecv, alive) + } else { + + p_result := ping_result{ + + Target: target, + Sent: stats.PacketsSent, + Recv: stats.PacketsRecv, + Alive: alive, + } + json_result, err := json.Marshal(p_result) + if err != nil { + log.Fatalf("Unable to encode") + } + fmt.Println(string(json_result)) + } + } + + //pinger.SetPrivileged(true) + err = pinger.Run() + if err != nil { + + fmt.Printf("Failed to ping target host :%s", err) + } + +} + +func main() { + + getopt.Parse() + p_args := ping_args{ + Target: target, + Target_list: target_list, + Timeout: timeout, + Count: count, + Privilege: privilege} + + var json_data []byte + + json_data, err := json.Marshal(p_args) + if err != nil { + log.Println(err) + } + if json_out == true { + fmt.Println(string(json_data)) + } + + if target_list != "" { + + buf, err := os.Open(target_list) + if err != nil { + log.Fatal(err) + } + + snl := bufio.NewScanner(buf) + for snl.Scan() { + pingme(snl.Text(), 3, 3000000000) + + } + } else { + pingme(target, 3, 3000000000) + } + if json_out == false { + fmt.Printf("Done\n") + } +} diff --git a/targets.txt b/targets.txt new file mode 100644 index 0000000..eff26e6 --- /dev/null +++ b/targets.txt @@ -0,0 +1,7 @@ +127.0.0.1 +127.0.0.2 +127.0.0.3 +www.google.com +1.1.1.1 +8.8.8.8 +www.wired.com