221 lines
9.1 KiB
PHP
221 lines
9.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* This script parses a wiki text file containing network information and generates an SQL script
|
|
* to populate the IpNetwork table with a complete, hierarchical representation of the network.
|
|
*
|
|
* @version 1.0
|
|
* @author Gemini
|
|
*/
|
|
|
|
// --- Configuration ---
|
|
$inputFile = 'combined_wiki.txt';
|
|
$outputFile = 'initial_data.sql';
|
|
$dbTableName = 'IpNetwork';
|
|
|
|
// --- Main Execution ---
|
|
|
|
// Read and parse the wiki file into a structured PHP array
|
|
$networkData = parseWikiFile($inputFile);
|
|
|
|
// Generate the SQL script from the parsed data
|
|
$sqlScript = generateSqlScript($networkData, $dbTableName);
|
|
|
|
// Save the generated SQL script to the output file
|
|
file_put_contents($outputFile, $sqlScript);
|
|
|
|
echo "SQL-Skript wurde erfolgreich in '$outputFile' generiert.\n";
|
|
|
|
|
|
/**
|
|
* Parses the entire wiki text file and organizes the network data hierarchically.
|
|
*
|
|
* @param string $filename The path to the wiki text file.
|
|
* @return array A structured array containing all network information.
|
|
*/
|
|
function parseWikiFile(string $filename): array
|
|
{
|
|
if (!file_exists($filename)) {
|
|
die("Fehler: Eingabedatei '$filename' nicht gefunden.\n");
|
|
}
|
|
|
|
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
$networks = [];
|
|
$currentNetwork = null;
|
|
$currentSubnet = null;
|
|
|
|
foreach ($lines as $line) {
|
|
// Ignore section headers
|
|
if (preg_match('/^=====.*=====$/', $line)) {
|
|
continue;
|
|
}
|
|
|
|
// Match network/subnet definitions (e.g., "10.0.0.0/29 stko-tauka" or "10.0.0.1/32 Gateway")
|
|
if (preg_match('/^([\d\.]+)\/(\d+)\s*(.*)$/', trim($line), $matches)) {
|
|
$ip = $matches[1];
|
|
$cidr = (int)$matches[2];
|
|
$description = trim($matches[3]);
|
|
|
|
// Determine if it's a main network or a subnet based on CIDR
|
|
if ($cidr < 29) { // Heuristic: Treat larger blocks as potential parent subnets
|
|
$currentSubnet = "$ip/$cidr";
|
|
$networks[$currentSubnet] = [
|
|
'ip' => $ip,
|
|
'cidr' => $cidr,
|
|
'name' => $description,
|
|
'description' => $description,
|
|
'children' => []
|
|
];
|
|
} else { // Treat smaller blocks and hosts as children
|
|
if ($currentSubnet) {
|
|
$networks[$currentSubnet]['children'][] = [
|
|
'ip' => $ip,
|
|
'cidr' => $cidr,
|
|
'name' => $description,
|
|
'description' => $description,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
// Match host definitions (e.g., "10.0.0.2 stko-tauk:stko")
|
|
elseif (preg_match('/^([\d\.]+)\s+(.*)$/', trim($line), $matches)) {
|
|
$ip = $matches[1];
|
|
$description = trim($matches[2]);
|
|
|
|
if ($currentSubnet) {
|
|
$networks[$currentSubnet]['children'][] = [
|
|
'ip' => $ip,
|
|
'cidr' => 32, // Assume /32 for host entries
|
|
'name' => $description,
|
|
'description' => $description,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
return $networks;
|
|
}
|
|
|
|
|
|
/**
|
|
* Generates the complete SQL script string.
|
|
*
|
|
* @param array $data The structured network data.
|
|
* @param string $tableName The name of the database table.
|
|
* @return string The complete SQL script.
|
|
*/
|
|
function generateSqlScript(array $data, string $tableName): string
|
|
{
|
|
// --- SQL Header ---
|
|
$sql = "-- SQL-Daten für die Tabelle '$tableName'\n";
|
|
$sql .= "-- Automatisch generiert aus der Wiki-Dokumentation\n\n";
|
|
$sql .= "TRUNCATE TABLE `$tableName`;\n\n";
|
|
$sql .= "-- Eltern-Netzwerke (Top-Level)\n";
|
|
|
|
// --- Insert Top-Level Parent Networks ---
|
|
$parents = [
|
|
['10.0.0.0', 8, 'Privates Netzwerk Klasse A', 'RFC 1918 privater Adressbereich für große Netzwerke.'],
|
|
['172.16.0.0', 12, 'Privates Netzwerk Klasse B', 'RFC 1918 privater Adressbereich für mittlere Netzwerke.'],
|
|
['192.168.0.0', 16, 'Privates Netzwerk Klasse C', 'RFC 1918 privater Adressbereich für kleine Netzwerke.'],
|
|
['100.64.0.0', 10, 'Carrier-Grade NAT (CGNAT)', 'RFC 6598 Adressbereich für Carrier-Grade NAT.'],
|
|
['5.206.200.0', 21, 'Öffentlicher Netzblock 5.206.200.0/21', 'Öffentlicher IP-Adressbereich.'],
|
|
['185.29.88.0', 22, 'Öffentlicher Netzblock 185.29.88.0/22', 'Öffentlicher IP-Adressbereich.'],
|
|
['193.105.204.0', 22, 'Öffentlicher Netzblock 193.105.204.0/22', 'Öffentlicher IP-Adressbereich.'],
|
|
['193.186.244.0', 22, 'Öffentlicher Netzblock 193.186.244.0/22', 'Öffentlicher IP-Adressbereich.'],
|
|
['45.82.168.0', 22, 'Öffentlicher Netzblock 45.82.168.0/22', 'Öffentlicher IP-Adressbereich.'],
|
|
['46.151.200.0', 21, 'Öffentlicher Netzblock 46.151.200.0/21', 'Öffentlicher IP-Adressbereich.'],
|
|
['91.227.230.0', 22, 'Öffentlicher Netzblock 91.227.230.0/22', 'Öffentlicher IP-Adressbereich.']
|
|
];
|
|
|
|
foreach ($parents as $p) {
|
|
$sql .= generateInsertStatement($tableName, $p[0], $p[1], 'NULL', 'active', $p[2], $p[3]);
|
|
}
|
|
|
|
// --- Insert Child Networks and Hosts ---
|
|
$sql .= "\n-- Kind-Netzwerke und Hosts aus der Wiki-Dokumentation\n";
|
|
foreach ($data as $subnetKey => $subnet) {
|
|
// Insert the subnet itself, linking it to the correct top-level parent
|
|
$parentSelect = getParentSelect($subnet['ip']);
|
|
$sql .= generateInsertStatement($tableName, $subnet['ip'], $subnet['cidr'], "($parentSelect)", 'active', $subnet['name'], $subnet['description']);
|
|
|
|
// Insert all children of this subnet
|
|
if (!empty($subnet['children'])) {
|
|
$childParentSelect = "SELECT id FROM `$tableName` p WHERE p.network_address = INET_ATON('{$subnet['ip']}') AND p.cidr = {$subnet['cidr']}";
|
|
foreach ($subnet['children'] as $child) {
|
|
$sql .= generateInsertStatement($tableName, $child['ip'], $child['cidr'], "($childParentSelect)", 'active', $child['name'], $child['description']);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $sql;
|
|
}
|
|
|
|
/**
|
|
* Generates a single SQL INSERT statement.
|
|
*
|
|
* @param string $tableName
|
|
* @param string $ip
|
|
* @param int $cidr
|
|
* @param string $parentIdSql SQL string for parent ID (can be 'NULL' or a subquery).
|
|
* @param string $status
|
|
* @param string $name
|
|
* @param string $description
|
|
* @return string The generated INSERT statement.
|
|
*/
|
|
function generateInsertStatement(string $tableName, string $ip, int $cidr, string $parentIdSql, string $status, string $name, string $description): string
|
|
{
|
|
$name = substr(addslashes($name), 0, 100);
|
|
$description = addslashes($description);
|
|
|
|
return "INSERT INTO `$tableName` (`network_address`, `cidr`, `parent_network_id`, `status`, `name`, `description`, `location`, `create`, `edit`) VALUES " .
|
|
"(INET_ATON('$ip'), $cidr, $parentIdSql, '$status', '$name', '$description', NULL, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());\n";
|
|
}
|
|
|
|
/**
|
|
* Determines the correct parent network's SELECT statement based on the IP address.
|
|
*
|
|
* @param string $ip The IP address of the child network.
|
|
* @return string A SQL SELECT statement to find the parent ID.
|
|
*/
|
|
function getParentSelect(string $ip): string
|
|
{
|
|
if (strpos($ip, '10.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('10.0.0.0') AND p.cidr = 8";
|
|
}
|
|
if (strpos($ip, '172.16.') === 0 || strpos($ip, '172.17.') === 0 || strpos($ip, '172.18.') === 0 || strpos($ip, '172.19.') === 0 || strpos($ip, '172.2') === 0 || strpos($ip, '172.30.') === 0 || strpos($ip, '172.31.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('172.16.0.0') AND p.cidr = 12";
|
|
}
|
|
if (strpos($ip, '192.168.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('192.168.0.0') AND p.cidr = 16";
|
|
}
|
|
if (strpos($ip, '100.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('100.64.0.0') AND p.cidr = 10";
|
|
}
|
|
if (strpos($ip, '5.206.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('5.206.200.0') AND p.cidr = 21";
|
|
}
|
|
if (strpos($ip, '185.29.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('185.29.88.0') AND p.cidr = 22";
|
|
}
|
|
if (strpos($ip, '193.105.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('193.105.204.0') AND p.cidr = 22";
|
|
}
|
|
if (strpos($ip, '193.186.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('193.186.244.0') AND p.cidr = 22";
|
|
}
|
|
if (strpos($ip, '45.82.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('45.82.168.0') AND p.cidr = 22";
|
|
}
|
|
if (strpos($ip, '46.151.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('46.151.200.0') AND p.cidr = 21";
|
|
}
|
|
if (strpos($ip, '91.227.') === 0) {
|
|
return "SELECT id FROM `IpNetwork` p WHERE p.network_address = INET_ATON('91.227.230.0') AND p.cidr = 22";
|
|
}
|
|
|
|
// Fallback for networks that don't match a known parent
|
|
return 'NULL';
|
|
}
|
|
|
|
?>
|