脚本编写学习日记:Go(2)

脚本编写练习:增加-o 参数,可写入输出结果到指定文件,支持从文件中读取多个ip或单个ip,可指定范围端口或多个特定端口

如果不需要批量或者输出文件可以用"脚本编写学习日记:Go(1)"中的脚本,或者直接>>也可以

(从channel改成了sync.WaitGroup,批量ip扫描的输出结果比较乱,最好从输出的文件中查找某一特定ip)

package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"net"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"
)

var (
	ip       string
	ports    string
	filepath string
	result   string
)

func init() {
	flag.StringVar(&ip, "i", "", "Enter the ip")
	flag.StringVar(&ports, "p", "21,22", "Enter the port")
	flag.StringVar(&filepath, "f", "ip.txt", "Enter the filepath")
	flag.StringVar(&result, "o", "", "Enter the result filename")
}

func main() {
	flag.Parse()
	var wg sync.WaitGroup
	starttime := time.Now()
	if strings.TrimSpace(ip) != "" {
		wg.Add(1)
		go scanonly(ip, ports, &wg)
	} else if strings.TrimSpace(filepath) != "" {
		ips, err := os.Open(filepath)
		if err != nil {
			panic(err)
		}
		defer func(ips *os.File) {
			err = ips.Close()
			if err != nil {

			}
		}(ips)
		ipline := bufio.NewReader(ips)
		for {
			content, _, errs := ipline.ReadLine()
			if errs == io.EOF {
				break
			}
			content1 := string(content)
			wg.Add(1)
			go scanonly(content1, ports, &wg)
		}
	} else {
		fmt.Println("傻鸟,-h 看一下")
	}
	wg.Wait()
	endtime := time.Since(starttime)
	fmt.Println("扫描时长:", endtime)
}

func scanonly(ip string, ports string, wg *sync.WaitGroup) {
	defer wg.Done()
	var num int
	fmt.Println("当前检验的ip :", ip)
	if strings.Contains(ports, ",") || (!strings.Contains(ports, ",") && !strings.Contains(ports, "-")) {
		portlist := strings.Split(ports, ",")
		for _, port := range portlist {
			num = 0
			wen := make(chan int)
			portint, _ := strconv.Atoi(port)
			wg.Add(1)
			go scan(ip, portint, wen)
			port_true := <-wen
			if port_true != 0 {
				num = 1
				fmt.Println(port_true, "端口 is open!!!")
				port_true1 := strconv.Itoa(port_true)
				if strings.TrimSpace(result) != "" {
					go saveResult(result, ip, port_true1, wg, num)
				} else {
					wg.Done()
				}
			} else {
				fmt.Println(port, "端口 is close")
				if strings.TrimSpace(result) != "" {
					go saveResult(result, ip, port, wg, num)
				} else {
					wg.Done()
				}
			}
		}
	} else if strings.Contains(ports, "-") {
		portlist := strings.Split(ports, "-")
		start, _ := strconv.Atoi(portlist[0])
		end, _ := strconv.Atoi(portlist[1])
		portlists := make([]int, 0)
		for i := start; i <= end; i++ {
			portlists = append(portlists, i)
		}
		for _, port := range portlists {
			num = 0
			wen := make(chan int)
			wg.Add(1)
			go scan(ip, port, wen)
			port_true := <-wen
			if port_true != 0 {
				num = 1
				fmt.Println(port_true, "端口 is open!!!")
				port_true1 := strconv.Itoa(port_true)
				if strings.TrimSpace(result) != "" {
					go saveResult(result, ip, port_true1, wg, num)
				} else {
					wg.Done()
				}
			} else {
				fmt.Println(port, "端口 is close")
				if strings.TrimSpace(result) != "" {
					port1 := strconv.Itoa(port)
					go saveResult(result, ip, port1, wg, num)
				} else {
					wg.Done()
				}
			}
		}
	}
}

func scan(ip string, port int, wen chan int) {
	conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Second*5)
	if err != nil {
		wen <- 0
		return
	}
	defer conn.Close()
	wen <- port
}

func saveResult(result string, ip string, port string, wg *sync.WaitGroup, num int) {
	defer wg.Done()
	file, err := os.OpenFile(result, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
	if err != nil {
		fmt.Println("Error opening file:", err)
		return
	}
	defer file.Close()
	if num == 1 {
		_, err = file.WriteString(ip + ":" + port + "  : open" + "\n")
	} else {
		_, err = file.WriteString(ip + ":" + port + "  : close" + "\n")
	}
	if err != nil {
		fmt.Println("Error writing to file:", err)
	}
}

 powershell中查找特定ip方法

 linux中查找特定ip的方法

 想扫描某一网段的ip,写一个循环输出到文件就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值