Exemplo n.º 1
0
/**
 * Function to edit a lab config.
 *
 * @param   Lab     $lab                Lab
 * @param   Array   $p                  Parameters
 * @return  Array                       Return code (JSend data)
 */
function apiEditLabConfig($lab, $p)
{
    if (!isset($lab->getNodes()[$p['id']])) {
        // Node not found
        $output['code'] = 404;
        $output['status'] = 'fail';
        $output['message'] = $GLOBALS['messages'][20024];
        return $output;
    }
    // Edit config
    $rc = $lab->setNodeConfigData($p['id'], $p['data']);
    if ($rc === 0) {
        $output['code'] = 201;
        $output['status'] = 'success';
        $output['message'] = $GLOBALS['messages'][60023];
    } else {
        $output['code'] = 400;
        $output['status'] = 'fail';
        $output['message'] = $GLOBALS['messages'][$rc];
    }
    return $output;
}
Exemplo n.º 2
0
Arquivo: cli.php Projeto: qyqx/unetlab
/**
 * Function to export a node running-config.
 *
 * @param   int     $node_id            Node ID
 * @param   Node    $n                  Node
 * @param   Lab     $lab                Lab
 * @return  int                         0 means ok
 */
function export($node_id, $n, $lab)
{
    $tmp = tempnam(sys_get_temp_dir(), 'unl_cfg_' . $node_id . '_');
    if (is_file($tmp) && !unlink($tmp)) {
        // Cannot delete tmp file
        error_log('ERROR: ' . $GLOBALS['messages'][80059]);
        return 80059;
    }
    switch ($n->getNType()) {
        default:
            // Unsupported
            error_log('ERROR: ' . $GLOBALS['messages'][80061]);
            return 80061;
            break;
        case 'dynamips':
            foreach (scandir($n->getRunningPath()) as $filename) {
                if (preg_match('/_nvram$/', $filename)) {
                    $nvram = $n->getRunningPath() . '/' . $filename;
                    break;
                } else {
                    if (preg_match('/_rom$/', $filename)) {
                        $nvram = $n->getRunningPath() . '/' . $filename;
                        break;
                    }
                }
            }
            if (!isset($nvram) || !is_file($nvram)) {
                // NVRAM file not found
                error_log('ERROR: ' . $GLOBALS['messages'][80066]);
                return 80066;
            }
            $cmd = '/usr/bin/nvram_export ' . $nvram . ' ' . $tmp;
            exec($cmd, $o, $rc);
            error_log('INFO: exporting ' . $cmd);
            if ($rc != 0) {
                error_log('ERROR: ' . $GLOBALS['messages'][80060]);
                error_log((string) $o);
                return 80060;
            }
            break;
        case 'iol':
            $nvram = $n->getRunningPath() . '/nvram_' . sprintf('%05u', $node_id);
            if (!is_file($nvram)) {
                // NVRAM file not found
                error_log('ERROR: ' . $GLOBALS['messages'][80066]);
                return 80066;
            }
            $cmd = '/opt/unetlab/scripts/iou_export ' . $nvram . ' ' . $tmp;
            exec($cmd, $o, $rc);
            usleep(1);
            error_log('INFO: exporting ' . $cmd);
            if ($rc != 0) {
                error_log('ERROR: ' . $GLOBALS['messages'][80060]);
                error_log((string) $o);
                return 80060;
            }
            break;
        case 'qemu':
            if (is_file('/opt/unetlab/scripts/config_' . $n->getTemplate() . '.py')) {
                $cmd = '/opt/unetlab/scripts/config_' . $n->getTemplate() . '.py -a get -p ' . $n->getPort() . ' -f ' . $tmp . ' -t 15';
                exec($cmd, $o, $rc);
                error_log('INFO: exporting ' . $cmd);
                if ($rc != 0) {
                    error_log('ERROR: ' . $GLOBALS['messages'][80060]);
                    error_log((string) $o);
                    return 80060;
                }
            }
    }
    if (!is_file($tmp)) {
        // File not found
        error_log('ERROR: ' . $GLOBALS['messages'][80062]);
        return 80062;
    }
    // Now save the config file within the lab
    $fp = fopen($tmp, 'r');
    if (!isset($fp)) {
        // Cannot open file
        error_log('ERROR: ' . $GLOBALS['messages'][80064]);
        return 80064;
    }
    $config_data = fread($fp, filesize($tmp));
    if ($config_data === False || $config_data === '') {
        // Cannot read file
        error_log('ERROR: ' . $GLOBALS['messages'][80065]);
        return 80065;
    }
    if ($lab->setNodeConfigData($node_id, $config_data) !== 0) {
        // Failed to save startup-config
        error_log('ERROR: ' . $GLOBALS['messages'][80063]);
        return 80063;
    }
    if (!unlink($tmp)) {
        // Failed to remove tmp file
        error_log('WARNING: ' . $GLOBALS['messages'][80070]);
    }
    return 0;
}
Exemplo n.º 3
0
/**
 * Function to export a node running-config.
 *
 * @param   int     $node_id            Node ID
 * @param   Node    $n                  Node
 * @param   Lab     $lab                Lab
 * @return  int                         0 means ok
 */
