mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Added function implementations to generate hosts by subnet and CIDR
This commit is contained in:
parent
994bf07716
commit
555ecf679d
3 changed files with 92 additions and 6 deletions
14
cmd/scan.go
14
cmd/scan.go
|
|
@ -16,6 +16,7 @@ var (
|
||||||
begin uint8
|
begin uint8
|
||||||
end uint8
|
end uint8
|
||||||
subnets []string
|
subnets []string
|
||||||
|
subnetMasks []string
|
||||||
disableProbing bool
|
disableProbing bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,8 +29,12 @@ var scanCmd = &cobra.Command{
|
||||||
if len(hosts) > 0 {
|
if len(hosts) > 0 {
|
||||||
hostsToScan = hosts
|
hostsToScan = hosts
|
||||||
} else {
|
} else {
|
||||||
for _, subnet := range subnets {
|
for i, subnet := range subnets {
|
||||||
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, begin, end)...)
|
if len(subnetMasks) > 0 {
|
||||||
|
hostsToScan = append(hostsToScan, magellan.GenerateHostsWithSubnet(subnet, subnetMasks[i])...)
|
||||||
|
} else {
|
||||||
|
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet)...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,9 +68,10 @@ 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().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().StringSliceVar(&subnetMasks, "subnet-mask", []string{}, "set the subnet masks to use for network")
|
||||||
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")
|
||||||
|
|
||||||
rootCmd.AddCommand(scanCmd)
|
rootCmd.AddCommand(scanCmd)
|
||||||
|
|
|
||||||
|
|
@ -50,11 +50,79 @@ func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []Scan
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateHosts(subnet string, begin uint8, end uint8) []string {
|
func GenerateHosts(subnet string, mask string, begin uint8, end uint8) []string {
|
||||||
hosts := []string{}
|
hosts := []string{}
|
||||||
ip := net.ParseIP(subnet).To4()
|
ip := net.ParseIP(subnet).To4()
|
||||||
for i := begin; i < end; i++ {
|
for i := begin; i < end; i++ {
|
||||||
ip[3] = byte(i)
|
ip = util.GetNextIP(ip, 1)
|
||||||
|
hosts = append(hosts, fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]))
|
||||||
|
}
|
||||||
|
return hosts
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateHostsWithCIDR(subnet string) []string {
|
||||||
|
// check for network with valid CIDR
|
||||||
|
ip, network, err := net.ParseCIDR(subnet)
|
||||||
|
if err != nil && (network != nil || ip != nil) {
|
||||||
|
network.Mask = ip.DefaultMask()
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for IP with no CIDR
|
||||||
|
if ip == nil {
|
||||||
|
ip = net.ParseIP(subnet)
|
||||||
|
if ip == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if network == nil {
|
||||||
|
network = &net.IPNet{
|
||||||
|
Mask: ip.DefaultMask(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get all IP addresses in network
|
||||||
|
return generateHosts(ip, network.Mask)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateHostsWithSubnet(subnet string, subnetMask string) []string {
|
||||||
|
// if no subnet mask, use a default 24-bit mask (for now)
|
||||||
|
if subnetMask != "" {
|
||||||
|
ip, network, err := net.ParseCIDR(subnet)
|
||||||
|
if err != nil && (network != nil || ip != nil) {
|
||||||
|
network.Mask = ip.DefaultMask()
|
||||||
|
}
|
||||||
|
// check for IP with no CIDR
|
||||||
|
if ip == nil {
|
||||||
|
ip = net.ParseIP(subnet)
|
||||||
|
if ip == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if network == nil {
|
||||||
|
network = &net.IPNet{
|
||||||
|
Mask: ip.DefaultMask(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return generateHosts(ip, network.Mask)
|
||||||
|
} else {
|
||||||
|
ip := net.ParseIP(subnetMask)
|
||||||
|
if ip != nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
return generateHosts(ip, ip.DefaultMask())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateHosts(ip net.IP, mask net.IPMask) []string {
|
||||||
|
// get all IP addresses in network
|
||||||
|
ones, _ := mask.Size()
|
||||||
|
hosts := []string{}
|
||||||
|
fmt.Printf("ones: %d\n", ones)
|
||||||
|
for i := 0; i < 32-ones; i++ {
|
||||||
|
// ip[3] = byte(i)
|
||||||
|
ip = util.GetNextIP(ip, 1)
|
||||||
hosts = append(hosts, fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]))
|
hosts = append(hosts, fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]))
|
||||||
}
|
}
|
||||||
return hosts
|
return hosts
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -17,6 +18,17 @@ func PathExists(path string) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetNextIP(ip net.IP, inc uint) net.IP {
|
||||||
|
i := ip.To4()
|
||||||
|
v := uint(i[0])<<24 + uint(i[1])<<16 + uint(i[2])<<8 + uint(i[3])
|
||||||
|
v += inc
|
||||||
|
v3 := byte(v & 0xFF)
|
||||||
|
v2 := byte((v >> 8) & 0xFF)
|
||||||
|
v1 := byte((v >> 16) & 0xFF)
|
||||||
|
v0 := byte((v >> 24) & 0xFF)
|
||||||
|
return net.IPv4(v0, v1, v2, v3)
|
||||||
|
}
|
||||||
|
|
||||||
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
||||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue