2

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!

Martin Alonso
  • 726
  • 2
  • 9
  • 16
Ivan Madolev
  • 119
  • 1
  • 12

2 Answers2

1
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
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
  • Hey Smart, i am getting syntax error: File "test2.py", line 22 print (nm.csv(),file=open('a.csv','w')) ^ SyntaxError: invalid syntax – Ivan Madolev Aug 13 '17 at 16:43
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.

Martin Alonso
  • 726
  • 2
  • 9
  • 16