python and resolving ips from a csv file. 03.20.2008 15:48
Printer Friendly

Problem: I have a CSV file where I have source and destination IPs. I want to resolve only destination IPs.

Format of my CSV file: (let's call it test.csv) (it's tab separated...)

99.88.77.66
    11.22.33.44
    118340.86     187

The solution is pretty simple with python:

#!/usr/bin/env python
import csv
import socket

reader = csv.reader(open("./test.csv", "rb"), delimiter="\t")
    for row in reader:
    host, aliases, ips = socket.gethostbyaddr(row[1])
    print row[0] + "\t" + host + "\t" + row[2] + "\t" + row[3]

First we import the necessary libs. (socket and csv)

Then we open the file to read with as a csv object. (Careful because our delimiter is not comma, but TAB)

for each row we get the second row (row[1]), convert it to host, alias and ip by gethostbyaddr method.

The last line is to create the new tab separated format. (Just pipe it to a file and you have your new CSV file. [tab separated... but oh well..])

Done!

Back to Eblog

Latest two blog entries:
Weird Java Time problem
Lotus Notes Sametime with Pidgin (or gaim)


Make a comment!
Comments will be approved first to prevent suckers to misuse/abuse my comments section. So, there is a delay!
No html tags are allowed. Text is your friend ;)
I will never use your email for bad purposes, so, chill.. ;)

Date: 05.16.2008 22:33
Name:
Email:
URL:
Subject:
Comment:
Please type the number you see in this picture to the box below.