49

Obtaining IP addresses from CIDR notation in PHP

 4 years ago
source link: https://www.codesd.com/item/obtaining-ip-addresses-from-cidr-notation-in-php.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Obtaining IP addresses from CIDR notation in PHP

advertisements

Is there a way (or function/class) to get the list of IP addresses from a CIDR notation?

For example, I have 73.35.143.32/27 CIDR and want to get the list of all IP's in this notation. Any suggestions?

Thank you.


I'll edit the aforementioned class to contain a method for that. Here is the code I came up with that might help you until then.

function cidrToRange($cidr) {
  $range = array();
  $cidr = explode('/', $cidr);
  $range[0] = long2ip((ip2long($cidr[0])) & ((-1 << (32 - (int)$cidr[1]))));
  $range[1] = long2ip((ip2long($range[0])) + pow(2, (32 - (int)$cidr[1])) - 1);
  return $range;
}
var_dump(cidrToRange("73.35.143.32/27"));

//////////////////OUTPUT////////////////////////
// array(2) {
//   [0]=>
//   string(12) "73.35.143.32"
//   [1]=>
//   string(12) "73.35.143.63"
// }
/////////////////////////////////////////////////

Returns the low end of the ip range as the first entry in the array, then returns the high end as the second entry.

Related Articles

Obtain IP addresses from udp and http tracker torrent response

I am trying to get the peer-list: list of IP addresses from a torrent tracker Similar to the question here: how to get the peer list from torrent tracker response I wrote code which decodes the torrent files using the python bencode Bit-torrent libra

Obtain a variable from the database with PHP

Ok obviously i'm a beginner. What i'm trying to do here is probably simple. I'm trying to get all the field of my database to echo on my website. So i use this to show only the basic details: <?php // Make a MySQL Connection mysql_connect("localho

obtain multiple address from multiple latitude / longitude

I want to get addresses of multiple latitude/longitude, is there any way to do that in one request? I can get address of one location using this code : try { Geocoder geocoder; List<Address> addresses; geocoder = new Geocoder(MainActivity.this, Loca

How can I generate all possible IP addresses from a list of IP ranges in Python?

Let's say I have a text file contains a bunch of ip ranges like this: x.x.x.x-y.y.y.y x.x.x.x-y.y.y.y x.x.x.x-y.y.y.y x.x.x.x-y.y.y.y x.x.x.x-y.y.y.y x.x.x.x is start value and y.y.y.y is end value of range. How can I convert these ip ranges to all p

How do I generate all possible IP addresses from a CIDR list in Python?

Let's say I have a text file contains a bunch of cidr ip ranges like this: x.x.x.x/24 x.x.x.x/24 x.x.x.x/23 x.x.x.x/23 x.x.x.x/22 x.x.x.x/22 x.x.x.x/21 and goes on... How can I convert these cidr notations to all possible ip list in a new text file i

What is my IP address in the CIDR notation?

I'm trying to following the Quick Start Wordpress for Google App Engine guide; however, the Google Cloud Console appears to have changed slightly with respect to the instructions. One of the instructions is to select Assign IP Address, and add your I

Is there a way to spoof IP address when retrieving content from a page via PHP

Heyy guys I'm trying to know if it's possible to spoof my server ip address while I fetch content from an url through file_get_contents/curl. I tried X-Forwarded-For with curl but [REMOTE_ADDRESS] on second page (page that is being fetched) is still

Obtaining a server IP address from the hostname

When performing an NSURLRequest to a hostname, is it possible to obtain the IP address of the server that the response came from? The NSURL method: - (NSString *)host; simply returns the hostname, and I see no way of obtaining the IP address from any

Amazon VPC n ^ 2 -4 IP addresses? CIDR Block

I was in the process of creating a new AWS VPC for my instances. However, I noticed that when I used CIDR Notation to create the VPC & Public Subnet, AWS indicates that I have n^2 - 4 (where n is the # of bits) available IP addresses? Why is this? I

Protect e-mail addresses from spambots / crawlers

How do you prevent emails being gathered from web pages by email spiders? Does mailto: linking them increase the likelihood of them being picked up? Is URL-encoding useful? Obviously the best counter-measure is to only show email addresses to logged-

Send an e-mail to the address from the form entry

I have a pretty standard contact form with inputs for name, email address and phone number. As well as sending the email to the specified email address as per a standard form, $to = '[email protected]'; I would also like to send the email to t

Local local IP address from a remote client in java

Does anyone know how to get the Local Net Ip Address from a Remote Client in java? thxIs this what you are looking for? InetAddress addr = InetAddress.getLocalHost(); byte[] ipAddr = addr.getAddress(); // Get IP Address String hostname = addr.getHost

Get json from the API using PHP

I have this API mywebsite.com/v1/api/Endorsements/xxxxxx/User/5003C2D5-6FD6-49BA-A309-EBB81A89FB60/RequestType/All/PageNo/1/PageSize/6/Object/null/Category/null/Search/null/Latitude/null/Longitude/null/Radius/null/StartDate/null/EndDate/null?Auth-Tok

Extract the e-mail address from the Outlook Exchange sender to Python by using win32

I am trying to extract the sender's email address from outlook 2013 using win32 package in python. There are two kinds of email address type in my Inbox, exchange and smtp. If I try to print the the sender's email address of Exchange type, I am getti

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK