Old Guy New Trick

An old guys journey to learn how to code.

NetCat Part Deux - The Writing on the Wall

Author: John on January 12, 2017

SUMMARY:  TUse NetCat to help troubleshoot a TCP Port issue on a Linux VM (Guest) and our Laptop (OS X) which is a Host running VirtualBox.

My node (laptop) on the network cannot talk to a new application running on a linux server.  Ever found yourself in this scenario?  To add a layer of complexity, that application running on Linux server is a VirtualBox VM (virtual machine.)  The VM was created with the network settings set to 'Nat'.  What does that mean?  It means that the VM can use the host computers network connection to get on the network, and out to the Internet - assuming your laptop/desktop has a working Internet connection.  

But what if I want to connect to that VM (guest) using ssh from my laptop (host)?  We can do this with VirtualBox, but you need to update your Network settings in VirtualBox and enable port forwarding.  For example, you may add a port forwarding entry as follows:

Name   Protocol  Host IP     Host Port   Guest IP   Guest Port
ssh    TCP   2222                   22

Once the above is setup and you start your VM, from your laptop (host) you should be able to connect using:  ssh username@ -p 2222

VirtualBox will forward your ssh connection from the laptop (host) on port 2222 on to the Linux server (guest) on port 22.  Normally, this setup works and you shouldn't encounter any issues.  However, if say you just installed a shiney new app on the Linux server (guest) that should run on ports 7001 and 7003, but you find that you have issues connecting from your laptop (host) then it would be time to roll up your sleeves and trouble shoot.

This is where NectCat (nc) can be helpful.  Remember, in this scenario we have our laptop (host) using VirtualBox to handle port forwarding to your Linux server (guest.)  If for example you try the following from your laptop, you may get a false-positive response:

jhogarty: ~ $ nc -v -z localhost 9001
found 0 associations
found 1 connections:
    1:    flags=82
   outif lo0
   src port 58563
   dst port 9001
   rank info not available
   TCP aux info available

Connection to localhost port 9001 [tcp/etlservicemgr] succeeded!
jhogarty: ~ $

In the above example I am using port 9001, an unprotected port, on my laptop (host) to try and connect to the Linux server (guest) on port 7001.  From the output in the above example, using netcat, all looks well.  But it isn't.  We need to modify the use of our tools to do a better test given our current scenario.

On the Linux server (guest) we will use NetCat with the following arguments:

nc -l 7001
# Example:
jhogarty: ~ $ ssh oracle@ -p 2223
oracle@'s password:
Last login: Fri Jan 13 09:38:13 2017 from
[oracle@localhost ~]$ nc -l 7001

To help with our testing, you should have two terminal sessions up - one for your Linux server and one for your laptop.  We set up the Linux server in one terminal with the example above and now we need to use NetCat in a different terminal session on your laptop.  Use the following:

nc 9001
# Example
jhogarty: ~ $ nc 9001

Hmm, don't see much going yet.  But here comes the cool part, assuming that we have good network connectivity between the server and the laptop.  In the terminal window for your laptop, start typing something - 'Hey, is this working?'   Press enter.  Take a look in the other terminal window, the one connected to your Linux server.  Do you see the text that you typed?  If so, you have a clear networking path between the two computers (nodes.)  Congratulations!

# Example
jhogarty: ~ $ nc 9001
Hey, is this working?   <- you just typed this!

And on the Linux Server you should see:

[oracle@localhost ~]$ nc -l 7001
Hey, is this working?     <- you should see this appear after typing on LAPTOP and pressing Enter

If you do not see the text on your Linux server, check to make sure you typed the commands correctly based on the examples given above.  If you are working in a similar scenario, using VirtualBox, make sure you have setup your port forwarding properly.  For example, for the Host Port setting, do not use any protected ports.  Another thing you can check, if using VirtualBox, are you using NAT?

Should you be troubleshooting a similar problem, but without something like VirtualBox, but instead your laptop/desktop/server-a is on a network that can normally talk to the target server, for example ssh works fine, then you may have a firewall or port blocking issue.  And that would be a topic for another day!

Learn Something New Everyday


OMG Netcat Can Do That?!!!

Author: John on December 14, 2016

SUMMARY:  Today we will see yet another cool thing netcat can do - file transfer.

I could be King if I surrounded myself by fools.  But I'd rather immerse myself in a group of people smarter than me so I can learn and grow.  I believe that I am pretty comfortable and good at the command line - far from a guru, but I've been known to impress.  Today however, I saw something my co-worker, Glenn Struzinski, did that just blew my mind.  

In my previous post one of the tips I covered included using netcat (nc).  When I saw that tip I thought I had learned something really cool about Netcat.  But what I will share with you today I think is a mind-bender of a tip - using netcat to transfer a file from Host-A to Host-B.  These hosts need to be able to communicate over the network.  My example will have Host-A as my server hosted at Digital Ocean, and Host-B is my laptop.  Currently my laptop is connected to the internet at a Starbucks cafe.

For illustrative purposes, let's say Host-A has an ip address of  (I am pulling this address out of the air, please use your own real address.)  While logged in to Host-A I enter the following:

➜  ~ cat BinaryIpsum.zip | nc -l -p 1234

Now you may have noticed that in the above terminal, the server is waiting for a connection to occur.  Let's review the command.  We have a file on our server called BinaryIpsum.zip.  Replace that with your desired file name.  Now we use cat and our file name and then we pipe that output to netcat (nc) and tell netcat to "listen" using the -l flag.  Lastly, we tell netcat to use port 1234 with the -p 1234.

So now we need to open up a terminal session on our laptop.  Navigate to a directory where you want to place the file and execute the following:

➜  NETCAT_TEST ls -la
total 0
drwxr-xr-x    2 jfhogarty  staff    68 Dec 13 19:49 .
drwx------+ 176 jfhogarty  staff  5984 Dec 13 19:49 ..
➜  NETCAT_TEST nc 1234 > NewFile.zip
➜  NETCAT_TEST ls -la
total 10632
drwxr-xr-x    3 jfhogarty  staff      102 Dec 13 19:49 .
drwx------+ 176 jfhogarty  staff     5984 Dec 13 19:49 ..
-rw-r--r--    1 jfhogarty  staff  5440660 Dec 13 19:49 NewFile.zip


Let's review what we did on our destination computer.  We used nc with the ip address of the host that has the file we want.  We redirected the output of that command using the greater than sign (>) which directed the output to the file named NewFile.zip.  I could have used the name BinaryIpsum.zip or something else if I had wanted.

If you do some pretty wild stuff with Netcat, let me know - I love learning new things, every day!


Learn Something New Everyday

Fist Full of Tips

Author: John on November 30, 2016

SUMMARY:  Today I will cover a few tips that I picked up recently from my new job and noodling around a bit.  We will re-visit ngrok, explore iTerm, use netcat (nc) and brush up against bundler.


It has been an exciting year for me, chuck full of turberlance and change.  But things have settled down and I find myself once again pledging to be more timely with my blog posts.  I was listening to a  podcast the other day and the guest was talking about how his goal for 2017 is to write a blog post every day.  That is a tad ambitious for me, but I will be increasing my writing in the coming year.  Ok, let's get to it!


While preparing for my Binary Ipsum talk which I presented at RubyJax this past October, I ran into the first tip for today.  As much as I have used bundler these past 3 or so years, I didn't know that one could use a neat command where bundler will create a basic Gemfile file for you:


bundle init

➜  ognt_tips ls -la
total 0
drwxr-xr-x   2 jfhogarty  staff    68 Nov 29 19:41 .
drwxr-xr-x  43 jfhogarty  staff  1462 Nov 29 19:41 ..
➜  ognt_tips bundle init
Writing new Gemfile to /Users/jfhogarty/Documents/RoR/LEARNING_NOTES/ognt_tips/Gemfile
➜  ognt_tips cat Gemfile
# frozen_string_literal: true
source "https://rubygems.org"

# gem "rails"
➜  ognt_tips

I can't remember how many times I manually created a Gemfile for my Ruby command line type scripts and applications.  This was a nice time saver!  If you are interested, the material for my talk for RubyJax can be found here.  See the Tutorial.md file.


In a previous post I talked about ngrok.  At the time I had only thought and used ngrok with some work I was doing between a Shopify application and a Rails API.  But here is another cool feature of ngrok - using it for ssh.  For example, say you are home and trying to pair with a co-worker who just happens to be chilling at the local coffee shop.  You can fire up ngrok and send the related info to your co-worker and they can ssh to your computer.  See the example below:


#On one computer, for example your laptop at your house:
ngrok tcp 22

ngrok by @inconshreveable                                                                                                 (Ctrl+C to quit)

Session Status                online
Account                       Your N. Here (Plan: Free)
Version                       2.1.18
Region                        United States (us)
Web Interface       
Forwarding                    tcp://0.tcp.ngrok.io:13641 -> localhost:22

Connections                   ttl     opn     rt1     rt5     p50     p90
                              2       0       0.00    0.00    8.47    16.86

# On another computer, have your co-work run:
ssh [a_username_here]@0.tcp.ngrok.io -p 13641

Wicked cool!  For more information on ngrok, checkout out their website here.


This next tip I learned during a trouble shooting session my first week at the new job.  We were trying to see if one of our hosts was able to communicate on a particular TCP port.  In this example, we were testing port 443.  In the past, one way I would trouble shoot required having the telnet server service loaded on the target.  Then I would run telnet ip_address desired_port.  But this was a pain.  Enter netcat (nc) to the rescue!  Check out the following example:


# On computer#1 you want to see if the port is accessible:
nc -l 443

The -l tells netcat to 'listen' and the 443 in this example is the port we are checking.  Your computer is now in listen mode and will wait for a request.

# On another computer, let's call it computer#2, that can communicate with over the network to computer#1, issue the following:
nc -z -w2 ip_address_here port_number_here

# Example from computer#2:
jfhogarty@kaylee:~$ nc -z -w2 -v 443
Connection to 443 port [tcp/https] succeeded!

I really wish I had known this many, many years ago!


Lastly I'd like to talk about some tips with iTerm.  If you are not a Mac user, or don't use iTerm, then you can move along.  Otherwise, sit back and enjoy the ride!  For a while I was using Tmux - partly because of the way I could split my terminal window into 3 or 4 panes.  I also used Tmux so that I could utilize the syncronize-panes feature.  See this post for more info. 


Earlier this year I had started using iTerms' ability to split the terminal horizontally or vertically.  You can find these options under the Shell menu item.  I then used Keys options under Preferences to map Control+H to 'Previous Pane' and Control+l to 'Next Pane'.  This helped me utilize iTerm for many of the things I was doing with Tmux.  But one thing I was missing was the syncronizing of panes.  Alas! iTerm can do this as well.  You can find this little gem under Shell, Broadcast Input and then choose 'All panes in All Tabs' or 'All panes in Current Tab.'  AWESOME!  The option for 'All panes in All Tabs' I think trumps what Tmux can do.  You be the judge.  ;)

Well, that wraps up today's blog post.  I hope that you find value in at least one of these tips.  Do you have a tip that you'd like to share?  Let me know - @hogihung on Twitter or john_hogarty@ognt.io


Learn Something New Everyday