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

时间:2017-08-13 14:06:09

标签: python csv nmap

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!

2 个答案:

答案 0 :(得分:1)

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

<强>被修改

您可以使用print_function从python2中的python3获取print()行为:

from __future__ import print_function

答案 1 :(得分:1)

你可以声明一个像这样的函数

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

然后通过传递一个参数,你可以指定另一个保存文件的路径,否则,使用与脚本相同的目录:

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

修改还记得import sys

我也建议你,如果你决定使用参数,请使用像argparse这样的模块。