Last active
February 11, 2020 10:36
-
-
Save tesh254/0402923ca8325fa3d5091bde190e95eb to your computer and use it in GitHub Desktop.
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
#!/bin/python3 | |
import sys # Allows us to enter command line arguments | |
import socket | |
from datetime import datetime | |
# Define our target | |
if len(sys.argv) == 2: | |
target = socket.gethostbyname(sys.argv[1]) # Transalate a host name to ipv4 | |
else: | |
print("Invalid amount of arguments") | |
print("Syntax: python3 scanner.py <ip>") | |
sys.exit() | |
# Add a pretty banner | |
print("-" * 50) | |
print("Scanning target "+target) | |
print("Time started "+str(datetime.now())) | |
print("-"*50) | |
try: | |
for port in range(50, 81): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
socket.setdefaulttimeout(1) # is a float | |
result = s.connect_ex((target, port))# returns error indicator | |
print("Checking port {}".format(port)) | |
if result == 0: | |
print("Port: {} is open".format(port)) | |
s.close() | |
except KeyboardInterrupt: | |
print("\nExiting program.") | |
sys.exit() | |
except socket.gaierror: | |
print("Hostname could not be resolved") | |
sys.exit() | |
except socket.error: | |
print("Couldn't connect to server") | |
sys.exit() |
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
import subprocess | |
import datetime | |
import re | |
import argparse | |
# not re.match(r"(\{1,3}\.\d{1,3}\.\d{1,3})", args.subnet) or | |
def write_result(filename, ping): | |
with open(filename, "w") as f: | |
f.write(f"Start time {datetime.datetime.now()}") | |
for result in ping: | |
f.write(result) | |
f.write(f"End time {datetime.datetime.now()}") | |
def ping_subnet(subnet): | |
for addr in range(1, 255): | |
yield subprocess.Popen([ "ping", f"{subnet}.{addr}", "-n", "1"], stdout=subprocess.PIPE).stdout.read().decode() | |
def main(subnet, filename): | |
write_result(filename, ping_subnet(subnet)) | |
def parse_arguments(): | |
parser = argparse.ArgumentParser(usage='%(prog)s [options] <subnet>', | |
description="ip checker", | |
epilog="python sweep.py xxx.xxx.xxx.xxx -f somefile.txt") | |
parser.add_argument('subnet', type=str, help="the subnet you want to ping") | |
parser.add_argument('-f', '--filename', type=str, help="The filename") | |
args = parser.parse_args() | |
print(args.subnet) | |
if any(a not in range(1, 255) for a in map(int, args.subnet.split("."))): | |
parser.error("This is not a valid subnet") | |
if " " in args.filename: | |
parse.error("There cannot be whitespaces in the filename") | |
print(args.subnet) | |
return args.subnet, args.filename | |
if __name__ == "__main__": | |
main(*parse_arguments()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment