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
64
cmd/scan.go
64
cmd/scan.go
|
|
@ -1,10 +1,12 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
magellan "github.com/OpenCHAMI/magellan/internal"
|
||||
"github.com/OpenCHAMI/magellan/internal/db/sqlite"
|
||||
|
|
@ -26,41 +28,56 @@ var scanCmd = &cobra.Command{
|
|||
Use: "scan",
|
||||
Short: "Scan for BMC nodes on a network",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("subnets in cmd: %v\n", subnets)
|
||||
// set hosts to use for scanning
|
||||
hostsToScan := []string{}
|
||||
var (
|
||||
hostsToScan []string
|
||||
portsToScan []int
|
||||
)
|
||||
|
||||
// start by adding `--host` supplied to scan
|
||||
if len(hosts) > 0 {
|
||||
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
|
||||
portsToScan := []int{}
|
||||
// add hosts from `--subnets` and `--subnet-mask`
|
||||
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 {
|
||||
portsToScan = ports
|
||||
} 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 {
|
||||
concurrency = mathutil.Clamp(len(hostsToScan), 1, 255)
|
||||
}
|
||||
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, concurrency, timeout, disableProbing, verbose)
|
||||
if verbose {
|
||||
for _, r := range probeStates {
|
||||
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
|
||||
format = strings.ToLower(format)
|
||||
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() {
|
||||
scanCmd.Flags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts 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().Uint8Var(&end, "end", 255, "set the ending point for range of IP addresses")
|
||||
scanCmd.Flags().StringVar(&format, "format", "", "set the output format")
|
||||
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")
|
||||
|
||||
viper.BindPFlag("scan.hosts", scanCmd.Flags().Lookup("host"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue