pythonpython-2.7socketscsvcsvkit

Using CSV kit to add a new column to a CSV and to populate it with results from socket.gethostname()


I am trying to write a python script to parse a csv file (results.csv) that takes the hostnames listed within it looks up the IP adresses for eac hostname and appends this data to the csv file in a new column.

I have done some searching and have hit a bit of a wall. Also I must stress that I am very new to Python and this is something I am doing to increase my knowledge.

I am using python 2.7.x on OS X and my code is as follows:

#!/usr/bin/python

import socket
import csvkit

with open('results.csv', 'rb') as infile:
    with open('output.csv', 'wb') as outfile:
        writer = csvkit.writer(outfile)
        for row in csvkit.reader(infile):
            IP = socket.gethostname(row)
            writer.writerow(row+[IP])

When I run this in PyCharm I get the following error:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/xxxxxx/PycharmProjects/typosquating/IP_lookup.py Traceback (most recent call last): File "/Users/xxxxxx/PycharmProjects/typosquating/IP_lookup.py", line 10, in IP = socket.gethostname(row) TypeError: gethostname() takes no arguments (1 given)

Process finished with exit code 1

Can anyone shed some light on what I am doing wring? I can use socket.gethostname() on another script in the same way above to go over the contents of the results.csv file and output the relevant IPs of each hostname on the screen, but it seems to failing in this usage.

Thanks for your help.


Solution

  • socket.gethostname() takes no arguments What you are looking for is socket.gethostbyname(hostname)

    You might want to RTFM