支持复杂命令的扫描器
import socket
from optparse import OptionParser
# 测试端口是否开放
def open(ip, port):
s = socket.socket()
try:
s.connect((ip, port))
return True
except:
return False
def scan(ip, port):
for x in port:
if open(ip, x):
print('%s host %s port is open'%(ip, x))
else:
print('%s host %s port is close' % (ip, x))
def rscan(ip, s, e):
for x in range(int(s), int(e) + 1):
if open(ip, x):
print('%s host %s port is open' % (ip, x))
else:
print('%s host %s port is close' % (ip, x))
def main():
usage = "usage: xxx.py -i <ip address> -p <port>" # 帮助
parse = OptionParser(usage=usage) # 添加usage方法,xxx.py -h就会出现上面的帮助
parse.add_option('-i', '--ip', type='string', dest='ipaddress', help='your target ip address here')
parse.add_option('-p', '--port', type='string', dest='port', help='your target port here')
(options, args) = parse.parse_args() # 获取选项和参数进行赋值
ip = options.ipaddress
port = options.port
defaultip = [135, 139, 445, 1433, 3306, 3389, 5944]
if ',' in port:
port = port.split(',')
a = []
for i in port:
a.append(int(i))
scan(ip, a)
elif '-' in port:
port = port.split('-')
rscan(ip, int(port[0]), int(port[1]))
elif 'all' == port:
rscan(ip, 1, 65535)
elif 'default' == port:
scan(ip, defaultip)
else:
print('Please input true')
if __name__ == "__main__":
main()
运行效果截图