计数程序

时间:2016-10-15 23:30:38

标签: python text-files nmap

我正在创建一个获取IP地址,执行nmap扫描并获取输出并将其放入文本文件的程序。扫描工作正常,但我似乎无法弄清楚为什么它没有在文本文件中写任何东西。

这是我到目前为止所拥有的

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) <= 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:>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 & 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")

1 个答案:

答案 0 :(得分:0)

您在show_scan()函数中使用print语句。而是尝试将文件引用传递给show_scan()并用f.write()调用替换print()调用。这样可以保存您当前正在打印到终端的所有内容。

或者您可以更改代码,以便show_scan与f.write()分开。

即改变

f.write(show_scan(report))

f.write(report)

这取决于您是要保存原始输出还是要将其打印到屏幕上。

此外,您需要将报告的引用传递给createFile,以便它具有要打印的报告,即

createFile(destination, report)

请确保始终使用字符串作为参数调用f.write()。

#Function - Define output text file name & 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")