简体   繁体   English

Python 端口扫描器

[英]Python Portscanner

I've been writing a little script to scan a host for a specific port range by choice and ran into some weird error while trying to test the custom range:我一直在编写一个小脚本来通过选择扫描主机的特定端口范围,并在尝试测试自定义范围时遇到一些奇怪的错误:

Traceback (most recent call last):
File "/root/Desktop/ScannerV1Beta.py", line 74, in <module>
   result = s.connect_ex((targetIP, i))

File "/usr/lib/python2.7/socket.py", line 224, in meth
   return getattr(self._sock,name)(*args)

File "/usr/lib/python2.7/socket.py", line 170, in _dummy
   raise error(EBADF, 'Bad file descriptor')

   socket.error: [Errno 9] Bad file descriptor
#!/usr/bin/env python

from socket import *


target = raw_input('Enter host or ip to scan: ')
targetIP = gethostbyname(target)
choice = raw_input('Please Choose the port range:\n (1) 1 - 1023 \n (2) 1024 - 5000 \n (3) 5000 - 10000 \n (c) custom \n ')
print 'Starting scan on host ', targetIP



if choice == '1':
    for i in range(1, 134):
        s = socket(AF_INET, SOCK_STREAM)

        result = s.connect_ex((targetIP, i))

        if(result == 0) :
            print 'Port %d: OPEN' % (i)
        else:
            print 'Port %d: CLOSED' % (i)
        s.close()

    for i in range(133, 1024):
        s = socket(AF_INET, SOCK_STREAM)

        result = s.connect_ex((targetIP, i))

        if(result == 0) :
            print 'Port %d: OPEN' % (i)
        else:
            print 'Port %d: CLOSED' % (i)

        s.close()



elif choice == '2':
    for i in range(1024, 5000):
        s = socket(AF_INET, SOCK_STREAM)

        result = s.connect_ex((targetIP, i))

        if(result == 0) :
            print 'Port %d: OPEN' % (i)
        else:
            print 'Port %d: CLOSED' % (i)

        s.close()

if choice == '3':
    for i in range(5000, 10000):
        s = socket(AF_INET, SOCK_STREAM)

        result = s.connect_ex((targetIP, i))

        if(result == 0) :
            print 'Port %d: OPEN' % (i)
        else:
            print 'Port %d: CLOSED' % (i)

        s.close()
if choice == 'c':
    firstPort = raw_input('Please specify starting Port: ')
    endPort = raw_input('Please specify ending Port(max 65535): ')
    endPort = int(endPort)
    firstPort = int(firstPort)
    s = socket(AF_INET, SOCK_STREAM)

    if firstPort in range(0, 65535):
        if endPort in range(0, 65535):
            for i in range(firstPort, endPort):
                result = s.connect_ex((targetIP, i))
                if(result == 0) :
                    print 'Port %d: OPEN' % (i)
                else:
                    print 'Port %d: CLOSED' % (i)
                s.close()
        else:
            print('Error: Ports not in range!!')
            s.close()
    else:
        print('Error:Ports not in range!!')
        s.close()

i know the way i handled and converted the chosen ports is not the best option but i was quite on a run there.我知道我处理和转换所选端口的方式不是最好的选择,但我在那里很忙。 What might be the possible cause for said errors?上述错误的可能原因是什么?

In the last "for" you are closing socket, but don't creating new. 在最后一个“ for”中,您要关闭套接字,但不要创建新套接字。 Rewrite the last "for" cycle as: 将最后一个“ for”周期重写为:

for i in range(firstPort, endPort):
    s = socket(AF_INET, SOCK_STREAM)
    result = s.connect_ex((targetIP, i))
    if(result == 0) :
        print 'Port %d: OPEN' % (i)
    else:
        print 'Port %d: CLOSED' % (i)
    s.close()

portscanner端口扫描器


#!/usr/bin/python3
import socket
import os
import platform
def cls():
    if platform.system() == 'Linux':
      os.system("clear")
    else:
        os.system("cls")
def main():
    cls()
    print("---[ Port Scanner ]---\n")
    host = input("Enter Host: ")
    print("\n")
    ports = [21,22,23,25,80,111,443] # ports
    for port in ports:
       s = socket.socket()
       ch = s.connect_ex((host,port))
       if ch.status_code == 200:
         print(f"Port: {port} Open!")
       else:
           print(f"Port: {port} Filter!")
if __name__ == '__main__':
  try:
     main()
  except (KeyboardInterrupt,EOFError):
      print("\nStop !!!")
      exit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM