Adding the Port Scanning Functionality
Learn how to implement the port scanning functionality.
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.
// Package scan provides types and functions to perform TCP port// scans on a list of hostspackage scanimport ("fmt""net""time")
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:
// PortState represents the state of a single TCP porttype PortState struct {Port intOpen state}
We define the custom type state as a wrapper on the bool type. This type uses true or false to indicate whether a port is open or closed. By creating a custom ...