I don't know how to use classes and i think its meant to be used here to avoid repetition of functions but i can be completely wrong, i am a student who keeps learning bit by bit. Please can you take a look into my code? i truly appreciate it :)
import nmap
import re
from sys import argv, version
#Open the file then read the content and print it
f= open("texttop.txt", "r")
file_contents= f.read()
print(file_contents)
sperate= "--------------------------------------------------------------------------------------------------------"
#shorten the function with a variable
scanner = nmap.PortScanner()
#Get the target ip address and the port range from the terminal
args = argv[1:]
if len(argv)==4:
target_ip=args[0]
port_range=args[1]
scan_type=args[2]
else:
print("Your supposed to enter the Target IP address then the port range and the attack type, enter them again.")
target_ip= str(input("Enter the target IP address:\n"))
port_range= str(input("Enter the port range please:\n "))
scan_type=str(input("Enter attack type"))
port_range.lower()
#Function for scan 1
def start_scan1():
#print the nmap version to the user
nversion = "NMAP.Version:" + str(scanner.nmap_version())
#Print nmap version without any special characters
string = re.sub(r"[() ]","",nversion)
#start scanning
print(string+"\nScanning...")
scanner.scan(target_ip,port_range,"-v -sA -O -sV")
#Save the scan info into a variable
print("This a list of the scan info. Please ignore if not wanted: " + str(scanner.scaninfo()))
#For every host in the target aquired we check its status
for host in scanner.all_hosts():
print(sperate)
#print the status of the target
print('Host : %s (%s) \t State : %s \n ' % (host, scanner[host].hostname(),scanner[host].state()))
#Check the protocol for every host
for proto in scanner[host].all_protocols():
print("Protocol: %s" % (proto))
#Make a variable to get the ports from the protocol
lport = scanner[host][proto].keys()
#Check port status
for port in lport:
print("Port: %s \t State: %s \t Service: %s \t Version: %s" % (port, scanner[host][proto][port]['state'],scanner[host][proto][port]['name'],scanner[host][proto][port]["version"]))
print("--------------------------------------------------------------------------------------------------------")
print("\n Finished scan.")
#Function for scan 2
def start_scan2():
#print the nmap version to the user
nversion = "NMAP.Version:" + str(scanner.nmap_version())
#Print nmap version without any special characters
string = re.sub(r"[() ]","",nversion)
#start scanning
print(string+"\nScanning...")
scanner.scan(target_ip,port_range,"-sS -T0")
#Save the scan info into a variable
print("This a list of the scan info. Please ignore if not wanted: " + str(scanner.scaninfo()))
#For every host in the target aquired we check its status
for host in scanner.all_hosts():
print(sperate)
#print the status of the target
print('Host : %s (%s) \t State : %s \n ' % (host, scanner[host].hostname(),scanner[host].state()))
#Check the protocol for every host
for proto in scanner[host].all_protocols():
print("Protocol: %s" % (proto))
#Make a variable to get the ports from the protocol
lport = scanner[host][proto].keys()
#Check port status
for port in lport:
print("Port: %s \t State: %s \t Service: %s \t Version: %s" % (port, scanner[host][proto][port]['state'],scanner[host][proto][port]['name'],scanner[host][proto][port]["version"]))
print("--------------------------------------------------------------------------------------------------------")
print("\n Finished scan.")
#if statement to check which scan combination the user want to use.
if scan_type==1:
start_scan1()
elif scan_type==2:
start_scan2()
else:
start_scan1()