脚本编写练习:增加-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,写一个循环输出到文件就可以了