64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package scan
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"github.com/Ullaakut/nmap/v3"
|
|
ipnet "github.com/korylprince/ipnetgen"
|
|
)
|
|
|
|
func Pcidr(cidr_addres string, get_ip int) {
|
|
|
|
scanner, err := nmap.NewScanner(
|
|
context.Background(),
|
|
nmap.WithTargets(cidr_addres),
|
|
nmap.WithPingScan(),
|
|
)
|
|
if err != nil {
|
|
log.Fatalf("unable to create nmap scanner: %v", err)
|
|
}
|
|
|
|
result, warnings, err := scanner.Run()
|
|
if len(*warnings) > 0 {
|
|
log.Printf("run finished with warnings: %s\n", *warnings) // Warnings are non-critical errors from nmap.
|
|
}
|
|
if err != nil {
|
|
log.Fatalf("nmap scan failed: %v", err)
|
|
}
|
|
|
|
var slices []string
|
|
for _, host := range result.Hosts {
|
|
slices = append(slices, host.Addresses[0].String())
|
|
}
|
|
|
|
var cird_ip_address []string
|
|
gen, err := ipnet.New(cidr_addres)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for ip := gen.Next(); ip != nil; ip = gen.Next() {
|
|
cird_ip_address = append(cird_ip_address, ip.String())
|
|
}
|
|
|
|
// Delete the first and last IP in cird_ip_address as reserved addresses.
|
|
cird_ip_address = cird_ip_address[1 : len(cird_ip_address)-1]
|
|
|
|
// Clear the addresses occupied by scanning in cird_ip_address.
|
|
for _, ip := range slices {
|
|
for i, v := range cird_ip_address {
|
|
if ip == v {
|
|
cird_ip_address = append(cird_ip_address[:i], cird_ip_address[i+1:]...)
|
|
}
|
|
}
|
|
}
|
|
|
|
// getIP
|
|
for i := 0; i < get_ip; i++ {
|
|
fmt.Println(cird_ip_address[i])
|
|
}
|
|
}
|