From d5e9ad93bf526c3db25bd087ad25f66512caf7b7 Mon Sep 17 00:00:00 2001 From: Kashish121 <56366103+Kashish121@users.noreply.github.com> Date: Sat, 13 Jun 2020 23:18:21 +0530 Subject: [PATCH 1/7] src/ipscanner.py added --- .vscode/settings.json | 3 +++ config.json | 10 +++++--- src/ipscanner.py | 58 +++++++++++++++++++++++++++++++++++++++++++ src/single/scanner.py | 2 +- 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 src/ipscanner.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..421e280 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\kashi\\.windows-build-tools\\python27\\python.exe" +} \ No newline at end of file diff --git a/config.json b/config.json index 511634d..8565052 100644 --- a/config.json +++ b/config.json @@ -3,10 +3,12 @@ "low": "1", "high": "8888" }, - + "iprange": { + "low": "0", + "high": "255" + }, "count": "10", - - "thread": { - "count": "32" + "thread": { + "count": "8" } } \ No newline at end of file diff --git a/src/ipscanner.py b/src/ipscanner.py new file mode 100644 index 0000000..7690605 --- /dev/null +++ b/src/ipscanner.py @@ -0,0 +1,58 @@ +import socket +from datetime import datetime +import json +import os + + +net1 = raw_input('Enter the IP address: ') +net2 = net1.split('.') +a = '.' +net3 = net2[0] + a + net2[1] + a + net2[2] + a + + +def get_absolute_path(relative_path): + dir = os.path.dirname(os.path.abspath(__file__)) + split_path = relative_path.split("/") + absolute_path = os.path.join(dir, *split_path) + return absolute_path + + +try: + with open(get_absolute_path('../config.json')) as config_file: + config = json.load(config_file) + print get_absolute_path('../config.json') + stn1 = int(config['iprange']['low']) + edn1 = int(config['iprange']['high']) + # defining number of threads running concurrently + CONST_NUM_THREADS = int(config['thread']['count']) + +except IOError: + print("config.json file not found") +except ValueError: + print("Kindly check the json file for appropriateness of range") + +edn1 = edn1 + 1 +td1 = datetime.now() + +def scan(addr): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + socket.setdefaulttimeout(1) + result = sock.connect_ex((addr, 135)) + if result == 0: + return 1 + else: + return 0 + + +def run1(): + for ip in xrange(stn1, edn1): + addr = net3+str(ip) + if (scan(addr)): + print addr, "this address is live" + + +run1() + +td2 = datetime.now() +total = td2-td1 +print "scanning complete in ", total diff --git a/src/single/scanner.py b/src/single/scanner.py index b135557..75d3c40 100644 --- a/src/single/scanner.py +++ b/src/single/scanner.py @@ -22,7 +22,7 @@ # scanning the port only in range of (1, 8888) try: - for port in range(1,8888): + for port in range(1,80): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex((remoteServerIP, port)) if result == 0: From 1abf8ca6403093e5be6a5f1ab4204c127db12a7b Mon Sep 17 00:00:00 2001 From: Kashish121 <56366103+Kashish121@users.noreply.github.com> Date: Sat, 13 Jun 2020 23:48:29 +0530 Subject: [PATCH 2/7] ipscanner with multithreading --- src/ipscanner.py | 39 ++++++++++++++++++++++++++----------- src/multi/scanner_thread.py | 2 +- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/ipscanner.py b/src/ipscanner.py index 7690605..3b54ca0 100644 --- a/src/ipscanner.py +++ b/src/ipscanner.py @@ -2,13 +2,22 @@ from datetime import datetime import json import os +from multi.scanner_thread import split_processing - +# Ask for input net1 = raw_input('Enter the IP address: ') net2 = net1.split('.') a = '.' net3 = net2[0] + a + net2[1] + a + net2[2] + a +# Print a nice banner with information on which host we are about to scan +print "-" * 60 +print "Please wait, scanning IP address....", net3+"XXX" +print "-" * 60 + +# Resolves the relative path to absolute path +# [BUG]: https://github.com/vinitshahdeo/PortScanner/issues/19 + def get_absolute_path(relative_path): dir = os.path.dirname(os.path.abspath(__file__)) @@ -17,12 +26,15 @@ def get_absolute_path(relative_path): return absolute_path +# Check what time the scan started +td1 = datetime.now() + try: with open(get_absolute_path('../config.json')) as config_file: config = json.load(config_file) - print get_absolute_path('../config.json') - stn1 = int(config['iprange']['low']) - edn1 = int(config['iprange']['high']) + # print get_absolute_path('../config.json') + range_low = int(config['iprange']['low']) + range_high = int(config['iprange']['high']) # defining number of threads running concurrently CONST_NUM_THREADS = int(config['thread']['count']) @@ -31,8 +43,10 @@ def get_absolute_path(relative_path): except ValueError: print("Kindly check the json file for appropriateness of range") -edn1 = edn1 + 1 -td1 = datetime.now() +ips = list(range(range_low, range_high, 1)) +# scanning the port only in range of (range_low, range_high) +range_high = range_high + 1 + def scan(addr): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -44,15 +58,18 @@ def scan(addr): return 0 -def run1(): - for ip in xrange(stn1, edn1): +def run1(ips, stn1, edn1): + for ip in xrange(range_low, range_high): addr = net3+str(ip) if (scan(addr)): - print addr, "this address is live" - + print addr + " this address is live\n" -run1() +# calling function from scanner_thread.py for multithreading +split_processing(ips, CONST_NUM_THREADS, run1, range_low, range_high) +# Checking the time again td2 = datetime.now() +# Calculates the difference of time, to see how long it took to run the script total = td2-td1 +# Printing the information to screen print "scanning complete in ", total diff --git a/src/multi/scanner_thread.py b/src/multi/scanner_thread.py index 2d836bf..549fee8 100644 --- a/src/multi/scanner_thread.py +++ b/src/multi/scanner_thread.py @@ -16,4 +16,4 @@ def split_processing(ports, num_splits, scan, range_low, range_high): # wait for all threads to finish for t in threads: - t.join() + t.join() \ No newline at end of file From d8dbf40a0f51e2e1aef6b82a8e43acc4379a22cf Mon Sep 17 00:00:00 2001 From: Kashish121 <56366103+Kashish121@users.noreply.github.com> Date: Sat, 13 Jun 2020 23:51:39 +0530 Subject: [PATCH 3/7] minor --- src/single/scanner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/single/scanner.py b/src/single/scanner.py index 75d3c40..b135557 100644 --- a/src/single/scanner.py +++ b/src/single/scanner.py @@ -22,7 +22,7 @@ # scanning the port only in range of (1, 8888) try: - for port in range(1,80): + for port in range(1,8888): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex((remoteServerIP, port)) if result == 0: From e72837be5e92dec043b64a97e8fc171fc6692dd0 Mon Sep 17 00:00:00 2001 From: Kashish121 <56366103+Kashish121@users.noreply.github.com> Date: Sun, 14 Jun 2020 00:46:49 +0530 Subject: [PATCH 4/7] added comments and minor changes --- .gitignore | 2 +- config.json | 2 +- src/ipscanner.py | 13 +++++++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 268d3e0..691877c 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,7 @@ pids *.seed *.pid.lock *.pyc - +*.vscode # Directory for instrumented libs generated by jscoverage/JSCover lib-cov diff --git a/config.json b/config.json index 8565052..e991037 100644 --- a/config.json +++ b/config.json @@ -3,7 +3,7 @@ "low": "1", "high": "8888" }, - "iprange": { + "IPRange": { "low": "0", "high": "255" }, diff --git a/src/ipscanner.py b/src/ipscanner.py index 3b54ca0..4556ae2 100644 --- a/src/ipscanner.py +++ b/src/ipscanner.py @@ -33,8 +33,8 @@ def get_absolute_path(relative_path): with open(get_absolute_path('../config.json')) as config_file: config = json.load(config_file) # print get_absolute_path('../config.json') - range_low = int(config['iprange']['low']) - range_high = int(config['iprange']['high']) + range_low = int(config['IPRange']['low']) + range_high = int(config['IPRange']['high']) # defining number of threads running concurrently CONST_NUM_THREADS = int(config['thread']['count']) @@ -46,13 +46,17 @@ def get_absolute_path(relative_path): ips = list(range(range_low, range_high, 1)) # scanning the port only in range of (range_low, range_high) range_high = range_high + 1 +# including the last address at 'range_high' def scan(addr): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket.setdefaulttimeout(1) result = sock.connect_ex((addr, 135)) + # 'result' is used as error indicator, port 135 is used for Windows + # 137, 138, 139, 445 can also be used if result == 0: + # tests if IP address is live return 1 else: return 0 @@ -61,8 +65,9 @@ def scan(addr): def run1(ips, stn1, edn1): for ip in xrange(range_low, range_high): addr = net3+str(ip) + # gets full address if (scan(addr)): - print addr + " this address is live\n" + print addr + " is live\n" # calling function from scanner_thread.py for multithreading @@ -72,4 +77,4 @@ def run1(ips, stn1, edn1): # Calculates the difference of time, to see how long it took to run the script total = td2-td1 # Printing the information to screen -print "scanning complete in ", total +print "scanning completed in ", total From 7f215fde4cde69d39d76fdf3b88bc31de1a48aed Mon Sep 17 00:00:00 2001 From: Kashish121 <56366103+Kashish121@users.noreply.github.com> Date: Sun, 14 Jun 2020 00:49:55 +0530 Subject: [PATCH 5/7] ipRange changed to CamelCase --- config.json | 2 +- src/ipscanner.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config.json b/config.json index e991037..fca20cc 100644 --- a/config.json +++ b/config.json @@ -3,7 +3,7 @@ "low": "1", "high": "8888" }, - "IPRange": { + "ipRange": { "low": "0", "high": "255" }, diff --git a/src/ipscanner.py b/src/ipscanner.py index 4556ae2..fcc1ce1 100644 --- a/src/ipscanner.py +++ b/src/ipscanner.py @@ -33,8 +33,8 @@ def get_absolute_path(relative_path): with open(get_absolute_path('../config.json')) as config_file: config = json.load(config_file) # print get_absolute_path('../config.json') - range_low = int(config['IPRange']['low']) - range_high = int(config['IPRange']['high']) + range_low = int(config['ipRange']['low']) + range_high = int(config['ipRange']['high']) # defining number of threads running concurrently CONST_NUM_THREADS = int(config['thread']['count']) From c743c679150f948c317d9c2420525fbdbac59544 Mon Sep 17 00:00:00 2001 From: Kashish121 <56366103+Kashish121@users.noreply.github.com> Date: Sun, 14 Jun 2020 01:07:04 +0530 Subject: [PATCH 6/7] issue due to multithreading function resolved --- src/ipscanner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ipscanner.py b/src/ipscanner.py index fcc1ce1..b050817 100644 --- a/src/ipscanner.py +++ b/src/ipscanner.py @@ -62,7 +62,7 @@ def scan(addr): return 0 -def run1(ips, stn1, edn1): +def run1(ips, range_low, range_high): for ip in xrange(range_low, range_high): addr = net3+str(ip) # gets full address From 2e431da035af4e3265731eaad5d2bb422156c9df Mon Sep 17 00:00:00 2001 From: Kashish121 <56366103+Kashish121@users.noreply.github.com> Date: Sun, 14 Jun 2020 01:17:35 +0530 Subject: [PATCH 7/7] .vscode/settings.json removed, s->S --- .vscode/settings.json | 3 --- src/ipscanner.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 421e280..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "python.pythonPath": "C:\\Users\\kashi\\.windows-build-tools\\python27\\python.exe" -} \ No newline at end of file diff --git a/src/ipscanner.py b/src/ipscanner.py index b050817..09775ca 100644 --- a/src/ipscanner.py +++ b/src/ipscanner.py @@ -77,4 +77,4 @@ def run1(ips, range_low, range_high): # Calculates the difference of time, to see how long it took to run the script total = td2-td1 # Printing the information to screen -print "scanning completed in ", total +print "Scanning completed in ", total