Adventures of a Hobbyist ~ Part Two
Tags: ssh, node, learning, opensource
Published: 2018-08-09 09:48:07

---
title: Adventures of a Hobbyist ~ Part Two
published: true
description: This week I've been exploring NPM and Node
tags: SSH, NodeJS, Learning, opensource
series: Adventures of a Hobbyist
---

# First week of Learning

![Hello World](https://thepracticaldev.s3.amazonaws.com/i/kq7hye8tmmlrxuve0t12.png)

## What is this?

This is part two of a weekly (ish) series following my journey of learning. I'm hoping to write some software to make my work life easier and thought I'd document my time. If you're interested in reading part one it's here: [Preamble](https://dev.to/link2twenty/adventures-of-a-hobbyist--part-one-2e0n).

## What have you been doing this week?

This was my first week so I installed NPM and Node on my computer, I followed this [guide](http://blog.teamtreehouse.com/install-node-js-npm-windows), though, it was so straightforward I needn't have bothered.

I ran a customary "Hello World" script. It was a refreshingly simple `console.log` that hardly seems worth sharing.

```javascript
console.log("Hello World");
```

## Did you do anything with the main project?

I decided to do some testing to see if SSHing to different servers in order to administer them was a viable option, this is how I had done it in the past.

I still consider this part of the planning stage, I'm a keen believer in looking for failure, the sooner you fail the sooner you can start looking for a solution.

I found a module called [SSH2](https://www.npmjs.com/package/ssh2) that seemed to be quite popular so thought I'd have a look at that. The documentation was clear and I decided to give it a go.

```javascript
// Import ssh2
const SSH_Client = require('ssh2').Client;
const ssh = new SSH_Client();

// Declare private key
const privKey = `
-----BEGIN RSA PRIVATE KEY-----

-----END RSA PRIVATE KEY-----`;

// Event listener for ready state
ssh.on('ready', () => {
  ssh_connected();
});

// Called when connection established and runs command
let ssh_connected = () => {
  ssh.exec('whoami', (err, stream) => {
    if (err) throw err;
    ssh_stream(stream)
  });
}

// Handle data stream returned from server
let ssh_stream = stream => {
  stream.on('close', (code, signal) => {
    ssh.end();
  });
  stream.on('data', data => {
    console.log(data.toString('utf8'));
  });
  stream.stderr.on('data', data => {
    console.log(data.toString('utf8'));
  });
}

// Initiate connection
ssh.connect({
  host: 'server',
  username: 'andrew',
  privateKey: privKey,
});
```

I ran this and, to my delight, my command prompt returned `andrew`, success! If you have anything to say about this method, it may be horrendously wrong for all I know, there is an issue open on [github](https://github.com/ignis-pwa/ideas/issues/8) please leave your thoughts there, thanks.

## What's next?

Next, I plan to look at [greenlock](https://www.npmjs.com/package/greenlock) to get a feel for making web servers with Node, again please tell me if you think I'm going about this wrong ([github issue](https://github.com/ignis-pwa/ideas/issues/9)). Also over the next week, I want to look at MySQL and Node see if I can figure that out.

## This wasn't a week...

I know, I know it's a new project and it's quite exciting, for me, so I'm giving it a lot of time. I want to keep these posts relatively short which means when I have enough to fill a post I will probably give it a go, I have no doubt this will slow down when I get to the more difficult stages of the project.

## Signing off

Thank you for reading this, it really amazes me anyone wants to read my ramblings. It you have someideas of where to take this project or you want to correct something I'm not doing write please feel free to do so. I have a open [repository on github](https://github.com/ignis-pwa/ideas)

Thanks again 🙂
Andrew