迭代多行变量以返回所有 ips

我正在尝试为初学者golang项目构建一个简单的端口扫描程序,并且大多数代码都按预期工作,但是我在函数方面遇到了问题,可以返回逐行生成的所有ip并将其传递给另一个函数以扫描它们 当前返回第一行只有一种方法可以循环访问变量以逐行返回所有ips?ipv4_gen()ipv4_gen()ip


package main


import (

    "fmt"

    "log"

    "net"

    "strconv"

    "time"

)


func ipv4_gen() string {

    ip, ipnet, err := net.ParseCIDR("192.168.1.1/24")

    if err != nil {

        log.Fatal(err)

    }

    for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {

        return ip.String()

    }

    return ip.String()

}


func inc(ip net.IP) {

    for j := len(ip) - 1; j >= 0; j-- {

        ip[j]++

        if ip[j] > 0 {

            break

        }

    }

}


func port_scanner(host string) {

    port := strconv.Itoa(80)

    conn, err := net.DialTimeout("tcp", host+":"+port, 1*time.Second)

    if err == nil {

        fmt.Println("Host:", conn.RemoteAddr().String(), "open")

        conn.Close()

    }

}


func main() {

    port_scanner(ipv4_gen())

}

如果要运行代码,请单击下面的链接 https://play.golang.org/p/YWvgnowZzhI


阿晨1998
浏览 156回答 3
3回答

慕运维8079593

要从函数返回多个结果(尤其是在生成可能生成数千个结果时),在 Go 中使用通道是惯用语。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "net"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "time")func ipv4_gen(out chan string) {&nbsp; &nbsp; ip, ipnet, err := net.ParseCIDR("192.168.1.1/24")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {&nbsp; &nbsp; &nbsp; &nbsp; out <- ip.String()&nbsp; &nbsp; }&nbsp; &nbsp; close(out)}func inc(ip net.IP) {&nbsp; &nbsp; for j := len(ip) - 1; j >= 0; j-- {&nbsp; &nbsp; &nbsp; &nbsp; ip[j]++&nbsp; &nbsp; &nbsp; &nbsp; if ip[j] > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func port_scanner(host string) {&nbsp; &nbsp; port := strconv.Itoa(80)&nbsp; &nbsp; conn, err := net.DialTimeout("tcp", host+":"+port, 1*time.Second)&nbsp; &nbsp; if err == nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Host:", conn.RemoteAddr().String(), "open")&nbsp; &nbsp; &nbsp; &nbsp; conn.Close()&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; ips := make(chan string)&nbsp; &nbsp; go ipv4_gen(ips)&nbsp; &nbsp; for s := range ips {&nbsp; &nbsp; &nbsp; &nbsp; port_scanner(s)&nbsp; &nbsp; }}

慕斯王

虽然通道对于在 goroutine 之间进行通信很有用,但使用 goroutines 将地址迭代与扫描分开并不会让您受益。频道减慢了速度。更简单、更快捷的解决方案是使用迭代器对象。任何语言的迭代器对象都旨在完全按照您的请求执行操作,以“迭代ip变量以返回所有ip”。下面是使用 IPAddress Go 库执行此操作的代码。免责声明:我是项目经理。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "github.com/seancfoley/ipaddress-go/ipaddr"&nbsp; &nbsp; "net"&nbsp; &nbsp; "time")func ipv4_gen() ipaddr.IPAddressIterator {&nbsp; &nbsp; block := ipaddr.NewIPAddressString("192.168.1.0/24").GetAddress()&nbsp; &nbsp; iterator := block.WithoutPrefixLen().Iterator()&nbsp; &nbsp; iterator.Next() // skip the first address 192.168.1.0&nbsp; &nbsp; return iterator}func port_scanner(iterator ipaddr.IPAddressIterator) {&nbsp; &nbsp; for iterator.HasNext() {&nbsp; &nbsp; &nbsp; &nbsp; conn, err := net.DialTimeout("tcp",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Sprint(iterator.Next(), ":", 80), time.Second)&nbsp; &nbsp; &nbsp; &nbsp; if err == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Host:", conn.RemoteAddr().String(), "open")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.Close()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; port_scanner(ipv4_gen())}

红糖糍粑

如果我正确地理解了你的问题,那么这应该有效。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "net"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "time")func ipv4_gen(ch chan<- string) {&nbsp; &nbsp; ip, ipnet, err := net.ParseCIDR("192.168.1.1/24")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {&nbsp; &nbsp; &nbsp; &nbsp; ch <- ip.String()&nbsp; &nbsp; }&nbsp; &nbsp; close(ch)}func inc(ip net.IP) {&nbsp; &nbsp; for j := len(ip) - 1; j >= 0; j-- {&nbsp; &nbsp; &nbsp; &nbsp; if ip[j]++; ip[j] > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}func port_scanner(hosts <-chan string) {&nbsp; &nbsp; for host := range hosts {&nbsp; &nbsp; &nbsp; &nbsp; port := strconv.Itoa(80)&nbsp; &nbsp; &nbsp; &nbsp; conn, err := net.DialTimeout("tcp", host+":"+port, 1*time.Second)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Host:", conn.RemoteAddr().String(), "open")&nbsp; &nbsp; &nbsp; &nbsp; conn.Close()&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; ip := make(chan string)&nbsp; &nbsp; go ipv4_gen(ip)&nbsp; &nbsp; port_scanner(ip)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go