{"id":307,"date":"2008-03-20T15:48:12","date_gmt":"2008-03-20T20:48:12","guid":{"rendered":""},"modified":"2009-04-29T22:09:04","modified_gmt":"2009-04-30T03:09:04","slug":"python-and-resolving-ips-from-a-csv-file","status":"publish","type":"post","link":"https:\/\/emresaglam.com\/blog\/blog\/2008\/03\/20\/python-and-resolving-ips-from-a-csv-file\/","title":{"rendered":"python and resolving ips from a csv file."},"content":{"rendered":"<p>Problem: I have a CSV file where I have source and destination IPs. I want to resolve only destination IPs.<\/p>\n<p>Format of my CSV file: (let`s call it test.csv) (it`s tab separated&#8230;)<\/p>\n<p><!--\nBODY,DIV,TABLE,THEAD,TBODY,TFOOT,TR,TH,TD,P { font-family:\"Arial\"; font-size:x-small }\n--><\/p>\n<table border=\"0\" cellspacing=\"0\">\n<tbody>\n<tr>\n<td align=\"left\">99.88.77.66<\/td>\n<td>11.22.33.44<\/td>\n<td align=\"right\">118340.86<\/td>\n<td align=\"right\">187<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The solution is pretty simple with python:<\/p>\n<blockquote><p>#!\/usr\/bin\/env python<br \/>\nimport csv<br \/>\nimport socket<\/p>\n<p>reader = csv.reader(open(&#8220;.\/test.csv&#8221;, &#8220;rb&#8221;), delimiter=&#8221;\t&#8220;)<br \/>\nfor row in reader:<br \/>\nhost, aliases, ips = socket.gethostbyaddr(row[1])<br \/>\nprint row[0] + &#8221;\t&#8221; + host + &#8221;\t&#8221; + row[2] + &#8221;\t&#8221; + row[3]<\/p><\/blockquote>\n<p>First we import the necessary libs. (socket and csv)<\/p>\n<p>Then we open the file to read with as a csv  object. (Careful because our delimiter is not comma, but TAB)<\/p>\n<p>for each row we get the second row (row[1]), convert it to host, alias and ip by gethostbyaddr method.<\/p>\n<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&#8230; but oh well..])<\/p>\n<p>Done!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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;) &hellip; <a href=\"https:\/\/emresaglam.com\/blog\/blog\/2008\/03\/20\/python-and-resolving-ips-from-a-csv-file\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">python and resolving ips from a csv file.<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[13,9],"_links":{"self":[{"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/posts\/307"}],"collection":[{"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":3,"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":748,"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/posts\/307\/revisions\/748"}],"wp:attachment":[{"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emresaglam.com\/blog\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}