-1

I'm trying to iterate through an imported .csv file and assign each value within a column to a variable. The idea being to use that variable to conduct a search of the Shodan API, print the search results to the screen and then move on to the value in the next row in the column, assign it to the variable, do the search, and so on.

Here's what I've cobbled together from what I've found so far....

import csv

# Initialize the API
from shodan import WebAPI
api = WebAPI("My Shodan Key")

# Open csv file

with open('C:\pythonfiles\sccm.csv', 'rb') as reader:
    sccmtable = csv.reader(reader, delimiter=';')
    #for row in sccmtable:
    #print ', '.join(row)

for row in sccmtable:
    for value in row:
        edbresults = api.exploitdb.search(value)
        print (edbresults)

It seems as if this is the correct start, as I can print the content of the newly imported csv to the screen, but I'm not sure how to take the next step. Any help is greatly appreciated.

Best regards.

  • Are you getting an error? what is it? Do you want to search each value in each row, or for each row search all values combined? `row` is a list of values, I'm just guessing `api.exploitdb.search` expects a string, not a list of strings. – shx2 Mar 12 '13 at 06:17
  • @shx2 Thanks for your response. You're correct that `api.exploitdb.search` expects a string and I wasn't aware that `row` is a list of values. I would like to search each value in each row individually. Thanks for any help. – user2145207 Mar 12 '13 at 13:47

2 Answers2

1

In order to search each value in each row individually, do:

for row in reader:
    for value in row:
        edbresults = api.exploitdb.search(value)
        print (edbresults)
shx2
  • 61,779
  • 13
  • 130
  • 153
  • Thanks again! I plugged in what you provided and i'm getting the following error: ``ValueError: I/O operation on closed file`` My brain is kind of mush from a long day at work right now, so i can do more research tomorrow on my own as to what would cause this. Any ideas? – user2145207 Mar 12 '13 at 20:18
  • I found this after some Google-fu: "It is important to be conscious of the file-like behavior here. Note that a seek is required after writing to get back to the beginning and read. Attempting to run the same iterator twice results in no values the second time through. ```StringIO.getvalue()``` returns a newly created string object with the full contents of the StringIO buffer." Do you think this approach would help? – user2145207 Mar 12 '13 at 20:25
  • Please fix the indentation in your original question, and include a full traceback of the error you're getting. That would make it easier to help you find a solution. – shx2 Mar 12 '13 at 20:35
  • Very sorry...here you go. Traceback (most recent call last): File "C:\Users\wilsond\workspace\Desktop Patching Lookup\ModuleOne.py", line 34, in for row in sccmtable: ValueError: I/O operation on closed file – user2145207 Mar 12 '13 at 21:09
  • please update your original question with the new code you're running and the traceback, otherwise I can't help.. – shx2 Mar 12 '13 at 21:12
0

Regarding your second question, when your with statement goes out of scope, your file gets closed. You need to read its contents while still in scope. Simply indent-in.

with open('C:\pythonfiles\sccm.csv', 'rb') as reader:
    sccmtable = csv.reader(reader, delimiter=';')
    #for row in sccmtable:
    #print ', '.join(row)

    for row in sccmtable:
        for value in row:
            edbresults = api.exploitdb.search(value)
            print (edbresults)
shx2
  • 61,779
  • 13
  • 130
  • 153
  • Thanks! It seems to be working now. My CSV has 375 rows each containing a name of an application. When I run the script, the first four return no results. The fifth is a good hit and returns good search results then next two have no results. Then something blows up. – user2145207 Mar 12 '13 at 21:26
  • Traceback (most recent call last): File "C:\Users\wilsond\workspace\Desktop Patching Lookup\ModuleOne.py", line 22, in edbresults = api.exploitdb.search(value) File "build\bdist.win32\egg\shodan\api.py", line 105, in search File "build\bdist.win32\egg\shodan\api.py", line 159, in _request File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 406, in open response = meth(req, response) – user2145207 Mar 12 '13 at 21:27
  • File "C:\Python27\lib\urllib2.py", line 519, in http_response 'http', request, response, code, msg, hdrs) File "C:\Python27\lib\urllib2.py", line 444, in error return self._call_chain(*args) File "C:\Python27\lib\urllib2.py", line 378, in _call_chain result = func(*args) File "C:\Python27\lib\urllib2.py", line 527, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 503: Service Temporarily Unavailable – user2145207 Mar 12 '13 at 21:28