My requirement is to connect to telnet server from client side and run commands on server machine using nodejs.
Here is the code i am using:
const net = require("net");
const cp = require("child_process");
net.connect({host: 192.168.192.136, port:23}, function() {
console.log("connected");
cp.exec('pwd', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
});
With this i am able to connect to server but when i run command using cp.exec it is running on local machine not on connected server.
1) how to run that command on server after connection?
2) Why connection is established to server without username or password. although when i try to connect to it through terminal it asks for username and password.
I also tried with some node js modules from npmjs but didn't get success.
Any kind of help will be appreciated. Thanks.
Use remote-exce module to do the same.
Here link to documentation:-
https://www.npmjs.com/package/remote-exec
Then Use telnet client module
Link to documentation
https://www.npmjs.com/package/telnet-client
On Nodejs v15 I don't think you need exec for that. I've managed to establish a telnet connection via the following method:
const net = require("net");
/*
* establish new client connection to the server
*/
let client = net.connect({
host: "192.168.192.136",
port: 23,
}, ()=> {
console.log("connected");
client.write("TELNET COMMAND HERE", ()=>{
console.log("Command sent!")
})
});
Tutorial reference: https://nodejs.org/api/net.html#net_net_createconnection_options_connectlistener
Related
I am trying to connect to a redis instance in aws. I can connect to it using something like
redis-cli -h localhost -p 6379 -a <auth_token> --tls PING
However when I try this using node (redis library v4.2.0) doing something like this, it hangs
const redis = require("redis");
(async () => {
const client = redis.createClient( {
auth_pass:
"<auth_token>",
tls: { servername: "localhost", port: 6379 },
});
client.on("error", (err) => {
console.log("Redis Client Error", err);
});
client.connect();
console.log(await client.ping());
})();
Portforwarding is setup for redis in aws, which is why localhost is used.
The auth token is the same token I entered to the sparkleformation when redis was configured. both resting and transit encryption has been configured as well.
I have been trying to poke around on google for an answer, however there seem to be a lot of old documentation out there and none of the new ones are clear as to how to get a connection working using tls and an auth token. Any idea how to get this working?
If anybody is running into the same issue, I was able to get it working using ioredis instead.
const Redis = require("ioredis");
(async () => {
const redisRef = new Redis("rediss://:<auth_token>#localhost:6379");
console.log(await redisRef.ping());
})();
and setting this environment variable when running locally:
export NODE_TLS_REJECT_UNAUTHORIZED=0
const redis = require("redis");
let client = redis.createClient({
host: process.env.host,
port: process.env.port,
password:process.env.password
});
(async () => {
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();
console.log("connected to redis")
})();
I have added redis-heroku addon to my project, Now I am trying to access it from my code but its giving me this error: "AuthError: ERR Client sent AUTH, but no password is set".
Also when I am trying to connect from terminal, I am able to connect to it but when I type any redis command , I get this "Error: Connection reset by peer".
If I am using this on my localsystem and local redis server its working fine
it will be helpful if anyone can provide me a working code of heroku redis, I think redis has two urls: REDIS_URL, REDIS_TLS_URL. The problem might be arising because of this tls(more secure)
Kinldy help me
Thanks
Heroku redis does not expose a host, port, and password variables. Instead they expose a REDIS_URL that contains all of those things in one string.
I believe you need to call createClient like this...
createClient({
url: process.env.REDIS_URL
});
In node-redis v4 the host and port should be inside a socket object, not directly on the main config object (see https://github.com/redis/node-redis/blob/master/docs/client-configuration.md):
const client = redis.createClient({
socket: {
host: process.env.host,
port: process.env.port
},
password: process.env.password
});
I've been trying to connect my node app to my remote Redis Labs server. I have the endpoint which from what I can discover, is my host and port (host.com:port). I've been trying to connect to the cloud server by using
const redis = require('redis');
const client = redis
.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST)
.on('error', err => console.error('FUCK', err));
client.on('connect', function(err, res) {
console.log('redis is connected!');
});
but I continue to get an error. "Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379"
I'm sure this is a simple fix, but I just don't understand how to get it to work. Any help would be much appreciated!
Figured it out. I knew it was a simple fix. Just had to call client.auth(process.env.REDIS_PASSWORD)
const client = redis.createClient(
process.env.REDIS_PORT,
process.env.REDIS_HOST
);
client.auth(process.env.REDIS_PASSWORD);
I want to connect my apps nodejs using node-posgres to PostgreSQL.
My apps in localhost (ubuntu) and my postgresql in virtual machine in the cloud with operating system OpenSuse.
This is my code :
var express = require('express');
var app = express();
var http = require('http');
var pg = require('pg');
var conString = "postgres://postgres:mypassword#myurl.com:5432/postgres";
app.get('/', function (req, res) {
res.send('HOLAAAA');
});
// respond with "SERVER" on the homepage
app.get('/server', function (req, res) {
var client = new pg.Client(conString);
client.connect(function (err) {
if (err) {
return console.error('could not connect to postgres', err);
}
console.log('CONNECT PASSED');
client.query('SELECT * FROM visit', function (err, result) {
if (err) {
return console.error('error running query', err);
}
console.log('QUERY PASSED');
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
});
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
But i got an error like this:
could not connect to postgres { [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
Please help me how to solve this.
Thanks!
#Madhavan Kumar thank you very much for your help
the steps to resolve this were as follows:
On the remote server:-
1- find \ -name "postgresql.conf" to find place of config file
2- sudo nano /path/to/config/postgresql.conf to edit config file
3- change this #listen_addresses = 'localhost' to this listen_addresses = '*' then save and exit
4- find \ -name "pg_hba.conf" to find hba config file
5- sudo nano /path/to/config/pg_hba.conf to edit hba config file
6- add
host all all 0.0.0.0/0 md5
host all all ::/0 md5
at the end of the file, then save and exit
7- run /etc/init.d/postgresql restart to restart postgres
In the code connect like this:-
let sequelize = new Sequelize(
config.db.name,
config.db.username,
config.db.password,
{
host: config.ip,
port: config.port,
dialect : 'postgres'
}
)
First try a simple psql command from the local end,
psql -d DBNAME -h YOUR_IP -U USERNAME
it mayn't work for you. This can be because of two reasons, the VM's ip is not resolved by your local station. (or) is the VM in public cloud like amazon (or) your desktop. If it is in public group, the port 5432 on the VM is not open to the public world. You need to write a security group to do it.
If you are sure, it is not any one of the above two issues, better visit postgres /etc/postgresql/9.3/main/pg_hba.conf and see if remote connections are enabled. NodeJs app, you must test at the end.
I am trying to connect using the following code.
I am running the nodejs app locally and I cannot connect to the MongoLab DB using the MongoDb Native Driver.
After about 30sec the error returned says:
{"name":"MongoError","message":"no valid seed servers in list"}
Should I declare something else somewhere? What am I missing?
When I run it live on Heroku it connects just fine.
Also, when I run it from the terminal (shell) it connects just fine.
var MongoClient = require('mongodb').MongoClient;
var util = require('util');
var assert = require('assert');
var auth = {
user: 'root',
pass: 'blabla',
host: 'blabla.mongolab.com',
port: 63879,
name: 'heroku_blabla'
};
// Connection URL
var url = util.format('mongodb://%s:%s#%s:%d/%s',
auth.user, auth.pass, auth.host, auth.port, auth.name);
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
db.close();
});
I solved it, but I don't know what caused the problem.
I suspected that it was a DNS problem.
So I went to the terminal and first run:
ping ds00000.mongolab.com
(change 00000 to the port of your MongoLab DB)
to check the host.
Then I run: nc -w 3 -v ds000000.mongolab.com 000000
Finally, if those two succeed then it is not a problem with the port.
So, then I changed the host name in my code with the IP of that host name in mongoLab, and that solved the error that appeared.
Note: The IP of the mongoLab DB host appears in the response of second command in your terminal.