//GO語言 實現埠掃描
//缺陷
//port 無法設定成全域性變數不知道怎麼設定的
//var l = list.New() 這個是陣列操作並不是訊息佇列 跟訊息佇列功能類似
//實現功能
//實現生成 IP段
//實現埠掃描
//實現引數傳入
//寫入檔案到本地
//main.go 58.215.20.30 58.215.201.30 80
//檔名 開始IP 結束IP 掃描埠
//QQ29295842 希望認識更多的朋友技術交流
//QQ群367196336 go golang WEB安全開發
// blog http://hi.baidu.com/alalmn
package main
import (
"container/list"
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
)
func ip2num(ip string) int {
canSplit := func(c rune) bool { return c == '.' }
lisit := strings.FieldsFunc(ip, canSplit) //[58 215 20 30]
//fmt.Println(lisit)
ip1_str_int, _ := strconv.Atoi(lisit[0])
ip2_str_int, _ := strconv.Atoi(lisit[1])
ip3_str_int, _ := strconv.Atoi(lisit[2])
ip4_str_int, _ := strconv.Atoi(lisit[3])
return ip1_str_int<<24 | ip2_str_int<<16 | ip3_str_int<<8 | ip4_str_int
}
func num2ip(num int) string {
ip1_int := (num & 0xff000000) >> 24
ip2_int := (num & 0x00ff0000) >> 16
ip3_int := (num & 0x0000ff00) >> 8
ip4_int := num & 0x000000ff
//fmt.Println(ip1_int)
data := fmt.Sprintf("%d.%d.%d.%d", ip1_int, ip2_int, ip3_int, ip4_int)
return data
}
func gen_ip(Aip1 int, Aip2 int) {
index := Aip1
for index < Aip2 {
//fmt.Println(num2ip(index))
// 入隊, 壓棧
ip_data := num2ip(index)
//fmt.Println(ip_data)
l.PushBack(ip_data)
index++
}
}
func text_add(name string, data string) { //向檔案中寫入資料 text_add("file2.txt", "qqqqqqqqqqqqqqqqqqqqqqq")
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0x644)
if err != nil {
panic(err)
}
defer f.Close()
_, err = f.WriteString(data)
_, err = f.WriteString("\r\n")
if err != nil {
panic(err)
}
}
//text_add("file2.txt", "qqqqqqqqqqqqqqqqqqqqqqq")
var l = list.New()
func socket_ip(host string, port string) bool {
var (
remote = host + ":" + port
)
tcpAddr, _ := net.ResolveTCPAddr("tcp4", remote) //轉換IP格式
//fmt.Printf("%s", tcpAddr)
conn, err := net.DialTCP("tcp", nil, tcpAddr) //檢視是否連線成功
if err != nil {
fmt.Printf("no==%s:%s\r\n", host, port)
return false
}
defer conn.Close()
fmt.Printf("ok==%s:%s\r\n", host, port)
return true
}
func for_ip(port string) {
now := time.Now()
year, mon, day := now.UTC().Date()
file_name := fmt.Sprintf("%d-%d-%d_%s", year, mon, day, port)
for { //死迴圈
if l.Len() <= 0 {
fmt.Println("跳出迴圈")
break //#跳出
}
// 出隊 從前讀取
i1 := l.Front()
l.Remove(i1)
IP, _ := i1.Value.(string)
if socket_ip(IP, port) {
//OK
//獲取當前 日期作為檔名 在把IP寫入進去
text_add(file_name+"_ok.txt", IP)
} //else {
// text_add(file_name+"_no.txt", IP)
// }
time.Sleep(time.Millisecond * 500) //納秒為單位
}
}
func main() {
argsLen := len(os.Args)
//fmt.Println(argsLen)
if argsLen != 4 {
fmt.Println("main.go 58.215.20.30 58.215.201.30 80")
} else {
gen_ip(ip2num(os.Args[1]), ip2num(os.Args[2]))
for index := 0; index < 200; index++ {
go for_ip(os.Args[3])
}
for {
time.Sleep(1 * time.Second) //納秒為單位
}
}
}