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

推荐订阅源

爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - Franky
D
Docker
B
Blog RSS Feed
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
B
Blog
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News

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";
?>