<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
set_time_limit(0);

require ('/home/powercheck/www/inc/connect.php');
require ('/home/powercheck/www/inc/functions.php');
require_once ('/home/powercheck/www/central/inc_classes/netconf/netconf/Device.php');
$switchsip[] = '192.168.180.100';
$switchsip[] = '192.168.180.101';
$switchsip[] = '192.168.180.102';
$switchsip[] = '192.168.180.103';
$switchsip[] = '192.168.180.104';

$switchsip[] = '192.168.180.105';
$switchsip[] = '192.168.180.106';
$switchsip[] = '192.168.180.107';
$switchsip[] = '192.168.180.108';
$switchsip[] = '192.168.180.109';
$switchsip[] = '192.168.180.110';
$switchsip[] = '192.168.180.111';
$switchsip[] = '192.168.180.112';
$switchsip[] = '192.168.180.113';
$switchsip[] = '192.168.180.114';
$switchsip[] = '10.0.47.116';

$switchsip[] = '192.168.180.117';
$switchsip[] = '192.168.180.118';
$switchsip[] = '192.168.180.119';
$switchsip[] = '192.168.180.120';
$switchsip[] = '192.168.180.121';
$switchsip[] = '192.168.180.122';
$switchsip[] = '192.168.180.123';
$switchsip[] = '192.168.180.124';
$switchsip[] = '192.168.180.125';
$switchsip[] = '192.168.180.126';
$switchsip[] = '192.168.180.127';
$switchsip[] = '192.168.180.132';
$switchsip[] = '192.168.180.133';
$switchsip[] = '192.168.180.134';
$switchsip[] = '192.168.180.135';
$switchsip[] = '192.168.180.136';
$switchsip[] = '192.168.180.137';
$switchsip[] = '192.168.180.138';
$switchsip[] = '192.168.180.139';
$switchsip[] = '192.168.180.140';
$switchsip[] = '192.168.180.141';
$switchsip[] = '192.168.180.142';
$switchsip[] = '192.168.180.143';
$switchsip[] = '192.168.180.144';
$switchsip[] = '192.168.180.145';
$switchsip[] = '192.168.180.146';


foreach ($switchsip as $ip) {

    echo '<br>attempting to connect to ' . $ip . ' and return display set config<br>';

    $param = array(
        "hostname" => $ip,
        "username" => "central",
        "password" => "fd38*M0i");


    $d = new Device($param);

    $d->setReplyTimeout(10);
    $d->setConnectTimeout(15);
    
    try{
    $d->connect();
    } catch (exception $e) {
        echo '<br>exception', $e->getMessage(), "\n<br>";
        break ;
    }

    
    try {
        
        echo "\n connected to device\n";
        $reply = $d->get_running_config_display_set();


        $explode1 = explode("<configuration-output>", $reply);
        $explode2 = explode("</configuration-output>", $explode1[1]);
        $contents2 = explode("\n", $explode2[0]);
        $switchreturn[$ip] = $contents2;


    }

    catch (exception $e) {
        //echo 'exception', $e->getMessage(), "\n";
    }


    //closing device
    $d->close();
    echo "device closed";
    echo '<br><br><br>';
    unset($d);
}
echo '<pre>';
//print_r($switchreturn);


$timestamp = time();
$sql[] = "truncate `powercheck`.`juniper_config_live`;";
foreach ($switchreturn as $ip => $configlines) {

    foreach ($configlines as $configline) {

        $sql[] = "INSERT INTO `powercheck`.`juniper_config_live` (`timestamp`, `juniperip`, `displayset`) VALUES ('" .
            $timestamp . "', '" . $ip . "', '" . $configline . "');";
        $sql[] = "INSERT INTO `powercheck`.`juniper_config_live_historic` (`timestamp`, `juniperip`, `displayset`) VALUES ('" .
            $timestamp . "', '" . $ip . "', '" . $configline . "');";

    }
}

print_r($sql);
print_R(insert_mysql_array($mysqli, $sql));
?>

