-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathip_init.js
More file actions
executable file
·64 lines (53 loc) · 2.19 KB
/
Copy pathip_init.js
File metadata and controls
executable file
·64 lines (53 loc) · 2.19 KB
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
// 此脚本用于从 init.csv 文件中提取唯一的 IP 和端口组合,并将其保存到 ip.txt 文件中。
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
// 获取当前脚本路径
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 输入 CSV 文件路径
const csvFilePath = path.resolve(__dirname, 'init.csv');
// 输出 TXT 文件路径
const txtFilePath = path.resolve(__dirname, 'ip.txt');
// 提取列
async function extractIpAndPort() {
try {
// 读取 CSV 文件内容
const data = await fs.promises.readFile(csvFilePath, 'utf8');
// 按行分割 CSV 内容
const lines = data
.split('\n')
.map((line) => line.trim())
.filter((line) => line); // 去掉空行
if (lines.length < 2) {
throw new Error('CSV 文件内容不足或格式不正确');
}
// 获取表头
const headers = lines[0].split(',');
const ipIndex = headers.findIndex((h) => h.includes('IP') || h.includes('ip'));
const portIndex = headers.findIndex((h) => h.includes('端口') || h.includes('port'));
if (ipIndex === -1 || portIndex === -1) {
throw new Error(`CSV 文件缺少 ${ipcom} 或 ${portcom} 列`);
}
// 提取 IP 和端口
const resultSet = new Set();
lines.slice(1).forEach((line, index) => {
const fields = line.split(',');
if (fields.length <= Math.max(ipIndex, portIndex)) return;
const ip = fields[ipIndex];
const port = fields[portIndex];
if (!Number(port)) {
console.log(`第${index + 2}行无效参数: ${ip} ${port}`);
return; // 直接跳过,不加入 Set
}
resultSet.add(`${ip} ${port}`);
});
const result = Array.from(resultSet).join('\n');
// 写入到 TXT 文件
await fs.promises.writeFile(txtFilePath, result, 'utf8');
console.log(`已成功提取到 ${txtFilePath}`);
} catch (error) {
console.error('处理文件时发生错误:', error.message);
}
}
await extractIpAndPort();