Then I took the natural language interpretation of the code generated by openai and pasted it into the codex javascript sandbox and out popped the following nodejs code. It even includes the asynchronous function to connect to ssh once the host port has been obtained from ngrok api. I could have just generated the code by typing in a few natural language lines describing the function. I forgot to ask it about establishing local forwards and asked it to add the localforwards to ssh -- see below
I have a zmNinja build environment setup and will figure out where to insert this function:
Code: Select all
/*
Can you add the following:
Obtain a list of local port forwards from the config file. The format is port:remote_hostname:remote_port.
Then when opening the ssh connection, establish the localforward ports on localhost
*/
var fs = require('fs');
var ngrok = require('ngrok');
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var apiKey = config.apiKey;
var username = config.username;
var password = config.password;
var port = config.port;
var localForwards = config.localForwards;
ngrok.authtoken(apiKey, function (err, token) {
if (err) {
console.log(err);
} else {
while (true) {
ngrok.connect({
proto: 'tcp',
addr: port,
auth: username + ':' + password
}, function (err, url) {
if (err) {
console.log(err);
} else {
var hostname = url.split(':')[0];
var hostport = url.split(':')[1];
console.log(hostname);
console.log(hostport);
var ssh = require('ssh2').Client();
ssh.on('ready', function () {
console.log('Client :: ready');
ssh.exec('uptime', function (err, stream) {
if (err) throw err;
stream.on('close', function (code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
ssh.end();
}).on('data', function (data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function (data) {
console.log('STDERR: ' + data);
});
});
}).connect({
host: hostname,
port: hostport,
username: username,
password: password,
localForwards: localForwards
});
}
});
sleep(2000);
}
}
});