Home > Hints, Smalltalk, VisualWorks > Bulk resolve IP addresses in Smalltalk

Bulk resolve IP addresses in Smalltalk

November 15, 2010 Leave a comment Go to comments

I’m sure bash and PowerShell geeks out there might have a better way, but since my day job is all about Smalltalk, the following seems very appropriate when it comes to translating a ton of IP addresses into domain names in a file for further review,

cache := Dictionary new.
rs := ('fwrules.20101110.raw.txt' asFilename withEncoding: #utf8)
		readStream lineEndTransparent.
ws := ('fwrules.20101110.resolved.txt' asFilename withEncoding: #utf8)
		writeStream lineEndTransparent.
['(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
  , '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
    , '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
      , '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' asRegex
        copyStream: rs
        to: ws
        translatingMatchesUsing:
                [:str |
                cache
                        at: str
        		ifAbsentPut:
                                [| ip |
                                ip := IPSocketAddress stringToBytes: str.
                                IPSocketAddress hostNameByAddress: ip]]]
        ensure:
                [rs close.
                ws close].
Categories: Hints, Smalltalk, VisualWorks
  1. November 15, 2010 at 4:49 pm | #1

    You don’t need to validate the IP addresses (and doing it with a regex is a bad idea anyway… using a screwdriver as a hammer). So simplify your life and code with:

    ‘(\d+)\.\(d+)\.(\d+)\.(\d+)’ asRegex

    Your fingers and maintenance programmers will thank you.

  2. January 16, 2011 at 7:50 am | #2

    I don’t know, I prefer more code that’s more readable than regex voodoo with a ton of useless nerd joy.

    Writing the right bit of regex can make you type less but can be time consuming to write but what’s worst, can be time consuming to read (so maintain) at a later time (say 18 months later).

    Not to mention if that is going to be made by another guy that is seeing that for the first time.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.