#!/usr/bin/env python3.7 # Controls the Christmas lights here: http://ipv6tree.bitnet.be/ # in Jan. 2019 it was here: https://jinglepings.com/ # Great presentation: https://prezi.com/t_7tf_xwbdf7/ # Requires sudo to ping, so do something like `sudo ./ipv6_fun.py` to run # Works on Windows without anything like sudo by doing something like `python ipv6_fun.py` # I used fayyazkl's answer in the following as a guide for how to ping... # https://stackoverflow.com/questions/3973142/python-raw-ipv6-socket-errors # Because the entire goal of my code is to ping flood as quickly as possible, # I set pausee to be very small and put in a try block. If you just make pausee # small without the try block, you get a "No buffer space available" error. # Also, if you make pausee too small with a too slow Internet connection, you're # just wasting CPU power without actually sending more pings. I usually prefer to # waste CPU power by setting pausee to 0 because I don't like to use the sleep # function because OSs have limits on the smallest time that something can sleep # (and this limit differs for different OSs), so I usually prefer to not worry # about using any sleep at all. # You may wish to use a nonzero (though still small) pausee value to not # interfere with other devices that are connecting to the Internet via the same # router. # Keep in mind that this code will keep sending without first establishing a # connection and without ever needed to hear any response from destination. # Even if this code and your computer's network monitor are telling you that # lots of data is successfully being sent, this only means that your router is # receiving the data. # If you disconnect your router from the Internet, your computer and this code # will keep telling you that this data is being successfully sent. # Most ISPs put strong limits on the data that you can upload to the Internet # compared to their download limits (to prevent you from doing things like ping # flooding!). # According to speedtest.net, Comcast gives me 6 Mbps uploads # (a bit more than the 5 Mbps I am guaranteed by Comcast), so I doubt that # anything more than 12,500 pings per second are actually getting through # Comcast's network. # Even if I connect my computer directly to my modem, # my computer and this code still show me that I am sending lots of pings, # so maybe all the pings ARE getting through!??? # Anyway, I think Comcast eventually blocked IPv6 from my router after # running this code for several hours??? I can get an IPv6 connection when # my computer directly connects to the modem, but I can no longer get a # connection through my router! # The following was true of LEDs for Christmas 2017: # Note that the lights are "smart". For inputs 0 to 255, I can visibly see 2 # rather than first requiring a large enough value to see light. # Finally, equal inputs for rgb always gives white. # So, these LEDs are very smart (probably due to PWM). # Also note that sending color 0:0:0 has no effect, which is OK because 1:1:1 # is how black is actually done. Perhaps this was an intentional choice # to prevent us from easily blacking out his tree! Note that black is also # obtained by 1:0:0, 0:1:0, 0:0:1, 0:1:1, 1:0:1, and 1:1:0. import socket import time # sleep time between pings in seconds pausee = 0 #pausee = 0.00001 num = 100000 # number of cycles to perform timee = 3 # time for each cycle in seconds ipBase = '2001:6a8:2880:2018::' ## the following hex color strings may be useful later when building ipList red = 'FF:00:00' yellow = 'FF:FF:00' green = '00:FF:00' cyan = '00:FF:FF' blue = '00:00:FF' purple = 'FF:00:FF' white = 'FF:FF:FF' black = '01:01:01' # note that 1 is off, and doing 0:0:0 is actually ignored by the tree! grey1 = '80:80:80' # value is at 256/2 = 128 = 2^7 grey2 = '40:40:40' # value is at 256/4 = 64 = 2^6 grey3 = '20:20:20' # value is at 256/8 = 32 = 2^5 ## create some lists that may be useful later when building ipList n = 300 # arbitrary length as long as it is large enough and not extremely large # outputs a hex string after ignoring decimal part of float def floatToHex(float): return hex(int(float)).split('x')[-1] # exponential for alternating between 2 colors hex1a = [floatToHex( 2**(i*8.0/(n-1)) - 1 ) for i in range(n)] hex1b = [floatToHex( 2**((n-1-i)*8.0/(n-1)) - 1 ) for i in range(n)] # linear for fading between 2 colors hex2a = [floatToHex( i*255.0/(n-1) ) for i in range(n)] hex2b = [floatToHex( (n-1-i)*255.0/(n-1) ) for i in range(n)] # exponential for pulsing a color width = 7.1 # value between 0 and 8 hex3a = [floatToHex( 2**((i+0.99)*width/n+8-width) ) for i in range(n)] hex3b = hex3a ## some ideas for creating ipList (switching between colors) ipList = [white,grey1,grey2,grey3] # good for testing brightness response ipList = [white,black] ipList = [red, yellow, green, cyan, blue, purple] ## another idea for creating ipList (for pulsing and fading) ipList = ['' for i in range(2*n)] for i in range(n): ipList[i] = hex3b[i] + ':' + '0' + ':' + hex3a[i] ipList[2*n-1-i] = hex3b[i] + ':' + '0' + ':' + hex3a[i] ## finally, let's do it! send_socket = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.getprotobyname('ipv6-icmp')) ipList = tuple(ipList) # for speed length = len(ipList) for i in range(num): print('cycle '+str(i+1)+' of '+str(num)) start = time.time() k=0 # count of failed pings l=0 # count of all attempts for j in range(length): while (time.time() - start) < (timee*(j+1.0)/length): l += 1 try: send_socket.sendto(b'\x80\0\0\0\0\0\0\0', (ipBase+ipList[j], 0, 0, 0)) except: k += 1 time.sleep(pausee) print('successful pings per second = '+str(int(round((l-k)*1.0/timee)))) print('failed pings per second = '+str(int(round(k*1.0/timee))))