Skip to content

Commit

Permalink
update: subnet batch run
Browse files Browse the repository at this point in the history
  • Loading branch information
Esonhugh committed Mar 19, 2024
1 parent ec0b8fe commit c16c7d8
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions cmd/subnet/subnet.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package subnet

import (
"net"
"os"

command "github.com/esonhugh/k8spider/cmd"
"github.com/esonhugh/k8spider/define"
"github.com/esonhugh/k8spider/pkg"
"github.com/esonhugh/k8spider/pkg/mutli"
"github.com/esonhugh/k8spider/pkg/scanner"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -28,20 +30,45 @@ var SubNetCmd = &cobra.Command{
log.Warnf("ParseStringToIPNet failed: %v", err)
return
}
var records define.Records = scanner.ScanSubnet(ipNets)
if records == nil || len(records) == 0 {
log.Warnf("ScanSubnet Found Nothing: %v", err)
return
}
if command.Opts.OutputFile != "" {
f, err := os.OpenFile(command.Opts.OutputFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Warnf("OpenFile failed: %v", err)
}
defer f.Close()
records.Print(log.StandardLogger().Writer(), f)
if command.Opts.BatchMode {
BatchRun(ipNets)
} else {
records.Print(log.StandardLogger().Writer())
Run(ipNets)
}
},
}

func Run(net *net.IPNet) {
var records define.Records = scanner.ScanSubnet(net)
if records == nil || len(records) == 0 {
log.Warnf("ScanSubnet Found Nothing")
return
}
printResult(records)
}

func BatchRun(net *net.IPNet) {
scan := mutli.NewSubnetScanner()
var finalRecord []define.Record
for r := range scan.ScanSubnet(net) {
finalRecord = append(finalRecord, r...)
}
if len(finalRecord) == 0 {
log.Warn("ScanSubnet Found Nothing")
return
}
printResult(finalRecord)
}

func printResult(records define.Records) {
if command.Opts.OutputFile != "" {
f, err := os.OpenFile(command.Opts.OutputFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Warnf("OpenFile failed: %v", err)
}
defer f.Close()
records.Print(log.StandardLogger().Writer(), f)
} else {
records.Print(log.StandardLogger().Writer())
}
}

0 comments on commit c16c7d8

Please sign in to comment.