Enumeration Program
<p>I'm in the process of creating a program that takes an IP address, performs an nmap scan, and takes the output and puts it in a text file. The scan works fine, but I can't seem to figure out why it's not writing anything to the text file.</p>

<p>Here is what I have so far</p>

<pre><code>if __name__ == "__main__":

    import socket
    import nmap
    import sys
    import io
    from libnmap.parser import NmapParser, NmapParserException
    from libnmap.process import NmapProcess
    from time import sleep
    from os import path

    #Program Banner
    if len(sys.argv) &lt;= 1:
        print(
"""
test
""")
        sys.exit()


#Grab IP Address as argument
if len(sys.argv)==2:
    ip = sys.argv[1]
    print "\n[+] Reading IP Address"


#Function - Pass IP to Nmap then start scanning
print "\n[+] Passing " + ip + " to Nmap..."
print("\n[+] Starting Nmap Scan\n")

def nmap_scan(ip, options):
    parsed = None
    nmproc = NmapProcess(ip, options)
    rc = nmproc.run()
    if rc != 0:
        print("nmap scan failed: {0}".format(nmproc.stderr))

    try:
        parsed = NmapParser.parse(nmproc.stdout)
    except NmapParserException as e:
        print("Exception raised while parsing scan: {0}".format(e.msg))

    return parsed


#Function - Display Nmap scan results
def show_scan(nmap_report):
    for host in nmap_report.hosts:
        if len(host.hostnames):
            tmp_host = host.hostnames.pop()
        else:
            tmp_host = host.address

        print("Host is [ %s ]\n" % str.upper(host.status))
        print("  PORT     STATE         SERVICE")

        for serv in host.services:
            pserv = "{0:&gt;5s}/{1:3s}  {2:12s}  {3}".format(
                    str(serv.port),
                    serv.protocol,
                    serv.state,
                    serv.service)
            if len(serv.banner):
                pserv += " ({0})".format(serv.banner)
            print(pserv)


#Function - Define output text file name &amp; write to file
def createFile(dest):
    name = "Enumerator-Results.txt"

    if not(path.isfile(dest+name)):
        f = open(dest+name,"a+")
        f.write(show_scan(report))
        f.close()    



if __name__ == "__main__":
    report = nmap_scan(ip, "-sV")
    if report:
        destination = "/root/Desktop/"
        createFile(destination)
        show_scan(report)
        print "\nReport Complete!"
    else:
        print("No results returned")
</code></pre>

<p>You're using print statements in your show_scan() function. Instead try passing the file reference to show_scan() and replacing the print() calls with f.write() calls. This would save to file everything you're currently printing to the terminal.</p>

<p>Alternatively you could just change your code so that the show_scan is separate from the f.write().</p>

<p>ie change</p>

<pre><code>f.write(show_scan(report))
</code></pre>

<p>to</p>

<pre><code>f.write(report)
</code></pre>

<p>It depends on whether you want to save the raw output or what you're printing to the screen.</p>

<p>Also you will need to pass the reference of the report to createFile so that it has the report to print ie</p>

<pre><code>createFile(destination, report)
</code></pre>

<p>Just make sure you are always calling f.write() with a string as its parameter.</p>

<pre><code>#Function - Define output text file name &amp; write to file
def createFile(dest, report):
    name = "Enumerator-Results.txt"

    if not(path.isfile(dest+name)):
        f = open(dest+name,"a+")
        f.write(report)
        f.close()    



if __name__ == "__main__":
    report = nmap_scan(ip, "-sV")
    if report:
        destination = "/root/Desktop/"
        createFile(destination, report)
        show_scan(report)
        print "\nReport Complete!"
    else:
        print("No results returned")
</code></pre>

python text-files nmap