function export($node_id, $n, $lab)
{
    $tmp = tempnam(sys_get_temp_dir(), 'unl_cfg_' . $node_id . '_');
    if (is_file($tmp) && !unlink($tmp)) {
        // Cannot delete tmp file
        error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80059]);
        return 80059;
    }
    switch ($n->getNType()) {
        default:
            // Unsupported
            error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80061]);
            return 80061;
            break;
        case 'dynamips':
            foreach (scandir($n->getRunningPath()) as $filename) {
                if (preg_match('/_nvram$/', $filename)) {
                    $nvram = $n->getRunningPath() . '/' . $filename;
                    break;
                } else {
                    if (preg_match('/_rom$/', $filename)) {
                        $nvram = $n->getRunningPath() . '/' . $filename;
                        break;
                    }
                }
            }
            if (!isset($nvram) || !is_file($nvram)) {
                // NVRAM file not found
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80066]);
                return 80066;
            }
            $cmd = '/opt/unetlab/scripts/wrconf_dyn.py -p ' . $n->getPort() . ' -t 15';
            exec($cmd, $o, $rc);
            error_log(date('M d H:i:s ') . 'INFO: force write configuration ' . $cmd);
            if ($rc != 0) {
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80060]);
                error_log(date('M d H:i:s ') . (string) $o);
                return 80060;
            }
            $cmd = '/usr/bin/nvram_export ' . $nvram . ' ' . $tmp;
            exec($cmd, $o, $rc);
            error_log(date('M d H:i:s ') . 'INFO: exporting ' . $cmd);
            if ($rc != 0) {
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80060]);
                error_log(date('M d H:i:s ') . (string) $o);
                return 80060;
            }
            break;
        case 'vpcs':
            if (!is_file($n->getRunningPath() . '/startup.vpc')) {
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80062]);
            } else {
                copy($n->getRunningPath() . '/startup.vpc', $tmp);
            }
            break;
        case 'iol':
            $nvram = $n->getRunningPath() . '/nvram_' . sprintf('%05u', $node_id);
            if (!is_file($nvram)) {
                // NVRAM file not found
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80066]);
                return 80066;
            }
            $cmd = '/opt/unetlab/scripts/wrconf_iol.py -p ' . $n->getPort() . ' -t 15';
            exec($cmd, $o, $rc);
            error_log(date('M d H:i:s ') . 'INFO: force write configuration ' . $cmd);
            if ($rc != 0) {
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80060]);
                error_log(date('M d H:i:s ') . (string) $o);
                return 80060;
            }
            $cmd = '/opt/unetlab/scripts/iou_export ' . $nvram . ' ' . $tmp;
            exec($cmd, $o, $rc);
            usleep(1);
            error_log(date('M d H:i:s ') . 'INFO: exporting ' . $cmd);
            if ($rc != 0) {
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80060]);
                error_log(date('M d H:i:s ') . implode("\n", $o));
                return 80060;
            }
            // Add no shut
            if (is_file($tmp)) {
                file_put_contents($tmp, preg_replace('/(\\ninterface.*)/', '$1' . chr(10) . ' no shutdown', file_get_contents($tmp)));
            }
            break;
        case 'qemu':
            if ($n->getStatus() < 2 || !isset($GLOBALS['node_config'][$n->getTemplate()])) {
                // Skipping powered off nodes or unsupported nodes
                error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][80084]);
                return 80084;
            } else {
                $cmd = '/opt/unetlab/scripts/' . $GLOBALS['node_config'][$n->getTemplate()] . ' -a get -p ' . $n->getPort() . ' -f ' . $tmp . ' -t 15';
                exec($cmd, $o, $rc);
                error_log(date('M d H:i:s ') . 'INFO: exporting ' . $cmd);
                if ($rc != 0) {
                    error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80060]);
                    error_log(date('M d H:i:s ') . implode("\n", $o));
                    return 80060;
                }
                // Add no shut
                if (($n->getTemplate() == "crv" || $n->getTemplate() == "vios" || $n->getTemplate() == "viosl2" || $n->getTemplate() == "xrv") && is_file($tmp)) {
                    file_put_contents($tmp, preg_replace('/(\\ninterface.*)/', '$1' . chr(10) . ' no shutdown', file_get_contents($tmp)));
                }
            }
    }
    if (!is_file($tmp)) {
        // File not found
        error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80062]);
        return 80062;
    }
    // Now save the config file within the lab
    clearstatcache();
    $fp = fopen($tmp, 'r');
    if (!isset($fp)) {
        // Cannot open file
        error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80064]);
        return 80064;
    }
    $config_data = fread($fp, filesize($tmp));
    if ($config_data === False || $config_data === '') {
        // Cannot read file
        error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80065]);
        return 80065;
    }
    if ($lab->setNodeConfigData($node_id, $config_data) !== 0) {
        // Failed to save startup-config
        error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80063]);
        return 80063;
    }
    if (!unlink($tmp)) {
        // Failed to remove tmp file
        error_log(date('M d H:i:s ') . 'WARNING: ' . $GLOBALS['messages'][80070]);
    }
    return 0;
}
Exemplo n.º 4
0
function apiImportLabs($p)
{
    ini_set('max_execution_time', '300');
    ini_set('memory_limit', '64M');
    if (!isset($p['file']) || empty($p['file'])) {
        // Upload failed
        $output['code'] = 400;
        $output['status'] = 'fail';
        $output['message'] = $GLOBALS['messages'][80081];
        return $output;
    }
    if (!isset($p['path'])) {
        // Path is not set
        $output['code'] = 400;
        $output['status'] = 'fail';
        $output['message'] = $GLOBALS['messages'][80076];
        return $output;
    }
    if (checkFolder(BASE_LAB . $p['path']) !== 0) {
        // Path is not valid
        $output['code'] = 400;
        $output['status'] = 'fail';
        $output['message'] = $GLOBALS['messages'][80077];
        return $output;
    }
    $finfo = new finfo(FILEINFO_MIME);
    if (strpos($finfo->file($p['file']), 'application/zip') !== False) {
        // UNetLab export
        $cmd = 'unzip -o -d "' . BASE_LAB . $p['path'] . '" ' . $p['file'] . ' *.unl';
        exec($cmd, $o, $rc);
        if ($rc != 0) {
            $output['code'] = 400;
            $output['status'] = 'fail';
            $output['message'] = $GLOBALS['messages'][80079];
            return $output;
        }
        $output['code'] = 200;
        $output['status'] = 'success';
        $output['message'] = $GLOBALS['messages'][80080];
        return $output;
    } else {
        if (strpos($finfo->file($p['file']), 'application/x-gzip') !== False) {
            // iou-web export
            $tmp = tempnam(sys_get_temp_dir(), 'iouweb_');
            $dst = '/opt/unetlab/labs/Imported';
            $cmd = 'gunzip -c ' . $p['file'] . ' > ' . $tmp;
            $default_image = 'L3-TPGEN+ADVENTERPRISEK9-M-12.4-20090714.bin';
            exec($cmd, $o, $rc);
            if ($rc != 0) {
                unlink($tmp);
                $output['code'] = 400;
                $output['status'] = 'fail';
                $output['message'] = $GLOBALS['messages'][80086];
                return $output;
            }
            // Open the iou-web database
            try {
                $db = new PDO('sqlite:' . $tmp);
                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (Exception $e) {
                unlink($tmp);
                $output['code'] = 400;
                $output['status'] = 'fail';
                $output['message'] = $GLOBALS['messages'][80086];
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80086]);
                error_log(date('M d H:i:s ') . (string) $e);
                return $output;
            }
            // Creating import dir if it does not exist
            if (!is_dir($dst) && !mkdir($dst)) {
                unlink($tmp);
                $output['code'] = 400;
                $output['status'] = 'fail';
                $output['message'] = $GLOBALS['messages'][80086];
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80086]);
                error_log(date('M d H:i:s ') . (string) $e);
                return $output;
            }
            // List all iou-web labs
            try {
                $query_labs = 'SELECT lab_id, lab_name, lab_description, lab_info, lab_netmap FROM labs;';
                $statement_labs = $db->prepare($query_labs);
                $statement_labs->execute();
            } catch (Exception $e) {
                unlink($tmp);
                $output['code'] = 400;
                $output['status'] = 'fail';
                $output['message'] = $GLOBALS['messages'][80086];
                error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80086]);
                error_log(date('M d H:i:s ') . (string) $e);
                return $output;
            }
            // Import each lab
            while ($result_labs = $statement_labs->fetch(PDO::FETCH_ASSOC)) {
                //error_log('INFO: found iou-web lab "'.$result_labs['lab_name'].'" with lab_id = '.$result_labs['lab_id'].'.');
                $lab_file = $dst . '/' . $result_labs['lab_name'] . '.unl';
                if (is_file($lab_file)) {
                    error_log('ERROR: skipping lab lab_id = ' . $result_labs['lab_id'] . ', file "' . $lab_file . '" already exists.');
                    //continue;
                    unlink($lab_file);
                }
                // Adding new file
                try {
                    $lab = new Lab($lab_file, 0);
                } catch (Exception $e) {
                    unlink($tmp);
                    $output['code'] = 400;
                    $output['status'] = 'fail';
                    $output['message'] = $GLOBALS['messages'][80086];
                    error_log(date('M d H:i:s ') . 'ERROR: ' . $GLOBALS['messages'][80086]);
                    error_log(date('M d H:i:s ') . (string) $e);
                    return $output;
                }
                //error_log('INFO: importing lab "'.$result_labs['lab_name'].'.');
                // Setting author, description, version
                $p_lab = array('author' => 'Imported from iou-web', 'version' => date('Ymd'));
                if (!empty($result['lab_description']) || !empty($result_labs['lab_info'])) {
                    $p_lab['description'] = trim($result_labs['lab_description'] . ' ' . $result_labs['lab_info']);
                }
                $rc = $lab->edit($p_lab);
                if ($rc !== 0) {
                    error_log('ERROR: failed to set author/description/version while importing lab "' . $result_labs['lab_name'] . '.');
                    error_log($GLOBALS['messages'][$rc]);
                }
                // Adding pictures
                try {
                    $query_pictures = 'SELECT images.img_id, img_name, img_content, img_map FROM images LEFT JOIN rel_img_lab ON images.img_id = rel_img_lab.img_id WHERE rel_img_lab.lab_id = :lab_id';
                    $statement_pictures = $db->prepare($query_pictures);
                    $statement_pictures->bindParam(':lab_id', $result_labs['lab_id'], PDO::PARAM_INT);
                    $statement_pictures->execute();
                    $statement_pictures->execute();
                    while ($result_pictures = $statement_pictures->fetch(PDO::FETCH_ASSOC)) {
                        //error_log('INFO: found iou-web picture img_name = '.$result_pictures['img_name'].' with img_id = '.$result_pictures['img_id'].'.');
                        $p_picture = array('type' => 'image/png', 'data' => $result_pictures['img_content']);
                        if (!empty($result_pictures['img_name'])) {
                            $p_picture['name'] = $result_pictures['img_name'];
                        }
                        if (!empty($result_pictures['img_map'])) {
                            $p_picture['map'] = $result_pictures['img_map'];
                            $p_picture['map'] = preg_replace_callback('/:2*([0-9]+)/', function ($m) {
                                return ':{{NODE$' . (int) $m[1] . '}}';
                            }, $p_picture['map']);
                            //$p_picture['map'] = preg_replace_callback('/coords=\'([0-9]+),([0-9]+),/', function($m) { return 'coords=\''.((int) ($m[1] / 1.78)).','.((int) ($m[2] / 1.78).','); }, $p_picture['map']);
                        }
                        $rc = $lab->addPicture($p_picture);
                        if ($rc !== 0) {
                            error_log('ERROR: skipping picture img_id = ' . $result_pictures['img_id'] . ', error while creating picture.');
                            error_log($GLOBALS['messages'][$rc]);
                        }
                    }
                } catch (Exception $e) {
                    error_log('ERROR: cannot list pictures for iou-web lab "' . $result_labs['lab_name'] . '.');
                    error_log((string) $e);
                }
                // Parsing NETMAP
                $netmap = preg_replace('/(#.*)/', '', $result_labs['lab_netmap']);
                // Remove comments
                $netmap = preg_replace("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n\\']+/", "\n", $netmap);
                // Remove empty lines
                $netmap = preg_replace("/[\\s]+\n/", "\n", $netmap);
                // Remove trailing spaces (trim lines)
                $netmap = trim($netmap);
                // Remove trailing spaces (trim all)
                $network_id = 1;
                $node_id = 1;
                $all_nodes = array();
                // Key is iou-web node_id, value is UNetLab node_id
                $all_ethernets = array();
                // Key is iou-web node_id, value is configured ethernet interfaces
                foreach (explode("\n", $netmap) as $line) {
                    $line = preg_replace('/\\s+/', ' ', $line);
                    // Remove duplicated spaces
                    $elements = explode(' ', $line);
                    $network_added = False;
                    $remote_node = False;
                    foreach ($elements as $element) {
                        $id = (int) substr($element, 0, strpos($element, ':'));
                        $portgroup = (int) substr($element, strpos($element, ':') + 1, strpos($element, '/'));
                        $interface = (int) substr($element, strpos($element, '/') + 1);
                        $interface_id = $portgroup + $interface * 16;
                        //error_log('INFO: found iou-web node '.$id.':'.$portgroup.'/'.$interface.'.');
                        if ($id < 1) {
                            error_log('ERROR: skipping node, invalid iou-web ID.');
                            continue;
                        }
                        if (!isset($all_nodes[$id])) {
                            // New node, need to create
                            // Getting a single node
                            try {
                                $query_nodes = 'SELECT dev_name, bin_name, dev_ram, dev_nvram, dev_ethernet, dev_serial, dev_picture, dev_delay, dev_top, dev_left, cfg_config FROM devices LEFT JOIN configs ON devices.cfg_id = configs.cfg_id WHERE dev_id = :dev_id AND lab_id = :lab_id AND dev_picture != "cloud";';
                                $statement_nodes = $db->prepare($query_nodes);
                                $statement_nodes->bindParam(':dev_id', $id, PDO::PARAM_INT);
                                $statement_nodes->bindParam(':lab_id', $result_labs['lab_id'], PDO::PARAM_INT);
                                $statement_nodes->execute();
                                $result_nodes = $statement_nodes->fetch(PDO::FETCH_ASSOC);
                                $p_node = array('iou-web' => $id, 'id' => $node_id, 'type' => 'iol', 'template' => 'iol', 'name' => $result_nodes['dev_name'], 'image' => $result_nodes['bin_name'], 'nvram' => (int) $result_nodes['dev_nvram'], 'ram' => (int) $result_nodes['dev_ram'], 'ethernet' => (int) $result_nodes['dev_ethernet'], 'serial' => (int) $result_nodes['dev_serial'], 'picture' => $result_nodes['dev_picture'], 'delay' => (int) $result_nodes['dev_delay'], 'top' => 768 * $result_nodes['dev_top'] / 100, 'left' => 1024 * $result_nodes['dev_left'] / 100);
                                if ($p_node['top'] == 0) {
                                    unset($p_node['top']);
                                }
                                if ($p_node['left'] == 0) {
                                    unset($p_node['left']);
                                }
                                // Fixing images
                                if (!is_file('/opt/unetlab/addons/iol/bin/' . $p_node['image'])) {
                                    $p_node['image'] = $default_image;
                                }
                                // Fixing pictures
                                if ($p_node['picture'] == 'desktop') {
                                    $p_node['picture'] = 'Desktop.png';
                                }
                                if ($p_node['picture'] == 'hub') {
                                    $p_node['picture'] = 'HUB.png';
                                }
                                if ($p_node['picture'] == 'mpls') {
                                    $p_node['picture'] = 'MPLS.png';
                                }
                                if ($p_node['picture'] == 'router') {
                                    $p_node['picture'] = 'Router.png';
                                }
                                if ($p_node['picture'] == 'framerelay') {
                                    $p_node['picture'] = 'Frame Relay.png';
                                }
                                if ($p_node['picture'] == 'l3switch') {
                                    $p_node['picture'] = 'Switch L3.png';
                                }
                                if ($p_node['picture'] == 'switch') {
                                    $p_node['picture'] = 'Switch.png';
                                }
                                if ($p_node['picture'] == 'nam') {
                                    $p_node['picture'] = 'Network Analyzer.png';
                                }
                                //error_log('INFO: adding node node_id = '.$node_id.' (name = '.$p_node['name'].', image = '.$p_node['image'].', nvram = '.$p_node['nvram'].', ram = '.$p_node['ram'].', eth = '.$p_node['ethernet'].', ser = '.$p_node['serial'].', picture = '.$p_node['picture'].', delay = '.$p_node['delay'].', top = '.$p_node['top'].', left = '.$p_node['left'].').');
                                //print_r('INFO: adding node node_id = '.$node_id.' (name = '.$p_node['name'].', image = '.$p_node['image'].', nvram = '.$p_node['nvram'].', ram = '.$p_node['ram'].', eth = '.$p_node['ethernet'].', ser = '.$p_node['serial'].', picture = '.$p_node['picture'].', delay = '.$p_node['delay'].', top = '.$p_node['top'].', left = '.$p_node['left'].').');
                                $rc = $lab->addNode($p_node);
                                if ($rc !== 0) {
                                    error_log('ERROR: skipping node node_id = ' . $node_id . ', error while importing.');
                                    error_log($GLOBALS['messages'][$rc]);
                                    continue;
                                }
                                // Adding startup-config
                                if (!empty($result_nodes['cfg_config'])) {
                                    //error_log('INFO: adding config for node_id = '.$node_id.').');
                                }
                                $rc = $lab->setNodeConfigData($node_id, $result_nodes['cfg_config']);
                                if ($rc !== 0) {
                                    error_log('ERROR: skipping config for for node_id = ' . $node_id . ', error while importing.');
                                    error_log($GLOBALS['messages'][$rc]);
                                }
                                $all_nodes[$id] = $node_id;
                                $all_ethernets[$id] = $p_node['ethernet'];
                                $node_id = $node_id + 1;
                            } catch (Exception $e) {
                                error_log('ERROR: skipping node dev_id = ' . $id . ', error while creating node.');
                                error_log((string) $e);
                                continue;
                            }
                        } else {
                            //error_log('INFO: skipping iou-web node '.$id.':'.$portgroup.'/'.$interface.' ('.$interface_id.'), already created.');
                        }
                        // Node is added, need to connect interfaces
                        if ($portgroup <= $all_ethernets[$id] - 1) {
                            // Got an ethernet inteface
                            if ($network_added === False) {
                                // Need to create the network
                                //error_log('INFO: adding network network_id = '.$network_id.').');
                                $p_network = array('type' => 'bridge', 'name' => 'Net' . $network_id);
                                $rc = $lab->addNetwork($p_network);
                                if ($rc != 0) {
                                    error_log('ERROR: skipping network network_id = ' . $network_id . ', error while creating.');
                                    error_log($GLOBALS['messages'][$rc]);
                                    continue;
                                } else {
                                    $network_added = True;
                                }
                            }
                            // Connect the node
                            $p_interface = array($interface_id => $network_id);
                            //error_log('INFO: connecting (ethernet) node '.$id.':'.$portgroup.'/'.$interface.' ('.$all_nodes[$id].':'.$interface_id.') to network '.$network_id.'.');
                            $rc = $lab->connectNode($all_nodes[$id], $p_interface);
                            if ($rc != 0) {
                                error_log('ERROR: skipping interface ' . $portgroup . '/' . $interface . ' (' . $all_nodes[$id] . ':' . $interface_id . ') for node_id = ' . $all_nodes[$id] . ', error while connecting.');
                                error_log($GLOBALS['messages'][$rc]);
                            }
                        } else {
                            // Got a serial interface
                            if ($remote_node === False) {
                                // Fisrt node on this link
                                $remote_node = $all_nodes[$id] . ':' . $interface_id;
                            } else {
                                // Second node on this link, connect and reset the parameter
                                $p_interface = array($interface_id => $remote_node);
                                //error_log('INFO: connecting (serial) node '.$all_nodes[$id].':'.$portgroup.'/'.$interface.' ('.$all_nodes[$id].':'.$interface_id.') to node '.$remote_node.'.');
                                $rc = $lab->connectNode($all_nodes[$id], $p_interface);
                                if ($rc != 0) {
                                    error_log('ERROR: skipping interface ' . $portgroup . '/' . $interface . ' (' . $all_nodes[$id] . ':' . $interface_id . ') for node_id = ' . $all_nodes[$id] . ', error while connecting.');
                                    error_log($GLOBALS['messages'][$rc]);
                                }
                                $remote_node = False;
                            }
                        }
                    }
                    if ($network_added === True) {
                        $network_id = $network_id + 1;
                        $network_added = False;
                    }
                }
                error_log('INFO: lab "' . $result_labs['lab_name'] . '" imported into "' . $lab_file . '".');
            }
            $output['code'] = 200;
            $output['status'] = 'success';
            $output['message'] = $GLOBALS['messages'][80087];
            unlink($tmp);
            return $output;
        } else {
            // File is not a Zip
            $output['code'] = 400;
            $output['status'] = 'fail';
            $output['message'] = $GLOBALS['messages'][80078];
            return $output;
        }
    }
}
Exemplo n.º 5
0
         }
         if ($p_node['picture'] == 'nam') {
             $p_node['picture'] = 'Network Analyzer.png';
         }
         //error_log('INFO: adding node node_id = '.$node_id.' (name = '.$p_node['name'].', image = '.$p_node['image'].', nvram = '.$p_node['nvram'].', ram = '.$p_node['ram'].', eth = '.$p_node['ethernet'].', ser = '.$p_node['serial'].', picture = '.$p_node['picture'].', delay = '.$p_node['delay'].', top = '.$p_node['top'].', left = '.$p_node['left'].').');
         $rc = $lab->addNode($p_node);
         if ($rc !== 0) {
             error_log('ERROR: skipping node node_id = ' . $node_id . ', error while importing.');
             error_log($GLOBALS['messages'][$rc]);
             continue;
         }
         // Adding startup-config
         if (!empty($result_nodes['cfg_config'])) {
             //error_log('INFO: adding config for node_id = '.$node_id.').');
         }
         $rc = $lab->setNodeConfigData($node_id, $result_nodes['cfg_config']);
         if ($rc !== 0) {
             error_log('ERROR: skipping config for for node_id = ' . $node_id . ', error while importing.');
             error_log($GLOBALS['messages'][$rc]);
         }
         $all_nodes[$id] = $node_id;
         $all_ethernets[$id] = $p_node['ethernet'];
         $node_id = $node_id + 1;
     } catch (Exception $e) {
         error_log('ERROR: skipping node dev_id = ' . $id . ', error while creating node.');
         error_log((string) $e);
         continue;
     }
 } else {
     //error_log('INFO: skipping iou-web node '.$id.':'.$portgroup.'/'.$interface.' ('.$interface_id.'), already created.');
 }