How to use ethernet_packet method in autotest

Best Python code snippet using autotest_python

benchmark_pcap.py

Source:benchmark_pcap.py Github

copy

Full Screen

1#!/usr/bin/python2# -*- coding: utf-8 -*-3"""4===== 5Benchmark PCAP Creaion6===== 78Benchmark the creation and parsing of a biug pcap file9"""10__author__ = "Diarmuid Collins"11__copyright__ = "Copyright 2018"12__version__ = "0.0.1"13__maintainer__ = "Diarmuid Collins"14__email__ = "dcollins@curtisswright.com"15__status__ = "Production"161718import sys19sys.path.append("..")2021import time22import struct23import AcraNetwork.IENA as iena24import AcraNetwork.iNetX as inetx25import AcraNetwork.Pcap as pcap26import AcraNetwork.SimpleEthernet as SimpleEthernet27import argparse282930parser = argparse.ArgumentParser(description='Benchmark the creation of pcap files containing packets')31parser.add_argument('--type', required=True, type=str,choices=["udp","iena","inetx"], help='The type of _payload, udp iena or inetx')32parser.add_argument('--ignoretime',required=False, action='store_true', default=False)33args = parser.parse_args()3435# constants36PCAP_FNAME = "output_test.pcap"37PACKETS_TO_WRITE = 5000038PAYLOAD_SIZE = 1300 # size of the _payload in bytes39HEADER_SIZE = {'udp': 58, 'inetx':86,'iena':74}4041# Write out a pcapfile with each inetx and iena packet generated42mypcap = pcap.Pcap(PCAP_FNAME, mode='w')43mypcap.write_global_header()44ethernet_packet = SimpleEthernet.Ethernet()45ethernet_packet.srcmac = 0x00112233445546ethernet_packet.dstmac = 0x55443322110047ethernet_packet.type = SimpleEthernet.Ethernet.TYPE_IP48ip_packet = SimpleEthernet.IP()49ip_packet.dstip = "235.0.0.2"50ip_packet.srcip = "127.0.0.1"51udp_packet = SimpleEthernet.UDP()52udp_packet.dstport = 4422535455# Fixed _payload for both56payload = (struct.pack(">B",5) * PAYLOAD_SIZE)5758if args.type == "inetx":59 # Create an inetx packet60 avionics_packet = inetx.iNetX()61 avionics_packet.inetxcontrol = inetx.iNetX.DEF_CONTROL_WORD62 avionics_packet.pif = 063 avionics_packet.streamid = 0xdc64 avionics_packet.sequence = 065 avionics_packet.payload = payload66else:67 # Create an iena packet68 avionics_packet = iena.IENA()69 avionics_packet.key = 0xdc70 avionics_packet.keystatus = 071 avionics_packet.endfield = 0xbeef72 avionics_packet.sequence = 073 avionics_packet.payload = payload74 avionics_packet.status = 07576packets_written = 07778start_time = time.time()79while packets_written < PACKETS_TO_WRITE:8081 if args.type == "udp":82 udp_packet.srcport = 499983 udp_packet.payload = payload84 else:85 if args.ignoretime:86 currenttime = 087 else:88 currenttime = int(time.time())89 if args.type == "iena":90 avionics_packet.sequence = (avionics_packet.sequence + 1) % 6553691 udp_packet.srcport = 500092 else:93 avionics_packet.sequence = (avionics_packet.sequence + 1) % 0x10000000094 udp_packet.srcport = 50019596 avionics_packet.setPacketTime(currenttime)97 udp_packet.payload = avionics_packet.pack()9899 ip_packet.payload = udp_packet.pack()100 ethernet_packet.payload = ip_packet.pack()101 record = pcap.PcapRecord()102 if args.ignoretime:103 record.usec = 0104 record.sec = 0105 else:106 record.setCurrentTime()107 record.packet = ethernet_packet.pack()108 mypcap.write(record)109110 packets_written += 1111112mypcap.close()113end_time = time.time()114print("INFO: Wrote {} packets of type {} with _payload of {} bytes in {} seconds".format(PACKETS_TO_WRITE,args.type,PAYLOAD_SIZE,end_time-start_time))115print("INFO: Wrote {} bytes in {}".format((HEADER_SIZE[args.type]+PAYLOAD_SIZE)*PACKETS_TO_WRITE,end_time-start_time))116print("INFO: Wrote {} packets per second".format(PACKETS_TO_WRITE/(end_time-start_time))) ...

Full Screen

Full Screen

flow-gen.py

Source:flow-gen.py Github

