<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>eBlog &#187; python</title>
	<atom:link href="http://emresaglam.com/blog/tag/python/feed" rel="self" type="application/rss+xml" />
	<link>http://emresaglam.com/blog</link>
	<description>My Blog about my life and my thoughts...</description>
	<lastBuildDate>Fri, 03 Feb 2012 07:08:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>python and resolving ips from a csv file.</title>
		<link>http://emresaglam.com/blog/307</link>
		<comments>http://emresaglam.com/blog/307#comments</comments>
		<pubDate>Thu, 20 Mar 2008 20:48:12 +0000</pubDate>
		<dc:creator>Emre</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[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&#8230;) 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(&#8220;./test.csv&#8221;, &#8220;rb&#8221;), delimiter=&#8221; &#8220;) [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: I have a CSV file where I have source and destination IPs. I want to resolve only destination IPs.</p>
<p>Format of my CSV file: (let`s call it test.csv) (it`s tab separated&#8230;)</p>
<p><!--<br />
BODY,DIV,TABLE,THEAD,TBODY,TFOOT,TR,TH,TD,P { font-family:"Arial"; font-size:x-small }<br />
--></p>
<table border="0" cellspacing="0">
<tbody>
<tr>
<td align="left">99.88.77.66</td>
<td>11.22.33.44</td>
<td align="right">118340.86</td>
<td align="right">187</td>
</tr>
</tbody>
</table>
<p>The solution is pretty simple with python:</p>
<blockquote><p>#!/usr/bin/env python<br />
import csv<br />
import socket</p>
<p>reader = csv.reader(open(&#8220;./test.csv&#8221;, &#8220;rb&#8221;), delimiter=&#8221;	&#8220;)<br />
for row in reader:<br />
host, aliases, ips = socket.gethostbyaddr(row[1])<br />
print row[0] + &#8221;	&#8221; + host + &#8221;	&#8221; + row[2] + &#8221;	&#8221; + row[3]</p></blockquote>
<p>First we import the necessary libs. (socket and csv)</p>
<p>Then we open the file to read with as a csv  object. (Careful because our delimiter is not comma, but TAB)</p>
<p>for each row we get the second row (row[1]), convert it to host, alias and ip by gethostbyaddr method.</p>
<p>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..])</p>
<p>Done!</p>
]]></content:encoded>
			<wfw:commentRss>http://emresaglam.com/blog/307/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

