zoukankan      html  css  js  c++  java
  • python 实现简单的端口扫描器

      1 #coding=utf-8
      2 import socket
      3 import time
      4 import sys
      5  
      6 def portScanner(ip,port):
      7     server = (ip,port)
      8     sockfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      9     sockfd.settimeout(0.5)
     10     ret = sockfd.connect_ex(server)  #返回0则成功
     11     if not ret:
     12         sockfd.close()
     13         print '%s:%s is opened...' % (ip,port)
     14     else:
     15         sockfd.close()
     16         pass
     17     return ''
     18  
     19 def ip2num(ip):
     20     lp = [int(x) for x in ip.split('.')]
     21     return lp[0] << 24 | lp[1] << 16 | lp[2] << 8 |lp[3]
     22  
     23 def num2ip(num):
     24     ip = ['','','','']
     25     ip[3] = (num & 0xff)
     26     ip[2] = (num & 0xff00) >> 8
     27     ip[1] = (num & 0xff0000) >> 16
     28     ip[0] = (num & 0xff000000) >> 24
     29     return '%s.%s.%s.%s' % (ip[0],ip[1],ip[2],ip[3])
     30  
     31 def iprange(ip1,ip2):
     32     num1 = ip2num(ip1)
     33     num2 = ip2num(ip2)
     34     tmp = num2 - num1
     35     if tmp < 0:
     36         return None
     37     else:
     38         return num1,num2,tmp
     39 
     40 def scanip(ip):
     41     port = 0
     42     res = ''
     43     while port < 65536:
     44         s=socket.socket()
     45         address = str(ip)
     46         try:
     47             s.connect((address,port))
     48             res = res + str(port) + '
    '
     49             #print str(port) + ' is opening'
     50         except socket.error,e:
     51             pass
     52             #print str(port) + ' Not Opened'
     53         port = port + 1
     54     print res
     55  
     56 if __name__ == '__main__':
     57     #输出运行时的时间
     58     print 'start time : %s' % time.ctime(time.time())
     59     #没有传入参数,打印使用方法
     60     if len(sys.argv) == 1:
     61         print 'Usage 1:scannerports.py startip endip port (get the specfic given opening port from startip to endip)'
     62         print 'Usage 2:scannerports.py destip (get all the opening ports from destip) '
     63         print 'Usage 3:scannerports.py startip endip (get all the opening ports from startip to endip)'
     64         sys.exit()
     65     #传入了一个参数ip,扫描这个ip打开的端口
     66     if len(sys.argv) == 2:
     67         ip = sys.argv[1]
     68         print str(ip) + ' is scanned...'
     69         #调用扫描端口方法
     70         scanip(ip)
     71     elif len(sys.argv) == 3:#扫描网段内所有ip所有端口
     72         res = ()
     73         startip = sys.argv[1]#网段起始ip
     74         endip = sys.argv[2]#网段结束ip
     75         res = iprange(startip,endip)#判断网段大小
     76         
     77         if not res:
     78             print 'endip must be bigger than startone'
     79             sys.exit()
     80         elif res[2] == 0:#只有一个ip
     81             print str(ip) + ' is scanned...'
     82             scanip(ip)
     83         else:#循环扫描网段中的每个ip
     84             startipn = ip2num(startip)
     85             for x in xrange(endip-startip+1):
     86                 print str(ip) + ' is scanned...'
     87                 startipnum = startipn + x
     88                 scanip(num2ip(startipnum))#扫描ip的端口
     89     else:#扫描指定网段内的指定端口
     90         res = ()
     91         startip = sys.argv[1]#网段起始ip
     92         endip = sys.argv[2]#网段结束ip
     93         port = int(sys.argv[3])#指定端口
     94         res = iprange(startip,endip)#判断网段大小
     95         if not res:
     96             print 'endip must be bigger than startone'
     97             sys.exit()
     98         elif res[2] == 0:#只有一个ip
     99             portScanner(startip,port)
    100         else:#循环扫描网段中的每个ip
    101             startipn = ip2num(startip)
    102             for x in xrange(int(res[2])+1):
    103                 startipnum = startipn + x
    104                 portScanner(num2ip(startipnum),port)#扫描ip的指定端口
    105     print 'end time : %s' % time.ctime(time.time())#输出结束时间
    106      
  • 相关阅读:
    Redis 2种持久化模式的缺陷
    我看过得最易懂的一段AOP的解释
    mysql-高性能索引策略
    几款效率神器助你走上人生巅峰
    shell脚本报错:"[: =: unary operator expected"
    CentOS7中使用iptables
    php foreach用法和实例
    shell 学习四十五天---xargs
    chain issues incorrect order,EXtra certs,Contains anchor
    Ubuntu 能ping通DNS 地址 无法解析域名
  • 原文地址:https://www.cnblogs.com/gudygudy/p/10178723.html
Copyright © 2011-2022 走看看