# Ext script test into console

**URL:** <https://forum.genieacs.com/t/ext-script-test-into-console/1788>\
**Category:** Uncategorized\
**Created:** [June 14, 2021, 5:45pm UTC](https://forum.genieacs.com/t/ext-script-test-into-console/1788 "2021-06-14T17:45:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fernan06](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/fernan06/32/598_2.png) [@fernan06](https://forum.genieacs.com/u/fernan06)\
**Post date:** [June 14, 2021, 5:45pm UTC](https://forum.genieacs.com/t/ext-script-test-into-console/1788/1 "2021-06-14T17:45:20Z")

</div>

Dear all,

How to test the ext script into console ?

Many thanks

Laurent

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [June 15, 2021, 1:46am UTC](https://forum.genieacs.com/t/ext-script-test-into-console/1788/2 "2021-06-15T01:46:42Z")

</div>

Copy this script to `genieacs/config/ext/ext-runner.js`

Then from your genieacs dir run `node config/ext/ext-runner.js`. It will then list out your scripts and the exported functions, simliar to

> $ node config/ext/ext-runner.js  
> Must provide name of script and command to run.  
> Usage: node config/ext/ext-runner.js SCRIPT\_NAME FUNCTION  
> Script: Function:  
> cpe-config portForwards, staticReservations, resetPppoe, getConfig  
> events send, diagnostic  
> network ip

And to execute a script you would do: `node config/ext/ext-runner.js cpe-config getConfig`. That will run the getConfig func in the cpe-config script using the block of parameters in `let args = ...`

-dan

```auto
"use strict";

const process = require('process');
const path = require('path');
const fs = require('fs');

const serial = 'some-serial';
const oui = '3c9066';
const productClass = '963168_OT142C_B';

let args = {
    serial: serial,
    oui: oui,
    productClass: productClass,
    deviceId: 'some-acs-id'
};

let scriptName = process.argv[2];
let cmd = process.argv[3];
let dir = path.dirname(__filename);
let runner = path.basename(__filename);
let cmdIndex = -1;

let script = scriptName ? loadScript(scriptName) : null;
if (script) {
    cmdIndex = Object.keys(script).indexOf(cmd);
}

if (cmd === undefined || script === null || cmdIndex === -1) {
    if (cmd !== undefined) {
        console.error('Invalid option: \t' + cmd);
    } else {
        console.error('Must provide name of script and command to run.');
    }

    showHelp();
    process.exit(255);
}

console.log('Running script ' + cmd);

script[cmd]([JSON.stringify(args)], function (e, result) {
    if (e) {
        console.error(e);
    }

    if (result) {
        console.log(result);
    }
});

function loadScript(name) {
    if (name.indexOf('.js') === -1) {
        name += ".js"
    }

    let fullPath = dir + '/' + name;
    if (!fs.existsSync(fullPath)) {
        console.error(fullPath + ' was not found');
        return;
    }

    return require(fullPath);
}

function showHelp() {
    console.log('Usage: node config/ext/' + runner + ' SCRIPT_NAME FUNCTION');

    var files = fs.readdirSync(dir);

    console.log(' ' + padRight("Script:", 20) + "Function:");

    for (var i = 0; i < files.length; i++) {
        if (files[i] === runner) {
            continue;
        }

        let scriptName = files[i].replace(/\.js/, '');
        let script = require(dir + '/' + files[i]);
        console.log(' ' + padRight(scriptName, 20) + Object.keys(script).join(', '));
    }
}

function padRight(value, length) {
    return (value.toString().length < length) ? padRight(value + ' ', length) : value;
}

function padLeft(value, length) {
    return (value.toString().length < length) ? padLeft(' ' + value, length) : value;
}

```

---

<div class="post-metadata">

**Author:** ![fernan06](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/fernan06/32/598_2.png) [@fernan06](https://forum.genieacs.com/u/fernan06)\
**Post date:** [June 15, 2021, 8:00am UTC](https://forum.genieacs.com/t/ext-script-test-into-console/1788/3 "2021-06-15T08:00:47Z")

</div>

Hello,

Perfect !!!  
Many thanks Dan for your help.

Have a good day

Laurent
