Skip to content

Instantly share code, notes, and snippets.

@pirosuke
Created January 19, 2019 17:51
Show Gist options
  • Save pirosuke/959b58eccb63755458d0a262d9ad2624 to your computer and use it in GitHub Desktop.
Save pirosuke/959b58eccb63755458d0a262d9ad2624 to your computer and use it in GitHub Desktop.
Sample To Fetch Data From Remote Database Table Through SSH Tunnel With Node.js
const tunnel = require('tunnel-ssh');
import knexLib = require('knex');
async function main() {
const sshUserName = 'SSH_USER_NAME';
const sshPassword = 'SSH_PASSWORD';
let sshConfig = {
host: 'SSH_HOST_ADDRESS',
port: SSH_PORT,
username: sshUserName,
password: sshPassword,
keepaliveInterval: 60000,
keepAlive: true,
dstHost: 'TUNNEL_DEST_HOST_ADDRESS',
dstPort: TUNNEL_DEST_PORT,
localHost: 'localhost',
localPort: 45432
};
const tnl = tunnel(sshConfig, async (err: any, server: any) => {
if (err) {
throw err;
}
let knex = knexLib({
client: 'postgresql',
connection: {
database: 'DATABASE_NAME',
port: 45432,
user: 'DATABASE_USER',
password: 'DATABASE_PASSWORD'
}
});
const rows = await knex.select().from(TABLE_NAME);
console.log(rows);
await knex.destroy();
tnl.close();
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment