aboutsummaryrefslogtreecommitdiffstats
path: root/fap/tools/create_queries/create_queries.php
blob: d57937060f5ac5e83ecc40e5e1c1b46d6d7c084a (plain)
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
<?php
    /*
        Used for generating SQL queries for FAP
        Jonas Lindstad for TG15
        GPL V2
    */
    
    chdir(__DIR__); # sets the executing directory to be the path of this script - necessary for CLI require() usage
    header("Content-Type: text/plain");
    
    /*
        To get IPv4/IPv6 calculation functions
    */
    require '../tools_includes/ipcalc_functions.php';
    
    /*
        Fetch data sources
    
    $patchlist = file('../../../patchlist.txt');
    $switches = file('../../../switches.txt');
    */
    $patchlist = file('../../../patchlist_extras.txt');
    $switches = file('../../../switches_extras.txt');
    
    $dataset = array();
    
    foreach($patchlist as $lines){
        $pieces = explode(' ', trim($lines));
        $dataset[$pieces[0]] = array(
            'hostname' => $pieces[0],
            'distro_name' => 'rs1.' . $pieces[1], # prefix with "rs." so we get "rs1.distro0" syntax
            'distro_phy_port' => $pieces[2]
        );
    }
    
    /*
        Assign to logical and usable names in array
    */
    foreach($switches as $lines){
        $pieces = explode(' ', trim($lines));
        $dataset[$pieces[0]]['mgmt_v4_addr'] = explode('/', $pieces[3])[0];
        $dataset[$pieces[0]]['mgmt_v4_cidr'] = explode('/', $pieces[3])[1];
        $dataset[$pieces[0]]['mgmt_v4_gw'] = net_to_gw($pieces[3]);
        $dataset[$pieces[0]]['mgmt_v6_addr'] = explode('/', $pieces[4])[0];
        $dataset[$pieces[0]]['mgmt_v6_cidr'] = explode('/', $pieces[4])[1];
        $dataset[$pieces[0]]['mgmt_v6_gw'] = net_to_gw($pieces[4]);
        $dataset[$pieces[0]]['traffic_vlan'] = $pieces[5];
        $dataset[$pieces[0]]['mgmt_vlan'] = '666';
    }
    
    /*
        Prints the query rows
    */
    $sql_query = '';
    foreach($dataset as $k => $v){
        $columns = implode(', ', array_keys($v));
        $values = "'" . implode("', '", array_values($v)) . "'";
        $sql_query .= 'INSERT INTO switches (' . $columns . ') VALUES (' . $values . ');' . "\n";
    }
    
    echo $sql_query;
?>