惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
C
Check Point Blog
B
Blog RSS Feed
G
Google Developers Blog
H
Help Net Security
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Recent Announcements
Recent Announcements
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
DataBreaches.Net
小众软件
小众软件
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
T
Tailwind CSS Blog
J
Java Code Geeks
MyScale Blog
MyScale Blog
雷峰网
雷峰网
有赞技术团队
有赞技术团队
博客园 - 聂微东

WMI

How to Activate MySQL for Multiple Devices - WMI ESLint Flat Config for JS, TS, React, and Prettier - WMI Yarn NPM Registry Configuration - WMI Yarn Guides - WMI Set Up ADB Wireless Debugging on Android: A Full Guide - WMI How to Fill Tax Information on Google Adsense - WMI Migrate ESLint v9 for prettier typescript javascript - WMI PySide6 button click open new window - WMI PySide6 autocomplete input text - WMI Mobile Legends To The Stars Event Clue - WMI [PHP] generate big text file for testing purpose - WMI List of Chrome Driver command line arguments - WMI Happy eid mubarak - WMI Install markdown engine on vite ESM typescript - WMI Android Activity lifecycle - WMI OkHttp cookie handling on android (webview supported) - WMI Turn git log history into markdown - WMI enable automatic memory heap resizing of android studio - WMI is defining screen density can reduce build time ? - WMI
[PHP] generate random proxy IP:PORT from CIDR - WMI
Dimas Lanjaka · 2024-05-10 · via WMI

this script will Generate a random IP:PORT address within a CIDR range.

<?php

/**
 * Generate a random IP address within a CIDR range.
 *
 * @param string $cidr The CIDR range (e.g., "192.168.1.0/24").
 * @return string The random IP address.
 */
function generateRandomIP(string $cidr): string {
    list($ip, $subnet) = explode('/', $cidr);
    
    // Convert IP to binary format
    $ipBinary = ip2long($ip);
    
    // Calculate the number of available IP addresses in the subnet
    $subnetSize = pow(2, (32 - $subnet));
    
    // Generate a random offset within the subnet
    $offset = mt_rand(1, $subnetSize - 2); // Exclude network address and broadcast address
    
    // Calculate the resulting IP
    $randomIP = long2ip($ipBinary + $offset);
    
    return $randomIP;
}

/**
 * Generate a random port number.
 *
 * @return int The random port number.
 */
function generateRandomPort(): int {
    return mt_rand(1024, 65535);
}

// Example CIDR range
$cidr = "192.168.1.0/24";

// Generate a random IP address within the CIDR range
$randomIP = generateRandomIP($cidr);

// Generate a random port number
$randomPort = generateRandomPort();

// Output the random IP:Port combination
echo "Random IP:Port: $randomIP:$randomPort\n";
?>