Go language maker Port Scanner _golang

Source: Internet
Author: User

Go language to write the TCP port scanner, my first go program.

Git@osc:http://git.oschina.net/youshusoft/goscanner

To use the command:

Scanner StartIP [EndIP] Port thread

Parameter description:

StartIP Start IP
EndIP end IP, optional, no input means scan only StartIP
Port scan ports, single port: 3389; multiple ports: 1433,3389; continuous port: 135-3389
Thread maximum number of concurrent threads, maximum 2048

The scan results are saved in the same directory as the Result.txt, each start will be cleared out before the content.

Example one:
Scanner 58.96.172.22 58.96.172.220 80 512
Scans the 80 ports in the 58.96.172.22 to 58.96.172.220, the maximum concurrent thread 512.

Example two:
Scanner 58.96.172.22 58.96.172.220 21,5631 512
Scans the 21 and 5631 ports in the 58.96.172.22 to 58.96.172.220, the maximum concurrent thread 512.

Example three:
Scanner 58.96.172.22 58.96.172.220 1-520 512
Scans the 1 to 520 ports in the 58.96.172.22 to 58.96.172.220, the maximum concurrent thread 512.

Example four:
Scanner 58.96.172.22 1-520 512
Scans the 1 to 520 ports in the 58.96.172.22, the maximum concurrent thread 512.

Package main import ("FMT" "StrConv" "Flag" "Strings" "NET" "OS")/** scan address */var Ipaddrs chan string = make (chan str
ing)//scan result var results chan string = Make (Chan string)/thread number var thread chan int = make (chan int) var nowthread int; Close program var clo chan bool = make (chan bool)//Save Results Func Writeresult () {fileName: = "Result.txt" fout,err: = OS. Create (FileName) If Err!= nil{//File creation failed FMT. Println (fileName + "Create Error")} defer fout. Close () S,ok: = <-result for; OK; {Fout. 
WriteString (S + "\ r \ n") S,ok = <-result}//Notification process exit CLO <-true;
  Func Runscan () {t,ok: = <-Thread nowthread = t, starting scan thread based on thread parameters; If ok{for I: = 0;i < nowthread;i++{go Scan (StrConv. Itoa (i)}}//Waiting for thread to terminate for;<-thread = = 0;
{nowthread--if Nowthread = = 0{//All threads terminated, closing result write, exiting program close (results) break}}}
    /** Scan Thread */func Scan (ThreadId string) {s,ok: = <-ipaddrs for;ok;{ Fmt. Println ("[Thread-"+ threadId +"] Scan: "+ S" _,err: = Net. Dial ("TCP", s) if Err = = nil{//Port open result <-s} s,ok = <-ipaddrs} fmt.
Println ("[thread-" + ThreadId + "] end") thread <-0; //Get Next IP func nextip (IP string) string{ips: = Strings.
  Split (IP, ".")
  var i int; For i = Len (IPs)-1;i >= 0;i--{n,_: = StrConv. Atoi (Ips[i]) if n >= 255{//rounding Ips[i] = "1"}else{//+1 n++ ips[i] = StrConv.
  Itoa (n) Break}} if I = = -1{//All IP segments are carried, indicating that the IP itself has exceeded the range return ""; ip = "" Leng: = Len (IPs) for I: = 0;i < leng;i++{if i = = leng-1{IP + ips[i]}else{IP +
    = Ips[i] + "." } return IP}//Generate IP Address list func processip (Startip,endip string) []string{var ips = make ([]string,0) for; StartIP != Endip;startip = Nextip (startip) {if StartIP!= "" {ips = append (Ips,startip)}} ips = Append (Ips,sta RTIP) return IPs}//Processing parameters func processFlag (Arg []string) {//Start IP, end IP var startip,endip string//Port var ports []int = make ([]int,0) Index: = 0 start Ip = Arg[index] Si: = net. PARSEIP (startip) if si = = nil{//Start IP is not legitimate FMT. Println ("' StartIP ' Setting error") return} index++ EndIP = arg[index] ei: = net. 
  PARSEIP (EndIP) if (ei = = nil) {//no end IP specified, that is, scan only one IP EndIP = StartIP}else{index++} tmpport: = Arg[index] If strings. Index (Tmpport, "-")!= -1{//Continuous port tmpports: = Strings. Split (Tmpport, "-") var startport,endport int var err error startport,err = StrConv. Atoi (Tmpports[0]) If err!= nil | | Startport < 1 | | Startport > 65535{//Start port not valid return} If Len (tmpports) >= 2{//Specify end Port Endport,err = StrConv. Atoi (tmpports[1]) If err!= nil | | Endport < 1 | | Endport > 65535 | | Endport < startport{//end port is not legal FMT.
 Println ("' Endport ' Setting error") return}}else{//no end port specified     Endport = 65535} for I: = 0;startport + i <= endport;i++{ports = append (Ports,startport + i)} }else{//One or more ports PS: = strings. Split (Tmpport, ",") for I: = 0;i < Len (PS); i++{p,err: = StrConv. Atoi (Ps[i]) If Err!= nil{//port is not legal FMT. Println ("' Port ' Setting error") return} ports = Append (Ports,p)}} index++ T,err: = Strcon V.atoi (Arg[index]) if (err!= nil) {//thread is not valid FMT.
  Println ("' Thread ' Setting error") return}//MAX thread 2048 if T < 1{t = 1;
  }else if T > 2048{t = 2048; ///Routing boot thread number thread <-t//Generate scan address list ips: = Processip (startip,endip) IL: = Len (IPs) for I: = 0; I < il;i++{pl: = len (ports) for J: = 0;j < pl;j++{Ipaddrs <-Ips[i] + ":" + StrConv. Itoa (Ports[j])} close (Ipaddrs)} func main () {flag. Parse () if flag. Narg ()!= 3 && flag. Narg ()!= 4{//parameter is not legal FMT.
  PRINTLN ("Parameter error")  return}//get parameter args: = make ([]string,0,4) for I: = 0;i < flag. Narg (); i++{args = append (Args,flag. 
  ARG (i)}//Start scan thread Go Runscan ()//start result write thread Go Writeresult ()//Parameter processing Processflag (args)/wait for exit instruction <-CLO; Fmt.
 Println ("Exit")}

The above is the entire contents of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.