nmap and print(nm.csv()) help needed to print out to csv.file

陌路散爱 提交于 2021-01-27 19:25:40

问题


I need you help with nmap script and printing the output to csv file. When I run the script and finish it with print(nm.csv()) I got the following results displayed which is what I want first place:

host;hostname;hostname_type;protocol;port;name;state;product;extrainfo;reason;version;conf;cpe
82.214.228.176;176.228.214.82.in-addr.arpa;PTR;tcp;21;ftp;open;;;syn-ack;;3;
82.214.228.176;176.228.214.82.in-addr.arpa;PTR;tcp;22;ssh;open;;;syn-ack;;3;
82.214.228.176;176.228.214.82.in-addr.arpa;PTR;tcp;53;domain;open;;;syn-ack;;3;
82.214.228.176;176.228.214.82.in-addr.arpa;PTR;tcp;80;http;open;;;syn-ack;;3;

But my question is how to get this redirected or to create a csv file on the same Directory from where I run the script or maybe in a path. Thanks in advance!


回答1:


You could declare a function like this one

def save_csv_data(nm_csv, path='.'):
    with open(path + '/output.csv', 'w') as output:
        output.write(nm_csv)

then by passing an argument you could specify another path to save the file, or otherwise, use the same directory as the script:

if (len(sys.argv) > 1 and sys.argv[1]):
    save_csv_data(nm.csv(), path=sys.argv[1])
else:
    save_csv_data(nm.csv())

Edit: also remember to import sys

I also would suggest you, if you decide to use arguments, use a module like argparse.




回答2:


print (nm.csv(),file=open('a.csv','w'))

Edited:

You can use the print_function to get the print() behaviour from python3 in python2:

from __future__ import print_function


来源:https://stackoverflow.com/questions/45661207/nmap-and-printnm-csv-help-needed-to-print-out-to-csv-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!