Python Forum
How do i listen to loopback address on my local computer, without a port
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i listen to loopback address on my local computer, without a port
#2
(May-23-2023, 08:30 PM)billykid999 Wrote: I want to listent to my loopback address on my windows PC, without attaching it to a port. I want to listent to any messages, or ICMP pings sourced from address 10.0.0.1, as the 10.0.0.0 network is an internal network which shoots off a ping to the loopback in cases of network congestion. I'm using routers to send a ping, and they can't ping ports. (they can traceroute ports, which i may try to implement later). The python listener will then trigger based on a ping from 10.0.0.1. Any suggestions on how to do this? I read back last summer on socket programming and have done a few tutorials, but these usually refer to just that: socket programming, establishing connections, and binding to ports. I want a simple ip address listener, that listens to ip address loopback for a ping sourced from 10.0.0.1 ~ any tips?

To listen for ICMP ping packets originating from a specific IP address (such as 10.0.0.1) on the loopback interface in Python, you can use the socket module along with the AF_PACKET address family.

Here's an example code snippet that demonstrates how you can achieve this:

import socket
import struct

def listen_for_ping(source_ip):
    # Create a raw socket using AF_PACKET address family
    sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(3))

    # Set the interface to loopback
    sock.bind(("lo", 0))

    while True:
        # Receive a packet
        packet, _ = sock.recvfrom(65535)

        # Extract the IP header from the packet
        ip_header = packet[14:34]

        # Unpack the source and destination IP addresses from the header
        source_address = socket.inet_ntoa(ip_header[12:16])
        destination_address = socket.inet_ntoa(ip_header[16:20])

        # Check if the packet is an ICMP ping from the specified source IP
        if (
            source_address == source_ip
            and destination_address == "127.0.0.1"  # Loopback address
            and packet[23] == 1  # ICMP protocol (1 is for ICMP)
            and packet[34] == 8  # ICMP type 8 is Echo Request (ping)
        ):
            print("Received ping from", source_ip)

# Start listening for pings from 10.0.0.1 on the loopback interface
listen_for_ping("10.0.0.1")
In this code, I created a raw socket using the socket.socket function with the AF_PACKET address family. I then bind the socket to the loopback interface ("lo") using the bind method.

Inside the infinite loop, I receive packets using recvfrom. I then extract the IP header from the packet and check if the packet is an ICMP ping from the specified source IP address (10.0.0.1) and destined for the loopback address (127.0.0.1).

If the conditions are met, I print a message indicating that a ping was received from the specified source IP.

Please note that running this code may require administrative/root privileges as it involves listening on a raw socket. Additionally, ICMP packets might be filtered by firewalls or security software, so ensure that your setup allows ICMP traffic on the loopback interface.

Hope this helps.
Reply


Messages In This Thread
RE: How do i listen to loopback address on my local computer, without a port - by DigiGod - May-26-2023, 12:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Listen TCP and send data to websockets neoboby 3 3,084 Feb-13-2023, 09:47 PM
Last Post: Vadanane
  Your p“Timeout !!!!” when I enter a known good IP address when running port scanner James2000k 8 2,907 Aug-06-2022, 10:42 AM
Last Post: Larz60+
  error opening port from local to internet looney99 3 2,884 Oct-16-2020, 02:01 PM
Last Post: BeanieW
  UDP Listen without Bind() GaryFunk 3 8,557 Mar-29-2018, 02:28 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020