Skip to content

Commit

Permalink
Merge pull request #23 from Kashish121/master
Browse files Browse the repository at this point in the history
IP scanning feature added.
  • Loading branch information
vinitshahdeo authored Jun 13, 2020
2 parents 96e4f82 + 2e431da commit 3020c65
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pids
*.seed
*.pid.lock
*.pyc

*.vscode
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

Expand Down
10 changes: 6 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"low": "1",
"high": "8888"
},

"ipRange": {
"low": "0",
"high": "255"
},
"count": "10",

"thread": {
"count": "32"
"thread": {
"count": "8"
}
}
80 changes: 80 additions & 0 deletions src/ipscanner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import socket
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__))
split_path = relative_path.split("/")
absolute_path = os.path.join(dir, *split_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')
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'])

except IOError:
print("config.json file not found")
except ValueError:
print("Kindly check the json file for appropriateness of range")

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


def run1(ips, range_low, range_high):
for ip in xrange(range_low, range_high):
addr = net3+str(ip)
# gets full address
if (scan(addr)):
print addr + " is live\n"


# 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 completed in ", total
2 changes: 1 addition & 1 deletion src/multi/scanner_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 3020c65

Please sign in to comment.