Last active
August 29, 2015 14:25
-
-
Save mpurdon/3c6936d50406b782774e to your computer and use it in GitHub Desktop.
Threaded port scanner for finding chromecasts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Does a quick scan of the current subnet for the specified IP range and ports | |
- Used to find a listening chromecast | |
""" | |
import threading | |
import time | |
from Queue import Queue | |
from socket import * | |
CURRENT_IP = gethostbyname(gethostname()) | |
IP_BLOCK = CURRENT_IP[:CURRENT_IP.rfind('.')] | |
START_IP = 1 | |
END_IP = 255 | |
TARGET_PORTS = [8008, ]# 80] | |
DEBUGGING_PORTS = [9222,] | |
ALL_PORTS = TARGET_PORTS + DEBUGGING_PORTS | |
MAX_THREADS = END_IP - START_IP # One thread per IP | |
print_lock = threading.Lock() | |
target_ips = Queue() | |
chromecasts = [] | |
debuggable_chromecasts = [] | |
def worker(target_ip): | |
""" | |
Do the work the thread is trying to accomplish | |
""" | |
# Normal Ports | |
for target_port in TARGET_PORTS: | |
s = socket(AF_INET, SOCK_STREAM) | |
result = s.connect_ex((target_ip, target_port)) | |
if(result == 0) : | |
chromecasts.append(target_ip); | |
s.close() | |
# Debugging ports | |
for target_port in DEBUGGING_PORTS: | |
s = socket(AF_INET, SOCK_STREAM) | |
result = s.connect_ex((target_ip, target_port)) | |
if(result == 0) : | |
debuggable_chromecasts.append(target_ip); | |
s.close() | |
def threader(): | |
""" | |
Process the target ip queue using all available threads | |
""" | |
while True: | |
target_ip = target_ips.get() | |
worker(target_ip) | |
target_ips.task_done() | |
if __name__ == '__main__': | |
print 'Starting scan of {}.{}-{} on ports {}...'.format(IP_BLOCK, START_IP, END_IP, ','.join(str(port) for port in ALL_PORTS)) | |
# Set the default socket timeout. | |
setdefaulttimeout(0.1) | |
# Create our worker threads | |
for thread_number in xrange(MAX_THREADS): | |
thread = threading.Thread(target=threader) | |
thread.daemon = True | |
thread.start() | |
start = time.time() | |
# Build task queue | |
for ip in range(START_IP, END_IP): | |
target_ip = '{block}.{ip}'.format(block=IP_BLOCK, ip=ip) | |
target_ips.put(target_ip) | |
# Wait until threading stops | |
target_ips.join() | |
if len(chromecasts) > 0: | |
print '\nFound running chromecasts at:' | |
for chromecast in chromecasts: | |
print chromecast | |
else: | |
print '\nNo running chromecasts could be found.' | |
if len(debuggable_chromecasts) > 0: | |
print '\nThese chromecasts can be debugged:' | |
for chromecast in debuggable_chromecasts: | |
print chromecast | |
else: | |
print '\nNo running chromecasts can be debugged.' | |
print('\nScanning took: {:.2f}s'.format(time.time() - start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks for open debugging ports on the current LAN segment so you can figure out the IP of your chromecast.