From 88b540972cd5d4ae937463c579229294367caf63 Mon Sep 17 00:00:00 2001 From: cdryzun Date: Mon, 18 Sep 2023 10:28:35 +0800 Subject: [PATCH] git commit -m "first commit" --- .gitignore | 182 ++++++++++++++++++++++++++++++++++++++++++++++ Taskfile.yaml | 27 +++++++ cmd/nmap.go | 28 +++++++ cmd/root.go | 30 ++++++++ go.mod | 15 ++++ go.sum | 23 ++++++ main.go | 16 ++++ nmap/scan/scan.go | 63 ++++++++++++++++ 8 files changed, 384 insertions(+) create mode 100644 .gitignore create mode 100644 Taskfile.yaml create mode 100644 cmd/nmap.go create mode 100644 cmd/root.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 nmap/scan/scan.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f62d57 --- /dev/null +++ b/.gitignore @@ -0,0 +1,182 @@ +# ---> macOS +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# ---> VisualStudioCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +# ---> JetBrains +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +# ---> Go +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +# ---> Terraform +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +crash.*.log + +# Exclude all .tfvars files, which are likely to contain sensitive data, such as +# password, private keys, and other secrets. These should not be part of version +# control as they are data points which are potentially sensitive and subject +# to change depending on the environment. +*.tfvars +*.tfvars.json + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Include override files you do wish to add to version control using negated pattern +# !example_override.tf + +# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan +# example: *tfplan* + +# Ignore CLI configuration files +.terraformrc +terraform.rc + +bin/ \ No newline at end of file diff --git a/Taskfile.yaml b/Taskfile.yaml new file mode 100644 index 0000000..78e6082 --- /dev/null +++ b/Taskfile.yaml @@ -0,0 +1,27 @@ +version: '3' +vars: + BASE_DIR: + sh: dirname $(pwd) + PROJECT_NAME: + sh: pwd|sed "s#{{.BASE_DIR}}/##g" + GOOS: '' + GOARCH: '' +tasks: + default: + cmds: + - task: deps + - task: build:binary + silent: true + deps: + desc: Install all dependencies (except dredd requirements) + cmds: + - task: deps:be + deps:be: + desc: application dependencies + cmds: + - go mod tidy + build:binary: + desc: Build a binary for the current architecture + platforms: [amd64] + cmds: + - env CGO_ENABLED=0 GOOS={{ .GOOS }} GOARCH={{ .GOARCH }} go build -o ./bin/{{.PROJECT_NAME}}{{ if eq OS "windows" }}.exe{{ end }} \ No newline at end of file diff --git a/cmd/nmap.go b/cmd/nmap.go new file mode 100644 index 0000000..58e7566 --- /dev/null +++ b/cmd/nmap.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "git.treesir.pub/DevOps/devops-sing/nmap/scan" + "github.com/spf13/cobra" +) + +var ( + getIP int + cidr string +) + +var nmapCmd = &cobra.Command{ + Use: "nmap", + Short: "use nmap scan cidr", + Long: `use nmap scan cidr`, + Run: func(cmd *cobra.Command, args []string) { + scan.Pcidr(cidr, getIP) + }, +} + +func init() { + nmapCmd.Flags().IntVarP(&getIP, "getIP", "g", 1, "get ip address") + nmapCmd.Flags().StringVarP(&cidr, "cidr", "c", "192.168.8.0/24", "cidr address") + nmapCmd.MarkFlagRequired("getIP") + nmapCmd.MarkFlagRequired("cidr") + +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..dc6d384 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "devops-sing", + Short: "Devops-sing is a tool for devops", + Long: `Devops-sing is a tool for devops.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Execute the command devops-sing -h to view the usage method.") + }, +} + +func init() { + rootCmd.AddCommand() + rootCmd.AddCommand(nmapCmd) + +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6be018f --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module git.treesir.pub/DevOps/devops-sing + +go 1.21.0 + +require ( + github.com/Ullaakut/nmap/v3 v3.0.2 + github.com/korylprince/ipnetgen v1.0.1 + github.com/spf13/cobra v1.7.0 +) + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sync v0.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f4ff061 --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/Ullaakut/nmap/v3 v3.0.2 h1:AqQ9UYxLWzYZTv/rzMzVn8+LIgFGxGi+4h+3pDkFOII= +github.com/Ullaakut/nmap/v3 v3.0.2/go.mod h1:dd5K68P7LHc5nKrFwQx6EdTt61O9UN5x3zn1R4SLcco= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/korylprince/ipnetgen v1.0.1 h1:FIv2YOg3LBjyiwGV/b9m/U5QEe6qP8yNic4l7AJznSY= +github.com/korylprince/ipnetgen v1.0.1/go.mod h1:S0y7uqM4hX9g3NstxMbp6qmfHDSSXuEwcp05bb2zaiw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..c1ba90b --- /dev/null +++ b/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "math/rand" + "time" + + "git.treesir.pub/DevOps/devops-sing/cmd" +) + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +func main() { + cmd.Execute() +} diff --git a/nmap/scan/scan.go b/nmap/scan/scan.go new file mode 100644 index 0000000..4978e8c --- /dev/null +++ b/nmap/scan/scan.go @@ -0,0 +1,63 @@ +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]) + } +}