mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Added format flag to convery output to JSON and slighly changed scanning logic
This commit is contained in:
parent
a2b841b401
commit
b3ba75ea9a
3 changed files with 52 additions and 26 deletions
13
cmd/list.go
13
cmd/list.go
|
|
@ -1,7 +1,9 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
|
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
|
||||||
|
|
||||||
|
|
@ -17,12 +19,19 @@ var listCmd = &cobra.Command{
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("failed toget probe results: %v\n", err)
|
logrus.Errorf("failed toget probe results: %v\n", err)
|
||||||
}
|
}
|
||||||
for _, r := range probeResults {
|
format = strings.ToLower(format)
|
||||||
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
if format == "json" {
|
||||||
|
b, _ := json.Marshal(probeResults)
|
||||||
|
fmt.Printf("%s\n", string(b))
|
||||||
|
} else {
|
||||||
|
for _, r := range probeResults {
|
||||||
|
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
listCmd.Flags().StringVar(&format, "format", "", "set the output format")
|
||||||
rootCmd.AddCommand(listCmd)
|
rootCmd.AddCommand(listCmd)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import (
|
||||||
var (
|
var (
|
||||||
currentUser *user.User
|
currentUser *user.User
|
||||||
accessToken string
|
accessToken string
|
||||||
|
format string
|
||||||
timeout int
|
timeout int
|
||||||
concurrency int
|
concurrency int
|
||||||
ports []int
|
ports []int
|
||||||
|
|
|
||||||
64
cmd/scan.go
64
cmd/scan.go
|
|
@ -1,10 +1,12 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
magellan "github.com/OpenCHAMI/magellan/internal"
|
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||||
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
|
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
|
||||||
|
|
@ -26,41 +28,56 @@ var scanCmd = &cobra.Command{
|
||||||
Use: "scan",
|
Use: "scan",
|
||||||
Short: "Scan for BMC nodes on a network",
|
Short: "Scan for BMC nodes on a network",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Printf("subnets in cmd: %v\n", subnets)
|
var (
|
||||||
// set hosts to use for scanning
|
hostsToScan []string
|
||||||
hostsToScan := []string{}
|
portsToScan []int
|
||||||
|
)
|
||||||
|
|
||||||
|
// start by adding `--host` supplied to scan
|
||||||
if len(hosts) > 0 {
|
if len(hosts) > 0 {
|
||||||
hostsToScan = hosts
|
hostsToScan = hosts
|
||||||
} else {
|
|
||||||
for i, subnet := range subnets {
|
|
||||||
if len(subnet) <= 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(subnetMasks) < i+1 {
|
|
||||||
subnetMasks = append(subnetMasks, net.IP{255, 255, 255, 0})
|
|
||||||
}
|
|
||||||
|
|
||||||
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, &subnetMasks[i])...)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// set ports to use for scanning
|
// add hosts from `--subnets` and `--subnet-mask`
|
||||||
portsToScan := []int{}
|
for i, subnet := range subnets {
|
||||||
|
// subnet string is empty so nothing to do here
|
||||||
|
if subnet == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: should we check if subnet is valid here or is it done elsewhere (maybe in GenerateHosts)?
|
||||||
|
|
||||||
|
// no subnet masks supplied so add a default one for class C private networks
|
||||||
|
if len(subnetMasks) < i+1 {
|
||||||
|
subnetMasks = append(subnetMasks, net.IP{255, 255, 255, 0})
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate a slice of all hosts to scan from subnets
|
||||||
|
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, &subnetMasks[i])...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add ports to use for scanning
|
||||||
if len(ports) > 0 {
|
if len(ports) > 0 {
|
||||||
portsToScan = ports
|
portsToScan = ports
|
||||||
} else {
|
} else {
|
||||||
portsToScan = append(magellan.GetDefaultPorts(), ports...)
|
// no ports supplied so only use defaults
|
||||||
|
portsToScan = magellan.GetDefaultPorts()
|
||||||
}
|
}
|
||||||
|
|
||||||
// scan and store probe data in dbPath
|
// scan and store scanned data in cache
|
||||||
if concurrency <= 0 {
|
if concurrency <= 0 {
|
||||||
concurrency = mathutil.Clamp(len(hostsToScan), 1, 255)
|
concurrency = mathutil.Clamp(len(hostsToScan), 1, 255)
|
||||||
}
|
}
|
||||||
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, concurrency, timeout, disableProbing, verbose)
|
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, concurrency, timeout, disableProbing, verbose)
|
||||||
if verbose {
|
if verbose {
|
||||||
for _, r := range probeStates {
|
format = strings.ToLower(format)
|
||||||
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
if format == "json" {
|
||||||
|
b, _ := json.Marshal(probeStates)
|
||||||
|
fmt.Printf("%s\n", string(b))
|
||||||
|
} else {
|
||||||
|
for _, r := range probeStates {
|
||||||
|
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,10 +94,9 @@ var scanCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
scanCmd.Flags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan")
|
scanCmd.Flags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan")
|
||||||
scanCmd.Flags().IntSliceVar(&ports, "port", []int{}, "set the ports to scan")
|
scanCmd.Flags().IntSliceVar(&ports, "port", []int{}, "set the ports to scan")
|
||||||
// scanCmd.Flags().Uint8Var(&begin, "begin", 0, "set the starting point for range of IP addresses")
|
scanCmd.Flags().StringVar(&format, "format", "", "set the output format")
|
||||||
// scanCmd.Flags().Uint8Var(&end, "end", 255, "set the ending point for range of IP addresses")
|
|
||||||
scanCmd.Flags().StringSliceVar(&subnets, "subnet", []string{}, "set additional subnets")
|
scanCmd.Flags().StringSliceVar(&subnets, "subnet", []string{}, "set additional subnets")
|
||||||
scanCmd.Flags().IPSliceVar(&subnetMasks, "subnet-mask", []net.IP{}, "set the subnet masks to use for network")
|
scanCmd.Flags().IPSliceVar(&subnetMasks, "subnet-mask", []net.IP{}, "set the subnet masks to use for network (must match number of subnets)")
|
||||||
scanCmd.Flags().BoolVar(&disableProbing, "disable-probing", false, "disable probing scanned results for BMC nodes")
|
scanCmd.Flags().BoolVar(&disableProbing, "disable-probing", false, "disable probing scanned results for BMC nodes")
|
||||||
|
|
||||||
viper.BindPFlag("scan.hosts", scanCmd.Flags().Lookup("host"))
|
viper.BindPFlag("scan.hosts", scanCmd.Flags().Lookup("host"))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue