3 from abc import abstractmethod, ABCMeta
4 from cStringIO import StringIO
7 def ppp(headline, packet):
8 """ Return string containing the output of scapy packet.show() call. """
10 old_stdout = sys.stdout
14 sys.stdout = old_stdout
18 def ppc(headline, capture, limit=10):
19 """ Return string containing ppp() printout for a capture.
21 :param headline: printed as first line of output
22 :param capture: packets to print
23 :param limit: limit the print to # of packets
28 if limit < len(capture):
29 tail = "\nPrint limit reached, %s out of %s packets printed" % (
32 body = "".join([ppp("Packet #%s:" % count, p)
33 for count, p in zip(range(0, limit), capture)])
34 return "%s\n%s%s" % (headline, body, tail)
37 def ip4_range(ip4, s, e):
38 tmp = ip4.rsplit('.', 1)[0]
39 return ("%s.%d" % (tmp, i) for i in range(s, e))
42 def ip4n_range(ip4n, s, e):
43 ip4 = socket.inet_ntop(socket.AF_INET, ip4n)
44 return (socket.inet_pton(socket.AF_INET, ip)
45 for ip in ip4_range(ip4, s, e))
48 class NumericConstant(object):
49 __metaclass__ = ABCMeta
54 def __init__(self, value):
64 if self._value in self.desc_dict:
65 return self.desc_dict[self._value]
70 """ Generic test host "connected" to VPPs interface. """
79 """ IPv4 address - string """
84 """ IPv4 address of remote host - raw, suitable as API parameter."""
85 return socket.inet_pton(socket.AF_INET, self._ip4)
89 """ IPv6 address - string """
94 """ IPv6 address of remote host - raw, suitable as API parameter."""
95 return socket.inet_pton(socket.AF_INET6, self._ip6)
97 def __init__(self, mac=None, ip4=None, ip6=None):