我一直在编写一个小脚本来选择扫描主机以查找特定端口范围,并在尝试测试自定义范围时遇到了一些奇怪的错误:
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()
我知道我处理和转换所选端口的方式不是最好的选择,但我在那里很忙。上述错误的可能原因是什么?