gns3-web-ui/console-executor.js

65 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2019-03-05 13:26:09 +00:00
const { spawn } = require('child_process');
2019-04-12 10:16:46 +00:00
const { app } = require('electron');
const path = require('path');
async function setPATHEnv() {
const puttyLookup = [
path.join(__dirname, 'dist', 'putty'),
path.join(path.dirname(app.getPath('exe')), 'dist', 'putty')
];
// prevent adding duplicates
let extra = [
...puttyLookup,
].filter((dir) => {
return process.env.PATH.indexOf(dir) < 0;
});
extra.push(process.env.PATH);
process.env.PATH = extra.join(";");
}
2019-03-05 13:26:09 +00:00
exports.openConsole = async (consoleRequest) => {
2019-04-12 10:16:46 +00:00
// const genericConsoleCommand = 'xfce4-terminal --tab -T "%d" -e "telnet %h %p"';
const genericConsoleCommand = 'putty.exe -telnet %h %p -loghost "%d"';
2019-03-05 13:26:09 +00:00
const command = prepareCommand(genericConsoleCommand, consoleRequest);
2019-04-12 10:16:46 +00:00
console.log(`Setting up PATH`);
await setPATHEnv();
2019-03-05 13:26:09 +00:00
console.log(`Starting console with command: '${command}'`);
let consoleProcess = spawn(command, [], {
shell :true
});
consoleProcess.stdout.on('data', (data) => {
2019-04-12 10:16:46 +00:00
console.log(`Console stdout is producing: ${data.toString()}`);
});
consoleProcess.stderr.on('data', (data) => {
console.log(`Console stderr is producing: ${data.toString()}`);
});
consoleProcess.on('close', (code) => {
console.log(`child process exited with code ${code}`);
2019-03-05 13:26:09 +00:00
});
}
function prepareCommand(consoleCommand, consoleRequest) {
const mapping = {
h: consoleRequest.host,
p: consoleRequest.port,
d: consoleRequest.name,
i: consoleRequest.project_id,
n: consoleRequest.node_id,
c: consoleRequest.server_url
};
for(var key in mapping) {
const regExp = new RegExp(`%${key}`, 'g');
consoleCommand = consoleCommand.replace(regExp, mapping[key]);
}
return consoleCommand;
}