Last active
January 28, 2018 21:37
-
-
Save CarpeNoctem/0a3a75ae0e23f3ad0b9b5162030f0b97 to your computer and use it in GitHub Desktop.
Much better way to divide work than the way I initially did in port_scanner.py
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
# This is probably the right way to divide work. | |
# At least I think it's better than they I had done it previously in port_scanner.py, | |
# and I'll fix port_scanner.py to use the below method. | |
# Kudos to this question and answer for the better way to get intersecting points in | |
# a range for an arbitrary number of sections: | |
# https://stackoverflow.com/questions/20925175/evenly-distribute-x-values-within-a-given-range#20925282 | |
# I played around a while with that and combined it to the way I distributed work in my Elixir port scanner to eventually | |
# arrive at the below solution. | |
def child(host, low, high): | |
for i in xrange(low, high+1): | |
print "scanning %d on %s" % (i, host) | |
def scan(host, min, max, num_threads): | |
if num_threads > max - min + 1:s | |
return "Thread count must not be greater than number of ports to scan." | |
high = min - 1 | |
for i in xrange(num_threads): | |
low = high + 1 | |
high = min + (i+1)*(max-min) / num_threads | |
print "Thread #%d should scan from %d to %d" % (i+1, low, high) | |
child(host, low, high) | |
# scan("127.0.0.1", 3, 117, 21) |
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
# For comparison, I found the work distribution algorithm I arrived at in functional programming to be much simpler | |
# When I did the imperative version many years ago, it was buggy, and I had to create extra conditions for edge cases. | |
# When I started working on a functional implementation, I arrived quickly and simply at a solution that had no bugs or | |
# missing edge cases, and I hadn't had to search the internet for how to do such a task in functional programming - I was | |
# more or less guided down that path by the paradigm itself. | |
# Afterward, I finally got around to searching the internet for the proper solution in the imperitive way and put together | |
# the above implementation (see above comments). | |
defmodule WorkDemo do | |
defp child(host, bottom_port..top_port, open_ports) when bottom_port <= top_port do | |
IO.puts "Scanning #{bottom_port} on #{host}" | |
# If port is open, add to _open_ports accumulator | |
child(host, (bottom_port + 1)..top_port, open_ports) | |
end | |
defp child(_, _, _open_ports) do | |
#IO.puts "Open ports:" | |
#IO.inspect open_ports | |
end | |
def scan(host, bot..top, num_processes) when bot <= top do | |
mytop = bot + (Enum.count(bot..top) / num_processes |> round) - 1 | |
IO.puts "Thread ##{num_processes} should scan #{bot} through #{mytop}" | |
child(host, bot..mytop, []) | |
scan(host, (mytop + 1)..top, num_processes - 1) | |
end | |
def scan(_, _, 0) do | |
IO.puts "Scan complete." | |
end | |
end | |
# WorkDemo.scan("127.0.0.1", 3..117, 21) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment