Connect ssh server through nodejs code with ssh key and server name - javascript

I have vpn access, so i can ssh to server by following command from terminal
ssh qa-trinath01.my-qa
Its working fine from terminal.
But from nodejs, its not working. my code is
var express = require("express");
var app = express();
var node_ssh = require('node-ssh')
var
ssh = new node_ssh()
ssh.connect({
host: 'qa-trinath01.my-qa',
username: 'tanantham',
port: 27017,
privateKey: '/Users/tanantham/.ssh/id_rsa'
}).then(function() {
console.error('Success: ');
}).catch((error) => {
console.error('ERROR: ', error);
});;
app.listen(3001);
I am getting output as
ERROR: { Error: getaddrinfo ENOTFOUND qa-trinath01.my-qa qa-trinath01.my-qa:27017
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'qa-trinath01.my-qa',
host: 'qa-trinath01.my-qa',
port: 27017,
level: 'client-socket' }
Can some one suggest the nodejs code to connect to ssh, i have only ssh key and ssh server name details.
ssh key -> /Users/tanantham/.ssh/id_rsa
server name -> qa-trinath01.my-qa

Have you tried to replace 'privateKey' with 'password' and enter your password.
It's probably not the most secure but it normally works.
Unless you have to use your private key from id-rsa
ssh = new node_ssh()
ssh.connect({
host: 'qa-trinath01.my-qa',
username: 'tanantham',
port: 27017,
password: 'YourPasswordHere'
}).then(function() {
console.error('Success: ');
}).catch((error) => {
console.error('ERROR: ', error);
});;
app.listen(3001);

Related

Connection between MariaDB and NodeJS not working

I'm using a vServer from Ionos for a little project. I installed a MariaDB and NodeJS. Currently I'm trying to connect them both. Here is my code:
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user:'root',
password: 'xxxxxxxxx',
database: 'christmastrees',
connectionLimit: 5,
});
async function asyncFunction() {
let conn;
try {
conn = await pool.getConnection();
const rows = await conn.query("SELECT * from bäume");
console.log(rows); //[ {val: 1}, meta: ... ]
} catch (err) {
throw err;
} finally {
if (conn) return conn.end();
}
}
But when executing the file (node app), I get an error message after a few seconds
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1157:16)
From event:
at _registerHandshakeCmd (/root/server/node_modules/mariadb/lib/connection.js:745:11)
at /root/server/node_modules/mariadb/lib/connection.js:57:11
at new Promise (<anonymous>)
at Connection.connect (/root/server/node_modules/mariadb/lib/connection.js:56:16)
at createConnectionPoolPromise (/root/server/node_modules/mariadb/lib/pool-promise.js:31:8)
at creationTryout (/root/server/node_modules/mariadb/lib/pool-base.js:373:9)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
So I searched for help and after 2 hours I literally found only one Idea. I added this line of code to my connection...
port: '/var/run/mysqld/mysqld.sock'
I tryed again to use node app, but this time I didn't get an error message, it is loading forever and nothing happens.
I would be so grateful for any help...
So I found the solution to my problem.
I checked if my MariaDB Service is even running and no it wasn't ^^

Trouble making nodejs mysql connection

I am trying to make a simple connection between my nodejs server and my mysql db (using Microsoft SQL server manager studio v14), below is my code and the error message appearing in my console window.
here is my code:
var express = require('express');
var app = express();
var sql = require("mssql");
app.get('/', function (req, res) {
// config for your database
var config = {
user: 'superadmin',
password: '***',
server: 'localhost',
database: 'XXX'
};
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from Schools', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});
The error I am getting looks like this:
Server is running..
tedious deprecated The default value for `options.encrypt` will change from `false` to `true`. Please pass `false` explicitly if you want to retain current behaviour. node_modules\mssql\lib\tedious.js:212:23
{ ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at Connection.tedious.once.err (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:275:13)
at Connection.emit (events.js:182:13)
at Connection.socketError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:1004:14)
at C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:869:25
at SequentialConnectionStrategy.connect (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:154:9)
at Socket.onError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:170:16)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
code: 'ESOCKET',
originalError:
{ ConnectionError: Failed to connect to localhost:1433 - Could not connect (sequence)
at ConnectionError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\errors.js:12:12)
at Connection.socketError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:1004:30)
at C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connection.js:869:25
at SequentialConnectionStrategy.connect (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:154:9)
at Socket.onError (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\tedious\lib\connector.js:170:16)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:174:19)
message: 'Failed to connect to localhost:1433 - Could not connect (sequence)',
code: 'ESOCKET' },
name: 'ConnectionError' }
{ ConnectionError: Connection is closed.
at Request._query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:1299:37)
at Request._query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\tedious.js:497:11)
at Request.query (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:1242:12)
at C:\Users\smr09\Desktop\Code\ou\db_test\test.js:24:17
at _poolCreate.then.catch.err (C:\Users\smr09\Desktop\Code\ou\db_test\node_modules\mssql\lib\base.js:269:7)
at process._tickCallback (internal/process/next_tick.js:178:7) code: 'ECONNCLOSED', name: 'ConnectionError' }
I am rather new at this particularly with dealing with databases. Can someone explain what the error means?
Add this line to the config:
options: {
encrypt: false
}
It will finally look like:
var config = {
user: 'superadmin',
password: '***',
server: 'localhost',
database: 'XXX' ,
options: {
encrypt: false
}
};
try to put encrypt: false in your config{}, it will work.
your error:
ConnectionError: Failed to connect to localhost:1433 - Could not
connect (sequence)
My problem was resolved by adding port
var config = {
user: 'superadmin',
password: '***',
server: 'localhost',
database: 'XXX',
port: '',
};

Cannot send email in nodejs using nodemailer

I used nodemailer and my code as follows:
const nodemailer = require('nodemailer');
module.exports = function(obj) {
return new Promise((resolve, reject) => {
console.log('In root to mail send file...');
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'myemail#gmail.com',
pass: 'my password'
}
});
let mailOptions = {
from: '<myemail#gmail.com>', // sender address
to: obj.email, // list of receivers
subject: obj.subject, // Subject line
text: obj.msg, // plain text body
html: obj.html_msg // html body
};
console.log('sending function');
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log('Error due to send mail' + error);
reject(error);
} else {
console.log('Message %s sent: %s', info.messageId, info.response);
resolve(info);
}
});
});
}
When I run the code, I got this error
{
Error: connect ETIMEDOUT 74.125.200.109:465 at
Object.exports._errnoException (util.js:1022:11) at
exports._exceptionWithHostPort (util.js:1045:20 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
code: 'ECONNECTION',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '74.125.200.109',
port: 465,
command: 'CONN'
}
I don't know why, please help me if anybody know why this error occurred.
I have already set Access for less secure apps: turn on in my gmail account.
You could be behind a network proxy which could be causing a timeout error.

nodemailer google smtp connection closed error

I am trying to send email using nodemailer and nodemailer-smtp-transport in express js. i am creating a custom module to send mails to the user. When ever i am executing the script it is throwing 2 specific errors.
Error 1
D:\Projects\project_name\modules\mailer.js:45
throw error;
^
Error: Connection closed
at SMTPConnection.<anonymous> (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\lib\smtp-transport.js:113:29)
at SMTPConnection.g (events.js:260:16)
at emitNone (events.js:67:13)
at SMTPConnection.emit (events.js:166:7)
at SMTPConnection._destroy (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\lib\smtp-connection.js:558:10)
at SMTPConnection._onEnd (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\lib\smtp-connection.js:538:10)
at Socket.g (events.js:260:16)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:921:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
Error 2
{
[Error: getaddrinfo ENOTFOUND smtp.gmail.com smtp.gmail.com:465]
code: 'ECONNECTION',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'smtp.gmail.com',
host: 'smtp.gmail.com',
port: '465',
command: 'CONN' }
My Complete code is
var nodemailer = require('nodemailer');
var smtpTransport = require("nodemailer-smtp-transport")
var constants = require('./../modules/constants');
/*var smtpTransport = nodemailer.createTransport(constants.MAIL_HOST,{
service : "Gmail",
auth: {
user: constants.MAIL_FROM_AUTH,
pass: constants.MAIL_PASSWORD
}
});*/
var smtpTransport = nodemailer.createTransport(smtpTransport({
host : constants.MAIL_HOST,
secureConnection : constants.MAIL_SECURE,
port: constants.MAIL_PORT,
auth : {
user : constants.MAIL_FROM_AUTH,
pass : constants.MAIL_PASSWORD
},
ignoreTLS: true
}));
var sendMail = function(req, res, template, to, subject, callback) {
var mailOptions = {
from: constants.MAIL_FROM, // sender address
to: to, // list of receivers
subject: subject, // Subject line
text: "random text", // plaintext body
html: template // html body
}
console.log(mailOptions);
console.log(smtpTransport);
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
console.log("Message sent: " + response);
console.log(error);
if(error) {
throw error;
console.log(error);
}
else {
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // shut down the connection pool, no more messages
});
}
exports.sendMail = sendMail;

