Skip to content

Commit

Permalink
scan
Browse files Browse the repository at this point in the history
  • Loading branch information
duzhenxun committed Jan 14, 2020
1 parent 6d4ef16 commit de773d2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea
log
.DS_Store
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/xs25cn/scanPort

go 1.13

require github.com/andlabs/ui v0.0.0-20180902183112-867a9e5a498d // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/andlabs/ui v0.0.0-20180902183112-867a9e5a498d h1:4ianvxb8s3oyizgjuWWxGuTAUU+6JStcvj6BuHS4PVY=
github.com/andlabs/ui v0.0.0-20180902183112-867a9e5a498d/go.mod h1:5G2EjwzgZUPnnReoKvPWVneT8APYbyKkihDVAHUi0II=
7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ func main() {
lib.Mkdir(*path)

//初始化
scanIP := scan.ScanIp{
Debug: true,
Timeout: *timeout,
Process: *process,
}
scanIP:=scan.NewScanIp(*timeout,*process,true)

ips, err := scanIP.GetAllIp(*ip)
if err != nil {
fmt.Println(err.Error())
Expand Down
26 changes: 16 additions & 10 deletions scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ import (

//ip 扫描
type ScanIp struct {
Debug bool
Timeout int
Process int
debug bool
timeout int
process int
}

func NewScanIp(timeout int,process int,debug bool) *ScanIp {
return &ScanIp{
debug:debug,
timeout:timeout,
process:process,
}
}

//获取开放端口号
Expand All @@ -30,14 +38,14 @@ func (s *ScanIp) GetIpOpenPort(ip string, port string) []int {
)
ports, _ := s.getAllPort(port)
total = len(ports)
if total < s.Process {
if total < s.process {
pageCount = total
} else {
pageCount = s.Process
pageCount = s.process
}
num = int(math.Ceil(float64(total) / float64(pageCount)))

s.sendLog(fmt.Sprintf("%v 【%v】需要扫描端口总数:%v 个,总协程:%v 个,每个协程处理:%v 个,超时时间:%v毫秒", time.Now().Format("2006-01-02 15:04:05"), ip, total, pageCount, num, s.Timeout))
s.sendLog(fmt.Sprintf("%v 【%v】需要扫描端口总数:%v 个,总协程:%v 个,每个协程处理:%v 个,超时时间:%v毫秒", time.Now().Format("2006-01-02 15:04:05"), ip, total, pageCount, num, s.timeout))
start := time.Now()
all := map[int][]int{}
for i := 1; i <= pageCount; i++ {
Expand Down Expand Up @@ -70,9 +78,7 @@ func (s *ScanIp) GetIpOpenPort(ip string, port string) []int {
}(v, k)
}
wg.Wait()

s.sendLog(fmt.Sprintf("%v 【%v】扫描结束,执行时长%.3fs , 所有开放的端口:%v", time.Now().Format("2006-01-02 15:04:05"), ip, time.Since(start).Seconds(), openPorts))
time.Sleep(time.Second * 1)
return openPorts
}

Expand Down Expand Up @@ -117,7 +123,7 @@ func (s *ScanIp) GetAllIp(ip string) ([]string, error) {

//记录日志
func (s *ScanIp) sendLog(str string) {
if s.Debug == true {
if s.debug == true {
fmt.Println(str)
}
}
Expand Down Expand Up @@ -168,7 +174,7 @@ func (s *ScanIp) filterPort(str string) (int, error) {

//查看端口号是否打开
func (s *ScanIp) isOpen(ip string, port int) bool {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Millisecond*time.Duration(s.Timeout))
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), time.Millisecond*time.Duration(s.timeout))
if err != nil {
if strings.Contains(err.Error(),"too many open files"){
fmt.Println("连接数超出系统限制!"+err.Error())
Expand Down

0 comments on commit de773d2

Please sign in to comment.