Adding the Port Scanning Functionality
Explore how to develop port scanning features in a Go command-line interface tool. Learn to define custom types representing port states, implement scanning with network timeout controls, and handle multiple hosts with error detection. This lesson prepares you to build robust scanning functions integrated into your CLI applications.
We'll cover the following...
Our application is coming along nicely. We can manage hosts on which to
execute a port scan. Let’s implement the port scanning functionality now.
Let’s start by adding the functionality to the scan package. After that, we’ll
implement the subcommand on the command-line tool.
Creating the scanHosts.go file
We create and edit the file scanHosts.go to hold the code related to the scan functionality. We add the package definition and the import list.
For this functionality, we’ll use:
- The package
fmtfor formatted printing. - The package
netfor network-related functions. - The package time to define
timeouts.
Next, we define a new custom type called PortState that represents the state for a single TCP port. This struct has two fields: Port of type int that corresponds to the TCP port and Open of type state that indicates whether the port is open or closed.
We’ll define the type state shortly:
We define the custom type state as a wrapper on the bool type. This type uses true or false to indicate ...