Moved ScannedResult to scan.go and implemented redfish probe

This commit is contained in:
David J. Allen 2023-10-09 11:20:36 -06:00
parent 4694353220
commit f59d68797d
2 changed files with 39 additions and 10 deletions

View file

@ -16,6 +16,7 @@ var (
begin uint8 begin uint8
end uint8 end uint8
subnets []string subnets []string
disableProbing bool
) )
var scanCmd = &cobra.Command{ var scanCmd = &cobra.Command{
@ -44,7 +45,7 @@ var scanCmd = &cobra.Command{
if threads <= 0 { if threads <= 0 {
threads = mathutil.Clamp(len(hostsToScan), 1, 255) threads = mathutil.Clamp(len(hostsToScan), 1, 255)
} }
probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout) probeStates := magellan.ScanForAssets(hostsToScan, portsToScan, threads, timeout, disableProbing)
for _, r := range probeStates { for _, r := range probeStates {
fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol) fmt.Printf("%s:%d (%s)\n", r.Host, r.Port, r.Protocol)
} }
@ -60,11 +61,12 @@ var scanCmd = &cobra.Command{
} }
func init() { func init() {
scanCmd.PersistentFlags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan") scanCmd.Flags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan")
scanCmd.PersistentFlags().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().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().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().BoolVar(&disableProbing, "disable-probing", false, "disable probing scanned results for BMC nodes")
rootCmd.AddCommand(scanCmd) rootCmd.AddCommand(scanCmd)
} }

View file

@ -3,14 +3,24 @@ package magellan
import ( import (
"fmt" "fmt"
"net" "net"
"net/http"
"sync" "sync"
"time" "time"
"github.com/bikeshack/magellan/internal/util"
) )
func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []BMCProbeResult { type ScannedResult struct {
results := []BMCProbeResult{} Host string `json:"host"`
Port int `json:"port"`
Protocol string `json:"protocol"`
State bool `json:"state"`
}
func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []ScannedResult {
results := []ScannedResult{}
for _, p := range ports { for _, p := range ports {
result := BMCProbeResult{ result := ScannedResult{
Host: host, Host: host,
Port: p, Port: p,
Protocol: "tcp", Protocol: "tcp",
@ -50,8 +60,8 @@ func GenerateHosts(subnet string, begin uint8, end uint8) []string {
return hosts return hosts
} }
func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []BMCProbeResult { func ScanForAssets(hosts []string, ports []int, threads int, timeout int, disableProbing bool) []ScannedResult {
results := make([]BMCProbeResult, 0, len(hosts)) results := make([]ScannedResult, 0, len(hosts))
done := make(chan struct{}, threads+1) done := make(chan struct{}, threads+1)
chanHost := make(chan string, threads+1) chanHost := make(chan string, threads+1)
// chanPort := make(chan int, threads+1) // chanPort := make(chan int, threads+1)
@ -66,8 +76,25 @@ func ScanForAssets(hosts []string, ports []int, threads int, timeout int) []BMCP
wg.Done() wg.Done()
return return
} }
s := rawConnect(host, ports, timeout, true) scannedResults := rawConnect(host, ports, timeout, true)
results = append(results, s...) if !disableProbing {
probeResults := []ScannedResult{}
for _, result := range scannedResults {
url := fmt.Sprintf("https://%s:%d/redfish/v1/", result.Host, result.Port)
res, _, err := util.MakeRequest(url, "GET", nil, nil)
if err != nil || res == nil {
continue
} else if res.StatusCode != http.StatusOK {
continue
} else {
probeResults = append(probeResults, result)
}
}
results = append(results, probeResults...)
} else {
results = append(results, scannedResults...)
}
} }
}() }()
} }