1

I want to print the output of the NMAP scan to the webpage, however it does'nt display. I believe the webpage is not waiting till the bash command has finished before printing the output.

How can I resolve this so that the output can be viewed on the webpage? Thanks.

The print statement: " print("%s - %s" % (i + 1, ip)) "

The program runs fine without the web interface in the terminal window as shown: Terminal output

However, the web interface does not display the menu: Web page output

index.py:

#!/usr/bin/python
import subprocess
# Import modules for CGI handling 
import cgi, cgitb 
import time
# Create instance of FieldStorage 
form = cgi.FieldStorage()

# Get data from fields


print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"

print "</head>"
print "<body>"
print "</body>"
print "</html>"


def bash(command):
    return subprocess.check_output(['bash', '-c', command])


def nmap_scan(ip):
    print(("<H1>Scanning TCP ports on %s</H1>" % ip))
    res = bash('nmap -T4 -p1-65535 %s | grep "open"' % ip).splitlines()
    ports = []

    for port in res:
        print(port)
        ports.append(port.split("/")[0])

    port_list = ",".join(ports)
    print("<H1>\nRunning intense scan on open ports...\n</H1>")
    bash('nmap -T4 -A -sV -p%s -oN output.txt %s' % (port_list, ip))
    print("</H1>Nmap intense scan  results logged in 'output.txt'</H1>")
    exit()

ip_string = bash('ifconfig eth0 | grep "inet "')

ip = ip_string.decode("utf-8").strip().split(" ")[1]

print(("<H1>Your IP Address is: </H1>" + ip + "\n"))

octets = ".".join(ip.split(".")[:-1])
subnet = octets + ".0/24"
print(("<H1>Running netdiscover on local subnet: %s </H1>" % subnet))

ips = bash('netdiscover -P -r %s | grep "1" | cut -d " " -f2 ' % subnet).splitlines()
for i in range(0, len(ips)):
    ip = ips[i]
    print("<H1>%s - %s</H1>" % (i + 1, ip))

choice = input("<H1>\nEnter an option 1 - %s, or 0 to exit the script:\n</H1>" % len(ips))
nmap_scan(ips[choice - 1])

cgiPostMethod.html:

<html>
<head>
<title> Post method example </title>
</head>
<body>


<form action = "/cgi-enabled/index.py" method = "post">
        <p>Automated Host Discovery and NMAP scan:</p>
        <input type="submit" name="submit_button" value="NMAP Scan">
</form>

</body>
</html>

0 Answers0