copy

Full Screen

1from socket import *2def sendeth(ethernet_packet, payload, interface = "eth0"):3 """Send raw Ethernet packet on interface."""4 s = socket(AF_PACKET, SOCK_RAW)5 # From the docs: "For raw packet6 # sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])"7 s.bind((interface, 0))8 return s.send(ethernet_packet + payload)9def pack(byte_sequence):10 """Convert list of bytes to byte string."""11 return b"".join(map(chr, byte_sequence))12if __name__ == "__main__":13 # Note that this example contains HARDCODED packets, meaning that14 # it will ONLY work on the system it was designed for.15 # I got these values by sending a ping while running Wireshark.16 # You can do so yourself. Another way to construct these manually is to use17 # the impacket library (sudo pip install impacket)18 # src=fe:ed:fa:ce:be:ef, dst=52:54:00:12:35:02, type=0x0800 (IP)19 # src=00:3a:9c:07:4d:87, dst=00:3a:9c:03:b8:c720 ethernet_packet = [0x00, 0x3a, 0x9c, 0x03, 0xb8, 0xc7, 0x00, 0x3a, 0x9c,21 0x07, 0x4d, 0x87, 0x08, 0x00]22 ethernet_packet = [0x52, 0x54, 0x00, 0x12, 0x35, 0x02, 0xfe, 0xed, 0xfa,23 0xce, 0xbe, 0xef, 0x08, 0x00]24 # src=10.0.2.15, dst=195.88.54.16 (vg.no), checksum, etc.25 # src=2.2.2.2, dst=2.2.2.1 (vg.no), checksum, etc.26 ipv4_header = [0x45, 0x00, 0x00, 0x54, 0x05, 0x9f, 0x40, 0x00, 0x40, 0x01,27 0x2f, 0x93, 0x0a, 0x00, 0x02, 0x0f, 0xc3, 0x58, 0x36, 0x10]28 # echo (ping) request, checksum 2b45, etc29 icmp_ping = [0x08, 0x00, 0x2b, 0x45, 0x11, 0x22, 0x00, 0x02, 0xa9, 0xf4, 0x5c,30 0x53, 0x00, 0x00, 0x00, 0x00, 0xf5, 0x7b, 0x01, 0x00, 0x00, 0x00,31 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,32 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,33 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,34 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37]35 payload = "".join(map(chr, ipv4_header + icmp_ping))36 # Construct Ethernet packet with an IPv4 ICMP PING request as payload37 r = sendeth(pack(ethernet_packet),38 pack(ipv4_header + icmp_ping), "Eth1-49")...

Full Screen

Full Screen

week12.py

Source:week12.py Github

copy

Full Screen

1# Using dpkt for parsing2import dpkt3import socket4import tabulate5# Global data structure for storing the dns6dns_db = {}7# function to modulize reading from the the pcap8def read_from_pcap(open_pcap):9 counter = 110 for _, packet in open_pcap:11 ethernet_packet = dpkt.ethernet.Ethernet(packet)12 if isinstance(ethernet_packet.data, dpkt.ip.IP):13 if isinstance(ethernet_packet.data.data, dpkt.udp.UDP):14 udp = ethernet_packet.data.data15 if udp.sport == 53 or udp.dport == 53:16 dns = dpkt.dns.DNS(udp.data)17 if dns.opcode == dpkt.dns.DNS_QUERY and dns.qr == dpkt.dns.DNS_R:18 query_list = []19 answer_list = []20 for query in dns.qd:21 if query.type == dpkt.dns.DNS_A:22 query_list.append(query.name)23 for answer in dns.an:24 if answer.type == dpkt.dns.DNS_A:25 answer_list.append(socket.inet_ntoa(answer.ip))26 if query_list and answer_list:27 dns_db[counter] = {}28 dns_db[counter]['Query'] = query_list29 dns_db[counter][ 'Answer'] = answer_list30 counter += 131def create_data():32 result_list = []33 for k, v in dns_db.items():34 loop_result = []35 loop_result.append(k) 36 loop_result.append(v [ 'Query']) 37 loop_result.append(v[ 'Answer'])38 result_list.append(loop_result)39 40 return result_list41def display(result_list):42 print(tabulate.tabulate(result_list, headers=['Packet ID', 'Query', 'Answers']))43def main():44 pcap = r'assets\week11_dns.pcap'45 # Read file from source 46 open_pcap = dpkt.pcap.Reader(open(pcap, 'rb'))47 read_from_pcap(open_pcap)48 data = create_data()49 display(data)50if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful