ASN lookup using php

I need a piece of php code which would accept an IP number, doing a lookup for it's ASN org.
Don't need any other information, only the complete organization name that owns it.

Can't find anything on the net so thought I would ask here.

Thank you.
projectsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Have you got any references to share with us?  A Google search for ASN does not really seem helpful.  With an IP number we can find a lot of things if we know what we're looking for... :-)
Dave BaldwinFixer of ProblemsCommented:
This page http://www.iana.org/assignments/as-numbers/as-numbers.xhtml tells you where to look.  There are at least 5 different organizations that handle ASNs for different parts of the world.  Note also there are 16-bit and 32-bit ASNs.
giltjrCommented:
O.K, what about:

curl http://ipinfo.io/########/org

Where ######## is the IP address you want to look at.  This is what I originally suggested in your other question (http://www.experts-exchange.com/Networking/Windows_Networking/Internet_Protocols/Q_28626162.html)  when I though you had the address and needed the ASN.

In fact you can:

curl http://ipinfo.io/#######/org | awk '{print $1}'

And it will print out just the ASN.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SolarWinds® VoIP and Network Quality Manager(VNQM)

WAN and VoIP monitoring tools that can help with troubleshooting via an intuitive web interface. Review quality of service data, including jitter, latency, packet loss, and MOS. Troubleshoot call performance and correlate call issues with WAN performance for Cisco and Avaya calls

Dave BaldwinFixer of ProblemsCommented:
@giltjr, that works perfectly!
projectsAuthor Commented:
@giltjr, yes, this is what we determined from the other question :).

In this question, I am looking to do the same thing but using php code instead.
Ray PaseurCommented:
On this page:
http://www.iana.org/assignments/as-numbers/as-numbers.xhtml

There are links to two CSV files, looks like one for the 16-bit and one for the 32-bit.  You can use PHP fgetcsv() to "harvest" this data.  Do you know how to write PHP scripts?
Ray PaseurCommented:
See if this helps you get started.  You may want to modify this script to use $links[1] as well as $links[0] so you can get both 16-bit and 32-bit lists.
http://iconoun.com/demo/temp_projects.php

<?php // demo/temp_projects.php

/**
 * See http://www.experts-exchange.com/Networking/Misc/Q_28629566.html
 */
error_reporting(E_ALL);

$links = [
'http://www.iana.org/assignments/as-numbers/as-numbers-1.csv',
'http://www.iana.org/assignments/as-numbers/as-numbers-2.csv',
];

$fpr = fopen($links[0], 'r');
if (!$fpr) trigger_error("Unable to open: " . $links[0]. E_USER_ERROR);

echo '<table>';

$top = fgetcsv($fpr);
$ths = '<tr>'
. '<th>' . $top[0] . '</th>'
. '<th>' . $top[1] . '</th>'
. '<th>' . $top[2] . '</th>'
. '<th>' . $top[3] . '</th>'
. '<th>' . $top[4] . '</th>'
. '</tr>'
. PHP_EOL
;
echo $ths;

while($row = fgetcsv($fpr))
{
    $tds = '<tr>'
    . '<td>' . $row[0] . '</td>'
    . '<td>' . $row[1] . '</td>'
    . '<td>' . $row[2] . '</td>'
    . '<td>' . $row[3] . '</td>'
    . '<td>' . $row[4] . '</td>'
    . '</tr>'
    . PHP_EOL
    ;
    echo $tds;
}

echo '</table>';

Open in new window

Dave BaldwinFixer of ProblemsCommented:
PHP includes cURL.  Use that.
giltjrCommented:
Wait, you don't have ncat installed, but you do have PHP?

Well you could use PHP to retrieve the information from ipinfo.io and then use PHP to parse the output.

Although it may use a different website I think that is what Ray Paseur's post is doing.
projectsAuthor Commented:
Ray, by now, you already know I don't know php and never will. The questions I ask here are to get me started in the right direction, so that I can build a demo or pieces which I can later have a real programmer complete or add.

giltjr, no, I don't have php installed on the same device as we were talking about before. This code is to run on a lamp server so there I do have php :).
Ray PaseurCommented:
Sorry, I don't really keep track of what people know or have learned over the years.  I just try to answer the questions as they come.  Sometimes a good answer for one person will be "talking down" to another person, and we don't want to do that.  

Did you try the script I posted above?
projectsAuthor Commented:
Ray, I've seen your replies on my questions so many times Ray, I figured you would remember me by now. Most of your replies are typically links to more stuff to read :)

Yes, your code is looking at those csv files and outputting a list.

In the end, all I want to do is to maintain my own table of asn/ips/org, either starting from a known updated file on the net or by doing lookups. I don't want to abuse any of the services out there so when I do a lookup, I'd like to update the database so I don't have to do it again.

Couple of interesting ones might be;

http://thyme.apnic.net/current/data-used-autnums
http://thyme.apnic.net/current/data-raw-table
giltjrCommented:
Ray is involved in way more questions than I am and there is really no way I can remember somebody.  In fact most of the time I don't even look at who posted the question, I just try and answer it.

Do you want just a list of all ASN's and who owns them?  Or are you trying to map every public IP subnet to a ASN and track who owns both the subnet and the ASN?
Dave BaldwinFixer of ProblemsCommented:
Most of the time if we do remember you... it's because we Don't want to answer your questions.
Ray PaseurCommented:
This article is old, but it shows some of the principles you might want to implement.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_3437-IP-Address-to-Country-in-PHP.html

Examining the data at the thyme.apnic.net URLS, I find this:
http://thyme.apnic.net/current/data-used-autnums has 42,766 rows.
http://thyme.apnic.net/current/data-raw-table has 535,301 rows.

What would you like to do with the data?  It looks like used-autnums associates a numeric key with a named registrar, and the raw-table associates an IP address with a numeric key.  Do the keys relate across the tables?  If so, it's easy enough to join them.
giltjrCommented:
I too would be interested in knowing why you want to know who owns an ASN and what IP addresses may be part of a ASN.

Just to make sure you realize, not all ISP's delegate ownership of a IP subnet to a customer.  So although you may find that an IP address  is assigned to say Sprint, and the ASN that is associated with that IP address is Sprint, that does not mean it is actually Sprint that is using that address.  It could be any one of their customers.

You may also find that an IP address is owned by company "X", but that ASN that it is associated with is say Level 3.  Level 3 may have delegated that subnet to company X so it can setup PTR records, but if company X does not have its own ASN or is not multi-homed then the ASN will be that of Level 3.
projectsAuthor Commented:
Well, I certainly remember your names. It is somewhat rare to see new names in fact. Is there a small pool of people who reply only?

@giltjr;
>Do you want just a list of all ASN's and who owns them?  Or are you trying to map
>every public IP subnet to a ASN and track who owns both the subnet and the ASN?

For which ever reason they need it, I cannot tell you. What I do know is that is that they don't want to over use free services on the net so instead of looking things up over and over, the idea is to stick it in a db so we can do a local lookup.

>although you may find that an IP address  is assigned to say Sprint, and the ASN that
>is associated with that IP address is Sprint, that does not mean it is actually Sprint
>that is using that address.  It could be any one of their customers.

Sure, that makes sense. If a large enough top entity has a large enough customer which wants its own ASN, then the top tier could assign it to the customer. Something along those lines?
That is an interesting problem.

@Ray;
>What would you like to do with the data?  It looks like used-autnums associates a numeric key with a >named registrar, and the raw-table associates an IP address with a numeric key.  Do the keys relate >across the tables?  If so, it's easy enough to join them.

I am not sure what this means, sorry.
Dave BaldwinFixer of ProblemsCommented:
If you go to the main page for any topic, you can see a list of the 'Top Experts' in that topic area which are the people that answer the most questions in that topic.
giltjrCommented:
@Sure, that makes sense. If a large enough top entity has a large enough customer which wants its own ASN, then the top tier could assign it to the customer. Something along those lines?
That is an interesting problem.

Not necessarily a large customer.  The only reason to, that I know of, to get your own ASN is if you are multi-homed through different ISP's and you need at least a /24 to get an ASN.  If you have less that a /24 you can't get an ASN.
Ray PaseurCommented:
JOIN is a term of art in data base programming.  It lets you relate information in one table to information in another table (hence the term "relational" data base).
projectsAuthor Commented:
@giltjr; Off topic to some extent at this point but I understand what you're saying about assignment.
Thanks for the info it helps.

I've hired someone to work this out for me but the leads were helpful, thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Networking

From novice to tech pro — start learning today.