Reputation: 788
I have the following in my run.py script.
import time
NUM_PACKETS = 500
import random
import argparse
import threading
from scapy.all import sniff
from scapy.all import Ether, IP, IPv6, TCP
parser = argparse.ArgumentParser(description='run_test.py')
parser.add_argument('--random-dport',
help='Use a random TCP dest port for each packet',
action="store_true", default=False)
args = parser.parse_args()
class PacketQueue:
def __init__(self):
self.pkts = []
self.lock = threading.Lock()
self.ifaces = set()
def add_iface(self, iface):
self.ifaces.add(iface)
def get(self):
self.lock.acquire()
if not self.pkts:
self.lock.release()
return None, None
pkt = self.pkts.pop(0)
self.lock.release()
return pkt
def add(self, iface, pkt):
if iface not in self.ifaces:
return
self.lock.acquire()
self.pkts.append( (iface, pkt) )
self.lock.release()
queue = PacketQueue()
def pkt_handler(pkt, iface):
if IPv6 in pkt:
return
queue.add(iface, pkt)
class SnifferThread(threading.Thread):
def __init__(self, iface, handler = pkt_handler):
threading.Thread.__init__(self)
self.iface = iface
self.handler = handler
def run(self):
sniff(
iface = self.iface,
prn = lambda x: self.handler(x, self.iface)
)
class PacketDelay:
def __init__(self, bsize, bdelay, imin, imax, num_pkts = 100):
self.bsize = bsize
self.bdelay = bdelay
self.imin = imin
self.imax = imax
self.num_pkts = num_pkts
self.current = 1
def __iter__(self):
return self
def next(self):
if self.num_pkts <= 0:
raise StopIteration
self.num_pkts -= 1
if self.current == self.bsize:
self.current = 1
return random.randint(self.imin, self.imax)
else:
self.current += 1
return self.bdelay
pkt = Ether()/IP(dst='10.0.0.1', ttl=64)/TCP()
port_map = {
1: "veth3",
2: "veth5",
3: "veth7"
}
iface_map = {}
for p, i in port_map.items():
iface_map[i] = p
queue.add_iface("veth3")
queue.add_iface("veth5")
for p, iface in port_map.items():
t = SnifferThread(iface)
t.daemon = True
t.start()
import socket
send_socket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW,
socket.htons(0x03))
send_socket.bind((port_map[3], 0))
delays = PacketDelay(10, 5, 25, 100, NUM_PACKETS)
ports = []
print "Sending", NUM_PACKETS, "packets ..."
for d in delays:
# sendp is too slow...
# sendp(pkt, iface=port_map[3], verbose=0)
if args.random_dport:
pkt["TCP"].dport = random.randint(1025, 65535)
send_socket.send(str(pkt))
time.sleep(d / 1000.)
time.sleep(1)
iface, pkt = queue.get()
while pkt:
ports.append(iface_map[iface])
iface, pkt = queue.get()
print ports
print "DISTRIBUTION..."
for p in port_map:
c = ports.count(p)
print "port {}: {:>3} [ {:>5}% ]".format(p, c, 100. * c / NUM_PACKETS)
I tried running the script using the below command
./run_test.py '--random-dport' 2
It is throwing an unrecognized arguments error with the following message.
ubuntu@workshop:~/p4lang/tutorials/workshop_/mp$ sudo ./run.py 2
WARNING: No route found for IPv6 destination :: (no default route?)
usage: run.py [-h] [--random-dport]
run_test.py: error: unrecognized arguments: 2
ubuntu@workshop:~/p4lang/tutorials/workshop/mp$
What could be the problem here. I assume the rest of the run.py code is fine and the problem is in the above lines. I can add the rest of the code if asked for. I am stuck with this for a long time. Any inputs will help me!
Upvotes: 0
Views: 3008
Reputation: 9093
I'm not sure why you're using action="store_true"
.
Just do (instead of your add_argument
, the rest of the code is the same):
parser.add_argument("--random-dport", type=int,
help='Use a random TCP dest port for each packet',
default=1) #default to port 1
Then you can access the port by doing:
./run_test.py '--random-dport' 2
args.random-dport
>> 2
If you really want a true/false flag for a random port, as it seems your code is trying to achieve, you can use your original code, but then passing the 2
is pointless, as the presence of the random-dport
flag will store true into the variable:
./run_test.py
args.random-dport
>>False
It was not present->false
./run_test.py '--random-dport'
args.random-dport
>>True
It was present->true
./run_test.py '--random-dport' 2
>>error
It had an extra value->error.
The 2 is meaningless here, you are specifying you want a RANDOM dport, which means you can't pick a specific one. The previous section let's you pick a specific port.)
Either way check out the argparse tutorial. It is very helpful for giving you info on what you want to do.
Upvotes: 1