udp协议扫描
代码部分
def scan(ip):
try:
packet = IP(dst=ip)/UDP(dport=56789)
result = sr1(packet,timeout=0.5,verbose=0)
if int(result[IP].proto) == 0x01:
time.sleep(0.1)
print(ip + ' ' + "on line")
except:
return
def main():
usage = "Usage: arp扫描.py -f -i "
parse = OptionParser(usage=usage)
parse.add_option("-f", "--file", type="string", dest="filename", help="specify the IP address file")
parse.add_option("-i", '--ip', type="string", dest="address", help="specify the IP address")
(option, args) = parse.parse_args()
filename = option.filename
address = option.address
if filename:
if not os.path.exists(filename):
print("The file does not exist. Please enter it again")
sys.exit()
with open(filename, "r") as f:
for i in f.readlines():
ip = i.strip()
t = Thread(target=scan, args=(ip,))
t.start()
if address:
prefix = address.split(".")[0] + '.' + address.split(".")[1] + '.' + address.split(".")[2] + "."
for i in range(0, 255):
ip = prefix + str(i)
t = Thread(target=scan, args=(ip,))
t.start()
整体代码
import os
import time
from optparse import OptionParser
from scapy.all import *
def scan(ip):
try:
packet = IP(dst=ip)/UDP(dport=56789)
result = sr1(packet,timeout=0.5,verbose=0)
if int(result[IP].proto) == 0x01:
time.sleep(0.1)
print(ip + ' ' + "on line")
except:
return
def main():
usage = "Usage: arp扫描.py -f -i "
parse = OptionParser(usage=usage)
parse.add_option("-f", "--file", type="string", dest="filename", help="specify the IP address file")
parse.add_option("-i", '--ip', type="string", dest="address", help="specify the IP address")
(option, args) = parse.parse_args()
filename = option.filename
address = option.address
if filename:
if not os.path.exists(filename):
print("The file does not exist. Please enter it again")
sys.exit()
with open(filename, "r") as f:
for i in f.readlines():
ip = i.strip()
t = Thread(target=scan, args=(ip,))
t.start()
if address:
prefix = address.split(".")[0] + '.' + address.split(".")[1] + '.' + address.split(".")[2] + "."
for i in range(0, 255):
ip = prefix + str(i)
t = Thread(target=scan, args=(ip,))
t.start()
if __name__ == '__main__':
main()
运行效果