
python和c语言计算ipv6地址子网源代码,使用python脚本简直就是噩梦。。。
python和c语言计算ipv6地址子网源代码,使用python脚本简直就是噩梦。。。
使用python语言计算ipv6地址子网,简直就是慢的一批,不过还好没有死机,
使用c语言计算ipv6地址子网,速度简直就是闪亮。。。下面是python和c语言计算ipv6地址子网源代码
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import ipaddress
import os
from concurrent import futures
def calculate_ipv6_subnet(subnet, prefix_length):
# 将IPv6子网地址转换为ipaddress.IPv6Network对象
ipv6_network = ipaddress.IPv6Network(subnet + '/' + str(prefix_length), strict=False)
# 生成IPv6地址的生成器
addresses = ipv6_network.hosts()
return addresses
# 输入IPv6子网地址和前缀长度
subnet = input("请输入IPv6子网地址(不带前缀长度):")
prefix_length = int(input("请输入前缀长度:"))
# 计算IPv6子网地址并写入文件
file_path = os.path.expanduser("~/ipv6_subnet_addresses.txt")
with futures.ThreadPoolExecutor() as executor, open(file_path, "w") as file:
results = executor.submit(calculate_ipv6_subnet, subnet, prefix_length)
# 将结果写入文件
for address in results.result():
file.write(address.compressed + "\n")
print(f"结果已写入到文件: {file_path}")
python ipv6subnet.py
下面是c语言计算ipv6地址子网源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct ThreadData {
struct in6_addr network_addr;
int num_addresses;
FILE* file;
};
void* calculate_ipv6_subnet(void* arg) {
struct ThreadData* data = (struct ThreadData*)arg;
struct in6_addr network_addr;
memcpy(&network_addr, &(data->network_addr), sizeof(struct in6_addr));
char ipv6_str[INET6_ADDRSTRLEN];
for (int i = 0; i < data->num_addresses; i++) {
inet_ntop(AF_INET6, &network_addr, ipv6_str, INET6_ADDRSTRLEN);
fprintf(data->file, "%s\n", ipv6_str);
for (int j = 15; j >= 0; j--) {
if (++network_addr.s6_addr[j] != 0) {
break;
}
}
}
return NULL;
}
int main() {
char subnet[INET6_ADDRSTRLEN];
int prefix_length;
int num_threads;
printf("请输入IPv6子网地址(不带前缀长度):");
scanf("%s", subnet);
printf("请输入前缀长度:");
scanf("%d", &prefix_length);
printf("请输入线程数:");
scanf("%d", &num_threads);
struct in6_addr addr;
inet_pton(AF_INET6, subnet, &addr);
int num_addresses = 1 << (128 - prefix_length);
int addresses_per_thread = num_addresses / num_threads;
int remaining_addresses = num_addresses % num_threads;
FILE* file = fopen("~/ipv6_addresses.txt", "w");
if (file == NULL) {
printf("无法打开文件!\n");
return 1;
}
pthread_t threads[num_threads];
struct ThreadData thread_data[num_threads];
for (int i = 0; i < num_threads; i++) {
memcpy(&(thread_data[i].network_addr), &addr, sizeof(struct in6_addr));
thread_data[i].num_addresses = addresses_per_thread + (i < remaining_addresses ? 1 : 0);
thread_data[i].file = file;
pthread_create(&(threads[i]), NULL, calculate_ipv6_subnet, &(thread_data[i]));
for (int j = 15; j >= 0; j--) {
if (++addr.s6_addr[j] != 0) {
break;
}
}
}
for (int i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL);
}
fclose(file);
printf("结果已写入到文件: ~/ipv6_addresses.txt\n");
return 0;
}
1
2
3
gcc -o ipv3subyouhua ipv6subyouhua.c
clang -o ipv3subyouhua ipv6subyouhua.c
./ipv3subyouhua
在Mac系统编译C语言程序需要安装Xcode命令行工具
xcode-select --install
弹出一个对话框,提示您确认安装Xcode命令行工具。点击“安装”按钮进行安装。
当然这里的C语言使用了8线程。所以速度肯定块的,执行时间如下
0.00s user 0.00s system 0% cpu 11.792 total