Thursday, February 10, 2011

nslookup and dig in excel for mac

So, I cant program. I can babble perl, understand anything not c++ or ruby, and copy and paste. Oh, yes.

But need is the master of invention, and i had the need of report in an excel spreadsheet the IP address to which some domains and nodes would resolve, or not.

Easy, right?


common sense and time presure is saying to make cell B something like =nslookup(a1)
go scrolling down, and let the resolver do the job.
No such function or macro.

Looking in google show some visual basic crap, that requires windoze OLE objects (winsh, etc), but the, Applescript came to the rescue from some website. Changing the function for a shell objetc (dig +short xxx) made the trick, all the cells were nicely resolved down, in seconds.

Here is the Applescript code snippet:

on run

tell application "Microsoft Excel"

tell sheet "Sheet1" of workbook 1

set EndofRow to 333 -- This defines the last Excel Row with data; it this example, it is A333.

set input_data to ""

repeat with i from 1 to EndofRow -- This loop will run from row 1 until EndofRow set above. For this example, it was set to 333.

set InputCell to the value of (cell i of range ("A1:A" & i)) -- This line defines the range, where in this example it is set A1:A333 and i is the counter and will continue to loop until EndofRow.

set input_data to InputCell

set encoded_data to do shell script "dig +short " & input_data -- This is the resolver

set EncodedCell to ("B" & i) -- This is where to designate where the encoded data is to appear. In this example, the encoded data will be in Column "B".

set value of cell EncodedCell to encoded_data

end repeat

end tell

end tell

end run




And part of the final result (column c is "dig +short www.$domain):


Awesome!

0 Comments:

Post a Comment

<< Home