Home > Uncategorized > networking: python script to repeatedly query the all-hosts igmp group for igmp snooping

networking: python script to repeatedly query the all-hosts igmp group for igmp snooping


from socket import *
from struct import *
from time import *
import sys



src = '192.168.17.31'




def ichecksum(data, sum=0):
    """ Compute the Internet Checksum of the supplied data.  The checksum is
    initialized to zero.  Place the return value in the checksum field of a
    packet.  When the packet is received, check the checksum, by passing
    in the checksum field of the packet and the data.  If the result is zero,
    then the checksum has not detected an error.
    """
    # make 16 bit words out of every two adjacent 8 bit words in the packet
    # and add them up
    for i in range(0,len(data),2):
        if i + 1 >= len(data):
            sum += ord(data[i]) & 0xFF
        else:
            w = ((ord(data[i]) <> 16) > 0:
        sum = (sum & 0xFFFF) + (sum >> 16)

    # one's complement the result
    sum = ~sum

    return sum & 0xFFFF


def dump( data ):
    i = 0
    for x in data:
        if i == 4:
            print ''
            i = 0
        i += 1
        sys.stdout.write( ' %0.2x' % ord(x) )
    print ''

isrc = inet_aton( src )
idst = inet_aton( '224.0.0.1' )
router_alert = int( '1001010000000100', 2 ) << 16


ip_hdr = pack( '!BBHHHBBH4s4sI',
                (4 < to be computed
               isrc,
               idst,
               router_alert )    

crc = pack( '!H', ichecksum( ip_hdr ) )
ip_hdr = ip_hdr[:10] + crc + ip_hdr[12:]

igmp = pack( '!BBHI', 0x11, 120, 0, 0 )
crc = pack( '!H', ichecksum( igmp ) )
igmp = igmp[0:2] + crc + igmp[4:]


packet = ip_hdr + igmp
print 'packet:'
dump( packet )

s = socket( AF_INET, SOCK_RAW, 0 )
s.setsockopt( IPPROTO_IP, IP_HDRINCL, 1 )

while True:
    print s.sendto( packet, ('224.0.0.1',0) )
    sleep( 3 )
    

s.close()
Advertisements
  1. September 10, 2015 at 5:59 pm

    This script is garbage. If you’re going to post python scripts, make sure you know python syntax.

    • September 10, 2015 at 6:21 pm

      Thank you for your useful comment.

      Listen here, shit duck. Don’t be a gigantic fuck ridden douche. If you have a problem, fix that shit.

      Mind you, I used this script in production for several months. So I could say you don’t know your shit, but i’d rather attempt to infuriate you to the point of posting your own script with corrections. 

      P.s. When was this script posted? Years ago. What in the hell were you‎ searching for that even had this come up in Google and why in the fucking hell could you not just correct it?

      • November 18, 2016 at 3:48 am

        Death by comment

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: