Skip to content

Instantly share code, notes, and snippets.

@cxgreat2014
Forked from comzyh/breed_enter_linux.py
Created May 2, 2022 12:14
Show Gist options
  • Save cxgreat2014/085e98ce38baf93fee8d7c2076a3faf7 to your computer and use it in GitHub Desktop.
Save cxgreat2014/085e98ce38baf93fee8d7c2076a3faf7 to your computer and use it in GitHub Desktop.

Revisions

  1. @comzyh comzyh revised this gist Nov 25, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions breed_enter_linux.py
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ def send_thread_func(s, nic=None):
    s.sendto(b'BREED:ABORT', ('255.255.255.255', 37541))
    except Exception:
    print("Stop sending on {}".format(nic))
    break



  2. @comzyh comzyh revised this gist Nov 25, 2019. 2 changed files with 69 additions and 0 deletions.
    69 changes: 69 additions & 0 deletions breed_enter_linux.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #!/usr/bin/env python3
    import socket
    import time
    import threading
    import signal
    import sys
    import os
    sending = None


    def send_thread_func(s, nic=None):
    global sending
    while sending:
    time.sleep(0.1)
    try:
    s.sendto(b'BREED:ABORT', ('255.255.255.255', 37541))
    except Exception:
    print("Stop sending on {}".format(nic))



    def receive_thread_func(s, nic=None):
    global sending
    while sending:
    data, addr = s.recvfrom(2048)
    if data == b'BREED:ABORTED':
    print("BREED now running on {}\t{}".format(nic, addr[0]))
    print("http://{}".format(addr[0]))
    break
    sending = False


    def start_breed_enter():
    global sending
    nics = os.listdir('/sys/class/net/')
    for nic in nics:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    s.setsockopt(socket.SOL_SOCKET, 25, nic.encode(
    'utf-8')) # SO_BINDTODEVICE
    s.bind(('0.0.0.0', 37540))

    sending = True
    receive_thread = threading.Thread(target=receive_thread_func, args=(s, nic))
    send_thread = threading.Thread(target=send_thread_func, args=(s, nic))

    receive_thread.daemon = True
    send_thread.daemon = True
    receive_thread.start()
    send_thread.start()
    print('start listening on {}'.format(nic))

    print("Waiting breed to be discovered....")
    while sending:
    try:
    time.sleep(0.5)
    except KeyboardInterrupt:
    break

    print("QUIT")
    sys.exit(0)


    def main():
    start_breed_enter()


    if __name__ == '__main__':
    main()
    File renamed without changes.
  3. @comzyh comzyh revised this gist Nov 25, 2019. No changes.
  4. @comzyh comzyh created this gist Nov 25, 2019.
    59 changes: 59 additions & 0 deletions breed_enter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #!/usr/bin/env python3
    import socket
    import time
    import threading
    import signal
    import sys
    sending = None


    def send_thread_func(s):
    global sending
    while sending:
    time.sleep(0.1)
    s.sendto(b'BREED:ABORT', ('255.255.255.255', 37541))


    def receive_thread_func(s):
    global sending
    while sending:
    data, addr = s.recvfrom(2048)
    if data == b'BREED:ABORTED':
    print("BREED now running on {}".format(addr[0]))
    print("http://{}".format(addr[0]))
    break
    sending = False


    def start_breed_enter():
    global sending
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    s.bind(('0.0.0.0', 37540))

    sending = True
    receive_thread = threading.Thread(target=receive_thread_func, args=(s, ))
    send_thread = threading.Thread(target=send_thread_func, args=(s, ))

    receive_thread.daemon = True
    send_thread.daemon = True
    receive_thread.start()
    send_thread.start()

    print("Waiting breed to be discovered....")
    while sending:
    try:
    time.sleep(0.5)
    except KeyboardInterrupt:
    break

    print("QUIT")
    sys.exit(0)


    def main():
    start_breed_enter()


    if __name__ == '__main__':
    main()