Getting Data In

ARIN Rest API external lookup

asucrews
Path Finder

Hello,

This is my first time creating a external lookup, and I think am missing something. The error I am getting is "Error in 'lookup' command: Could not find all of the specified lookup fields in the lookup table." Can someone please review and let me know what i am missing.

/opt/splunk/etc/apps/soc/local/transforms.conf

[arinrestapi]
 external_cmd = arinRestAPI.py src_ip
 fields_list = abuseemail,company

/opt/splunk/etc/apps/soc/bin/arinRestAPI.py

import csv
import json
import sys
import requests

def abuseEMail(ip):
     try:
         ipUrl = 'https://whois.arin.net/rest/ip/' + ip +'.json'
         r = requests.get(ipUrl)
         org = r.json()
         orgUrl = org['net']['orgRef']['$'] + '/pocs.json'
         r = requests.get(orgUrl)
         poc = r.json()
         abuseUrl = poc['pocs']['pocLinkRef'][2]['$'] + '.json'
         r = requests.get(abuseUrl)
         abuse = r.json()
         return abuse['poc']['emails']['email']['$']
     except:
         return ''

 def company(ip):
     try:
         ipUrl = 'https://whois.arin.net/rest/ip/' + ip +'.json'
         r = requests.get(ipUrl)
         org = r.json()
         orgUrl = org['net']['orgRef']['$'] + '/pocs.json'
         r = requests.get(orgUrl)
         poc = r.json()
         abuseUrl = poc['pocs']['pocLinkRef'][2]['$'] + '.json'
         r = requests.get(abuseUrl)
         abuse = r.json()
         return abuse['poc']['company']['$']
     except:
         return ''


 def main():
     if len(sys.argv) != 2:
         print "Usage: python arinRestAPI.py [ip field]"
         sys.exit(1)

     ipfield = sys.argv[1]

     infile = sys.stdin
     outfile = sys.stdout

     r = csv.DictReader(infile)
     header = r.fieldnames

     w = csv.DictWriter(outfile, fieldnames=r.fieldnames)
     w.writeheader()

     for result in r:
         if result[ipfield]:
         # only ip was provided, add host
             result[abuseEMail] = abuseEMail(result[ipfield])
             result[company] = copmany(result[ipfield])

 main()
0 Karma
1 Solution

asucrews
Path Finder

fix it, well sort of , changed it to one input field to get one output field. not sure if there way to to one input to many outpu fields.

View solution in original post

0 Karma

asucrews
Path Finder

fix it, well sort of , changed it to one input field to get one output field. not sure if there way to to one input to many outpu fields.

0 Karma

youngsuh
Contributor

could you provide step by step procedure?  We need to do same for us.  Thanks in advance for you help.

0 Karma

manish20171
New Member

Can you share the final working version? I am still getting error.

0 Karma

asucrews
Path Finder

I am making procgess but no running in this error "Script for lookup table 'arinrestapi' returned error code 1. Results may be incorrect." I still missing something but I don't understand python enoght or I don't understand what Splunk is doing.

    [arinrestapi]
     allow_caching = 0
     case_sensitive_match = 0
     external_cmd = arinrestapi.py src_ip
     fields_list = src_ip,abuseemail,company

    import csv
     import json
     import sys
     import requests

     def abuseEMail(ip):
         try:
             ipUrl = 'https://whois.arin.net/rest/ip/' + ip +'.json'
             r = requests.get(ipUrl)
             org = r.json()
             orgUrl = org['net']['orgRef']['$'] + '/pocs.json'
             r = requests.get(orgUrl)
             poc = r.json()
             abuseUrl = poc['pocs']['pocLinkRef'][2]['$'] + '.json'
             r = requests.get(abuseUrl)
             abuse = r.json()
             return abuse['poc']['emails']['email']['$']
         except:
             return ''

     def company(ip):
         try:
             ipUrl = 'https://whois.arin.net/rest/ip/' + ip +'.json'
             r = requests.get(ipUrl)
             org = r.json()
             orgUrl = org['net']['orgRef']['$'] + '/pocs.json'
             r = requests.get(orgUrl)
             poc = r.json()
             abuseUrl = poc['pocs']['pocLinkRef'][2]['$'] + '.json'
             r = requests.get(abuseUrl)
             abuse = r.json()
             return abuse['poc']['companyName']['$']
         except:
             return ''

     def main():
         if len(sys.argv) != 2:
             print "Usage: python arinRestAPI.py [ip field]"
             sys.exit(1)

         ipfield = sys.argv[1]

         infile = sys.stdin
         outfile = sys.stdout

         r = csv.DictReader(infile)
         header = r.fieldnames

         w = csv.DictWriter(outfile, fieldnames=r.fieldnames)
         w.writeheader()

         for result in r:
             if result[ipfield]:
                 result[src_ip] = result[ipfield]
                 result[abuseemail] = abuseEMail(ipfield)
                 result[company] = company(ipfield)
                 w.writerow(result)

     main()
0 Karma

asucrews
Path Finder

After playing with script i think my issue is with for result in r loop, but i not really sure.

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

 Are you ready to revolutionize your IT operations? As digital transformation accelerates, the demand for ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...