DEV Community

Andrew Bone
Andrew Bone

Posted on • Updated on

Adventures of a Hobbyist ~ Part Two

First week of Learning

Hello World

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.

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, 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.

console.log("Hello World");
Enter fullscreen mode Exit fullscreen mode

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 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.

// 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,
});
Enter fullscreen mode Exit fullscreen mode

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 please leave your thoughts there, thanks.

What's next?

Next, I plan to look at 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). 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

Thanks again 🙂
Andrew

Top comments (0)