#!/usr/bin/env python

from systematic.shell import Script, ScriptError
from seine.whois.arin import ARINReverseIPQuery, WhoisError

script = Script()
script.add_argument('--details', action='store_true', help='Show details')
script.add_argument('addresses', nargs='*', help='Addresses to lookup')
args = script.parse_args()

if not args.addresses:
    script.exit(1, 'No addresses to lookup')

for address in args.addresses:
    try:
        response = ARINReverseIPQuery(address)
        for block in response:
            print block

        if args.details:
            for key, value in response.items():
                print '%-12s %s' % (key, value)

    except WhoisError, emsg:
        script.message('Error looking up %s: %s' % (address, emsg))

