Search code examples
node.jswhois

return only part of Whois information


Hi we are trying to use NodeJS to return IP address WHOIS information before we send the requesting IP address to the rest of our app - That part is easy.

However the part that is not easy is, selecting only the Organization part of the whois information.

for example this is a whois and what it returns

whois 137.184.236.168
% IANA WHOIS server
% for more information on IANA, visit http://www.iana.org
% This query returned 1 object

refer:        whois.arin.net

inetnum:      137.0.0.0 - 137.255.255.255
organisation: Administered by ARIN
status:       LEGACY

whois:        whois.arin.net

changed:      1993-05
source:       IANA

# whois.arin.net

NetRange:       137.184.0.0 - 137.184.255.255
CIDR:           137.184.0.0/16
NetName:        DIGITALOCEAN-137-184-0-0
NetHandle:      NET-137-184-0-0-1
Parent:         NET137 (NET-137-0-0-0-0)
NetType:        Direct Allocation
OriginAS:       AS14061
Organization:   DigitalOcean, LLC (DO-13)
RegDate:        2019-11-13
Updated:        2020-04-03
Comment:        Routing and Peering Policy can be found at https://www.as14061.net
Comment:        
Comment:        Please submit abuse reports at https://www.digitalocean.com/company/contact/#abuse
Ref:            https://rdap.arin.net/registry/ip/137.184.0.0



OrgName:        DigitalOcean, LLC
OrgId:          DO-13
Address:        101 Ave of the Americas
Address:        FL2
City:           New York
StateProv:      NY
PostalCode:     10013
Country:        US
RegDate:        2012-05-14
Updated:        2022-05-19
Ref:            https://rdap.arin.net/registry/entity/DO-13


OrgAbuseHandle: ABUSE5232-ARIN
OrgAbuseName:   Abuse, DigitalOcean 
OrgAbusePhone:  +1-347-875-6044 
OrgAbuseEmail:  [email protected]
OrgAbuseRef:    https://rdap.arin.net/registry/entity/ABUSE5232-ARIN

OrgTechHandle: NOC32014-ARIN
OrgTechName:   Network Operations Center
OrgTechPhone:  +1-347-875-6044 
OrgTechEmail:  [email protected]
OrgTechRef:    https://rdap.arin.net/registry/entity/NOC32014-ARIN

OrgNOCHandle: NOC32014-ARIN
OrgNOCName:   Network Operations Center
OrgNOCPhone:  +1-347-875-6044 
OrgNOCEmail:  [email protected]
OrgNOCRef:    https://rdap.arin.net/registry/entity/NOC32014-ARIN

The only thing we are interested in is Organization: DigitalOcean, LLC (DO-13)

As we want to drop all IP addresses from this host provider.

We noticed that we have been successful at stopping Google and AWS via using host command but Digital Ocean does not work this way and we need to do it via Whois.

I know in NodeJS I would request the information

 exec("whois "+ip, (error, stdout, stderr) => {
    console.log(stdout);
  }

Solution

  • Could use a regular expression:

    const organizationPattern = /^organization:\s*(.+)$/im;
    const match = organizationPattern.exec(stdout);
    const organization = match ? match[1] : 'unknown';
    
    console.log(organization);