GOLANG 端口扫描示例
2014-02-14 16:25
323 查看
这段代码主要来自网络,源代码存在问题,现在已经修复,可以直接编译运行
package main
import (
"net"
"fmt"
"os"
"runtime"
"time"
"strconv"
)
func loop(startport, endport int, inport chan int) {
for i := startport; i <= endport; i++{
inport <- i
}
}
func scanner(inport, outport, out chan int, ip net.IP, endport int){
for{
in := <- inport
tcpaddr := &net.TCPAddr{IP:ip,Port:in}
conn, err := net.DialTCP("tcp", nil, tcpaddr)
if err != nil {
outport <- 0
}else{
outport <- in
conn.Close()
}
if in == endport{
out <- in
}
}
}
func main() {
runtime.GOMAXPROCS(4)
inport := make(chan int)
starttime := time.Now().Unix()
outport := make(chan int)
out := make(chan int)
collect := []int{}
if len(os.Args) != 4 {
fmt.Println("Usage: scanner.exe IP startport endport")
fmt.Println("Endport must be larger than startport")
os.Exit(0)
}
ip := net.ParseIP(os.Args[1])
if(os.Args[3] < os.Args[2]) {
fmt.Println("Usage: scanner IP startport endport")
fmt.Println("Endport must be larger than startport")
os.Exit(0)
}
fmt.Printf("the ip is %s \r\n",ip)
startport, _:= strconv.Atoi(os.Args[2])
endport, _ := strconv.Atoi(os.Args[3])
fmt.Printf("%d---------%d\r\n",startport,endport)
go loop(startport, endport, inport)
for{
select {
case <-out:
fmt.Println(collect)
endtime := time.Now().Unix()
fmt.Println("The scan process has spent ",endtime - starttime,"second")
os.Exit(0)
default:
go scanner(inport, outport, out, ip, endport)
port := <- outport
if port != 0{
collect = append(collect, port)
}
}
}
}
package main
import (
"net"
"fmt"
"os"
"runtime"
"time"
"strconv"
)
func loop(startport, endport int, inport chan int) {
for i := startport; i <= endport; i++{
inport <- i
}
}
func scanner(inport, outport, out chan int, ip net.IP, endport int){
for{
in := <- inport
tcpaddr := &net.TCPAddr{IP:ip,Port:in}
conn, err := net.DialTCP("tcp", nil, tcpaddr)
if err != nil {
outport <- 0
}else{
outport <- in
conn.Close()
}
if in == endport{
out <- in
}
}
}
func main() {
runtime.GOMAXPROCS(4)
inport := make(chan int)
starttime := time.Now().Unix()
outport := make(chan int)
out := make(chan int)
collect := []int{}
if len(os.Args) != 4 {
fmt.Println("Usage: scanner.exe IP startport endport")
fmt.Println("Endport must be larger than startport")
os.Exit(0)
}
ip := net.ParseIP(os.Args[1])
if(os.Args[3] < os.Args[2]) {
fmt.Println("Usage: scanner IP startport endport")
fmt.Println("Endport must be larger than startport")
os.Exit(0)
}
fmt.Printf("the ip is %s \r\n",ip)
startport, _:= strconv.Atoi(os.Args[2])
endport, _ := strconv.Atoi(os.Args[3])
fmt.Printf("%d---------%d\r\n",startport,endport)
go loop(startport, endport, inport)
for{
select {
case <-out:
fmt.Println(collect)
endtime := time.Now().Unix()
fmt.Println("The scan process has spent ",endtime - starttime,"second")
os.Exit(0)
default:
go scanner(inport, outport, out, ip, endport)
port := <- outport
if port != 0{
collect = append(collect, port)
}
}
}
}
相关文章推荐
- 绝对的无root,首发用于logo上文字的字体终于出现在手机上了
- mongo update
- TOJ-2191-Golden Flower
- 支持多平台!2D和3D游戏引擎Godot宣布开源
- MFC Ribbon Category实现tab页切换视图功能
- 创建第一个django异常处理
- golang flag 用法示例
- cgo 用法示例
- go slice
- golang发送邮件,带附件
- golang 发送邮件(不带附件)
- 关于golang 文件映射的API
- golang 自定义错误示例
- golang zip 操作示例
- NOTEPAD++ 配置golang 高亮显示
- GOLANG 遍历文件夹
- 支持多平台的2D和3D游戏引擎Godot开源
- Changing the Auto-Logout Timeout in SSH
- Gold Coins
- New algorimths