{ [Error: listen EADDRINUSE :::5000]

What's wrong with the following Server.JS file? We are trying to connect Deployd to Heroku and keep getting the following error. We're using Nitrous, and I have configure the mongoDB port to 5000. Any help is appreciated. Thanks a ton!
Server.JS
// require deployd
var deployd = require('deployd');
// configure database etc.
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: '0.0.0.0',
port: 5000,
name: 'database_name',
credentials: {
username: 'username',
password: 'password'
}
}
});
// heroku requires these settings for sockets to work
server.sockets.server.set('transports', ["xhr-polling"]);
// start the server
server.listen();
// debug
server.on('listening', function() {
console.log("Server is listening on port: " + process.env.PORT);
});
// Deployd requires this
server.on('error', function(err) {
console.error(err);
process.nextTick(function() { // Give the server a chance to return an error
process.exit();
});
});
ERROR:
db:error Error: Cannot open store: MongoError: server 0.0.0.0:5000 timed out
at /home/nitrous/Find-Volunteerships/node_modules/deployd/lib/db.js:144:17
at /home/nitrous/Find-Volunteerships/node_modules/mongodb/lib/mongo_client.js:330:20
at /home/nitrous/Find-Volunteerships/node_modules/mongodb/lib/db.js:231:14
at null.<anonymous> (/home/nitrous/Find-Volunteerships/node_modules/mongodb/lib/server.js:240:9)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at /home/nitrous/Find-Volunteerships/node_modules/mongodb-core/lib/topologies/server.js:493:23
at commandCallback (/home/nitrous/Find-Volunteerships/node_modules/mongodb-core/lib/topologies/server.js:1149:20)
at Callbacks.flushConnection (/home/nitrous/Find-Volunteerships/node_modules/mongodb-core/lib/topologies/server.js:103:9)
at null.<anonymous> (/home/nitrous/Find-Volunteerships/node_modules/mongodb-core/lib/topologies/server.js:408:24)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at null.<anonymous> (/home/nitrous/Find- Volunteerships/node_modules/mongodb-core/lib/connection/pool.js:144:10)
at g (events.js:260:16)
at emitTwo (events.js:87:13) +0ms
session:error Error removing old sessions: Database connection error +14ms
Unhandled rejection Database connection error
{ [Error: listen EADDRINUSE :::5000]
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 5000 }
You are using same port for two servers.
You can kill one of them with that command
netstat -ano|findstr "PID :5000"
5000 is your port.

Categories

Resources