On one hand, I have an web server which has its own IP and subnet. The port which is used is 3000.
On the other hand, I have a Raspberry PI that I'd like to connect directly to the server by Ethernet (Direct connection).
What I'd like to do is to identify the subnet and the IP of the server to connect the Rpi through a NodeJS code.
For what I understand, I need to scan the whole subnets and then scan the IPs inside those subnets.
I have found the following libraries to scan networks:
- https://github.com/goliatone/arpscan
- https://github.com/skepticfx/arpjs
- https://www.npmjs.com/package/node-port-scanner
The following code would only scan the current subnet:
const arpScanner = require('arpscan/promise');
const nodePortScanner = require('node-port-scanner');
arpScanner({interface : 'eth0'})
.then(onResult)
.catch(onError);
function onResult(data) {
console.log(data);
data.forEach(server => {
nodePortScanner(server.ip, [3000])
.then(results => {
console.log(results);
})
.catch(error => {
console.log(error);
});
});
}
function onError(err) {
throw err;
}
How could I scan all available subnets ?
Once the subnet and the target IP/port has been identified, I would change the subnet of the Raspberry and connect it to the server:
const exec = require('child_process').exec;
const isPortReachable = require('is-port-reachable');
function execute(command, callback) {
exec(command, function(error, stdout, stderr){ callback(stdout); });
}
// Change subnet
execute('ifconfig eth0 netmask ' + subnet, config => {
console.log(config);
console.log(await isPortReachable(3000, {host: IP_FOUND_PREVIOUSLY}));
});
Would that work ?