Last active
November 8, 2023 17:56
-
-
Save Xnuvers007/05b5556d363fc67d37ea67fac519cb90 to your computer and use it in GitHub Desktop.
generate ip address ipv4 and ipv6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code by Xnuvers007 / Indra Dwi A | |
#include <iostream> | |
#include <cstdlib> | |
#include <ctime> | |
// Fungsi untuk menghasilkan angka heksadesimal 4 digit | |
std::string generateHexDigit() { | |
std::string hexChars = "0123456789ABCDEF"; | |
std::string result; | |
for (int i = 0; i < 4; i++) { | |
result += hexChars[rand() % 16]; | |
} | |
return result; | |
} | |
// Fungsi untuk menghasilkan alamat IPv4 secara acak | |
std::string generateRandomIPv4() { | |
return std::to_string(rand() % 256) + "." + | |
std::to_string(rand() % 256) + "." + | |
std::to_string(rand() % 256) + "." + | |
std::to_string(rand() % 256); | |
} | |
// Fungsi untuk menghasilkan alamat IPv6 secara acak | |
std::string generateRandomIPv6() { | |
std::string ipv6Address = ""; | |
for (int i = 0; i < 8; i++) { | |
ipv6Address += generateHexDigit(); | |
if (i < 7) { | |
ipv6Address += ":"; | |
} | |
} | |
return ipv6Address; | |
} | |
int main() { | |
srand(static_cast<unsigned>(time(0))); | |
std::cout << "Alamat IPv4:" << std::endl; | |
for (int i = 0; i < 5; ++i) { | |
std::string ipv4 = generateRandomIPv4(); | |
std::cout << "IPv4: " << ipv4 << std::endl; | |
} | |
std::cout << "Alamat IPv6:" << std::endl; | |
for (int i = 0; i < 5; ++i) { | |
std::string ipv6 = generateRandomIPv6(); | |
std::cout << "IPv6: " << ipv6 << std::endl; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code by Xnuvers007 / Indra Dwi A | |
import ipaddress | |
import random | |
def generate_random_ipv4(): | |
return ipaddress.IPv4Address(random.randint(0, 2**32 - 1)) | |
def generate_random_ipv6(): | |
return ipaddress.IPv6Address(random.randint(0, 2**128 - 1)) | |
def validate_ipv4(ip): | |
try: | |
ipaddress.IPv4Address(ip) | |
return True | |
except ipaddress.AddressValueError: | |
return False | |
def validate_ipv6(ip): | |
try: | |
ipaddress.IPv6Address(ip) | |
return True | |
except ipaddress.AddressValueError: | |
return False | |
print("Alamat IPv4:") | |
for _ in range(10): | |
ipv4 = generate_random_ipv4() | |
print(f"{ipv4} adalah alamat IPv4 yang valid." if validate_ipv4(ipv4) else f"{ipv4} bukan alamat IPv4 yang valid.") | |
print("Alamat IPv6:") | |
for _ in range(10): | |
ipv6 = generate_random_ipv6() | |
print(f"{ipv6} adalah alamat IPv6 yang valid." if validate_ipv6(ipv6) else f"{ipv6} bukan alamat IPv6 yang valid.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment