How to get names of devices in your local network using node js - node.js

I need to get HueBridge IP address in my app in node js, I would like to scan the whole network and then search for the name "Hue Bridge" or something similar, I found npm package named 'local-devices', but it doesn't return a device name for me for some reason.
const find = require('local-devices');
find().then(devices => {
console.log(devices)
})
Output:
[
{ name: '?', ip: '192.168.1.72', mac: 'secret_mac'},
{ name: '?', ip: '192.168.1.116', mac: 'secret_mac'},
{ name: '?', ip: '192.168.1.117', mac: 'secret_mac'}
]

Related

How to find and replace string using groovy script

I need to search some text from file and replace with other string using Groovy script. I am explaining my file below.
test.yml:
devices:
test-server:
type: test1
os: test
tacacs:
username: admin
passwords:
tacacs: admin
connections:
defaults:
class: unicon.Unicon
cli:
protocol: ssh
ip: 1.1.1.1
port: 2024
rest:
protocol: http
ip: 1.1.1.1
port: 8080
username: admin
password: admin
RFS1:
type: test
os: test
tacacs:
username: admin
passwords:
tacacs: admin
connections:
defaults:
class: unicon.Unicon
cli:
protocol: ssh
ip: 1.1.1.1
port: 2024
rest:
protocol: http
ip: 4.4.4.4
port: 8080
username: admin
password: admin
RFS2:
type: test
os: test
tacacs:
username: admin
passwords:
tacacs: admin
connections:
defaults:
class: unicon.Unicon
cli:
protocol: ssh
ip: 1.1.1.1
port: 2024
rest:
protocol: http
ip: 6.6.6.6
port: 8080
username: admin
password: admin
Here I need to search the IP which is under devices:/test-server:/connections:/cli:/ip: 1.1.1.1 with some new charcter like ip:10.10.10.10 using groovy script. I am using below code.
def myFile = new File("test.yml")
def fileText = myFile.text
fileText = (fileText =~ /ip:1.1.1.1/).replaceFirst("ip:10.10.10.10")
myFile.write(fileText)
Here my issue is its replacing the required string in whole file where ip:1.1.1.1 is present but I need to replace under devices:/test-server:/connections:/cli:/ip: 1.1.1.1. Please help me to resolve this issue.
A better way to do this is to simply do YAML parsing, manipulating the object, and saving back to the file.
Here's an example using Jackson:
#Grab(group='com.fasterxml.jackson.dataformat',
module='jackson-dataformat-yaml',
version='2.12.2')
def myFile = new File("test.yml")
def om = new com.fasterxml.jackson.databind.ObjectMapper(
new com.fasterxml.jackson.dataformat.yaml.YAMLFactory());
def value = om.readValue(myFile, Map)
value['devices']['test-server']['connections']['cli']['ip'] = '10.10.10.10'
That replaces the value in the in-memory object. You can then just save that back to a file, with something like:
om.writeValue(myFile, value)

How do get ILPIP (PIP) from within a Node.JS application running ona VM in Azure?

I need to get the DNS name (ILPIP DNS name) in a Node.JS application (an IoT gateway) that is running on a VM in Azure.
Background details.
I need this so the application can inform the web frontend where to open the socket.io connection to when the web based client wish to communicate with the IoT gateway.
I have been looking in Microsofts Azure modules for Node.JS but I haven't found anything that gives the ILPIP (assigned dns name)
You can try to use RoleEnvironment.getCurrentRoleInstance() function in Azure SDK for Node.js, run the following code snippet in a classic VM:
var azure = require('azure');
azure.RoleEnvironment.getCurrentRoleInstance(function (error, instance) {
if (!error && instance['endpoints']) {
//You can get information about "endpoint1" such as its address and port via
console.log(instance)
} else {
console.log(error);
}
});
You may get the following similar info of role instance:
{ id: 'WorkerRole1_IN_0',
roleName: 'WorkerRole1',
faultDomain: '0',
updateDomain: '0',
endpoints:
{ 'Microsoft.WindowsAzure.Plugins.RemoteAccess.Rdp':
{ name: 'Microsoft.WindowsAzure.Plugins.RemoteAccess.Rdp',
address: '100.104.92.19',
port: '3389',
publicPort: '0',
protocol: 'tcp',
roleInstanceId: 'WorkerRole1_IN_0' },
'Microsoft.WindowsAzure.Plugins.RemoteForwarder.RdpInput':
{ name: 'Microsoft.WindowsAzure.Plugins.RemoteForwarder.RdpInput',
address: '100.104.92.19',
port: '20000',
publicPort: '3389',
protocol: 'tcp',
roleInstanceId: 'WorkerRole1_IN_0' }
}
}

Can't install custom SailsJS adapter

Every time I try to use an adapter other than sails-disk I'll get a No find() method defined in adapter! Error.
My sails app will lift fine, but when ever I get to a point where the adapter is being called it fails. I've tried several different adapters and they all fail.
I'm using the boilerplate config files.
I had this problem too and only happened with custom adapters. So instead of using the path api/adapters, i moved the adapter to node_modules where others adapters such as sails-mongo are installed, and it did the trick.
usually, you just need to enable your new adapter in yourapp/config/adapters.js
For exemple, with mongo :
add sails-mongo dependence in your package.json file
"dependencies": {
...
"sails-mongo": "~0.9.6"
}
run $ npm install
change your config file (ie: yourapp/config/adapters.js) by editing :
module.exports.adapters = {
'default': 'mongo',
mongo: {
module: 'sails-mongo',
host: 'localhost',
user: '',
password: '',
database: 'your-prod-db'
}
};
Of course for local dev, edit yourapp/config/local.js like:
adapters : {
'default': 'mongo',
mongo: {
module: 'sails-mongo',
host: 'localhost',
user: '',
password: '',
database: 'your-local-db'
}
}

Inexplicable node.js http throwing connect ECONNREFUSED (IPv6?)

I am running node.js as follows:
> http = require('http')
> http.get('http://myhost.local:8080',
function (res) { console.log("RES" + res) }
).on('error', function (e) { console.log("Error:", e) })
> uri = require('url').parse("http://myhost.local:8080")
{ protocol: 'http:',
slashes: true,
auth: null,
host: 'myhost.local:8080',
port: '8080',
hostname: 'myhost.local',
hash: null,
search: null,
query: null,
pathname: '/',
path: '/',
href: 'http://myhost.local:8080/' }
> http.get(uri,
function (res) { console.log("RES" + res) }
).on('error', function (e) { console.log("Error:", e) })
An error is thrown for both the implicit and explicitly parsed URI and I get the following output for both:
Error: { [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
The host myhost.local is an alias for localhost in /etc/hosts, being:
127.0.0.1 localhost myhost.local myhost
255.255.255.255 broadcasthost
::1 localhost myhost.local myhost
fe80::1%lo0 localhost myhost.local myhost
EDIT: I tried virtually every permutation for the hosts file, including the most obvious:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost myhost.local myhost
fe80::1%lo0 localhost
EDIT I should also mention that I have tried this on more than one Mac now.
Although it seems this is a rather common error, I have seen no useful explanations or workarounds. Here are some notable related facts:
Running $ wget http://myhost.local:8080 works as expected, so it isn't a firewall problem.
Running $ telnet myhost.local 8080 and then manually GET'ing the url works fine, so it's not a weird HTTP problem.
I have no trouble using node.js to connect to other hosts e.g. http://www.google.com
I expect the useful system information would include:
$ node -v
v0.9.11
$ uname -a
Darwin myhost.local 12.2.1 Darwin Kernel Version 12.2.1:
Thu Oct 18 12:13:47 PDT 2012; root:xnu-2050.20.9~1/RELEASE_X86_64 x86_64
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.8.2
BuildVersion: 12C3104
$ sudo netstat -nalt | grep LISTEN | grep 8080
tcp6 0 0 ::1.8080 *.* LISTEN
Does anyone have any idea what is going on here, and what a fix might be?
I'm going to post this here in case somebody else has the problem.
Bert Belder, Node.js mailing list:
On your system "myhost.local" resolves to three different addresses
(127.0.0.1, ::1, and fe80::1). Node prefers ipv4 over ipv6 so it'll
try to connect to 127.0.0.1. Nothing is listening on 127.0.0.1:8080 so
the connect() syscall fails with ECONNREFUSED. Node doesn't retry with
any of the other resolved IPs - it just reports the error to you. A
simple solution would be to replace 'localhost' by the intended
destination ip address, '::1'.
Whether this behavior is right is somewhat open for debate, but this
is what causes it.
Bert
This stemmed from an issue with Node (though there are ways to work around it), as per the discussion on nodejs/Google Groups, as #alessioalex alluded in his answer. A useful comment per Bert Belder:
there should be a getaddrinfo wrapper that returns more that just the first result
For example,
> require('dns').lookup('myhost.local', console.log)
{ oncomplete: [Function: onanswer] }
> null '127.0.0.1' 4
This is the first of multiple getaddrinfo results passed to Node. It seems that nodejs only uses the first item of the getaddrinfo call. Bert and Ben Noordhuis agreed in the Groups discussion that there should be a way to return more than just the first result with the getaddrinfo wrapper.
Contrast python, which returns all results from getaddrinfo:
>>> import socket
>>> socket.getaddrinfo("myhost.local", 8080)
[(30, 2, 17, '', ('::1', 8080, 0, 0)),
(30, 1, 6, '', ('::1', 8080, 0, 0)),
(2, 2, 17, '', ('127.0.0.1', 8080)),
(2, 1, 6, '', ('127.0.0.1', 8080)),
(30, 2, 17, '', ('fe80::1%lo0', 8080, 0, 1)),
(30, 1, 6, '', ('fe80::1%lo0', 8080, 0, 1))]
does this work?
var http = require('http');
var options = {
host: 'myhost.local',
port: 8080,
path: '/'
};
http.get(options, function (res) {
console.log("RES" + res)
}).on('error', function (e) {
console.log("Error:", e)
});

Regarding the error occurred in my test case in nodeload.js script

I have designed an app using elastic search. And when Iam trying to write the test case using node load.js. I have got a problem that when I increase the number users I was getting the warning that "WARN: Error during HTTP request: Error: ECONNREFUSED, Could not contact DNS servers" and Iam unable rectify the problem so please help me in solving this error.
nl.run({
name: "test",
host: 'localhost',
port: 9200,
//path: '/my_river/page/_search?q=sweden',
numUsers: 2000, //Increased my num of user**
timeLimit: 180,
targetRps: 500,
stats: [
'result-codes',
{ name: 'latency', percentiles: [0.9, 0.99] },
'concurrency',
'rps',
'uniques',
{ name: 'http-errors', successCodes: [200,404], log: 'http-errors.log' }
],

Resources