Skip to content

Instantly share code, notes, and snippets.

@Lucretiel
Last active August 29, 2015 14:20
Show Gist options
  • Save Lucretiel/ab37d496e5bc07939778 to your computer and use it in GitHub Desktop.
Save Lucretiel/ab37d496e5bc07939778 to your computer and use it in GitHub Desktop.
import asyncio
@asyncio.coroutine
def scan_remote(host, port, timeout=1):
try:
connection = asyncio.open_connection(host, port)
yield from asyncio.wait_for(connection, timeout)
except (OSError, asyncio.TimeoutError):
pass
else:
print("Connected to {}:{}".format(host, port))
@asyncio.coroutine
def scan_hosts(hosts, port):
# Scan a given port against a list of hosts
yield from asyncio.gather(
*[scan_remote(host, port) for host in hosts])
@asyncio.coroutine
def scan_ports(host, ports=range(1, 1024)):
# Scan all the ports of a given host
yield from asyncio.gather(
*[scan_remote(host, port) for port in ports])
# Example: scan for port 22s in 192.168.0.X
hosts = map('192.168.0.{}'.format, range(256))
asyncio.get_event_loop.run_until_complete(scan_hosts(hosts, 22))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment