
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Use the Current Internet Connection Settings
|
929
new CheapoPortScanner.OpenPortFoundEventHandler(cps_OpenPortFound);
cps.Scan( );
Console.WriteLine("Found {0} ports open and {1} ports closed",
cps.OpenPorts.Count, cps.ClosedPorts.Count);
// Do the local machine, whole port range 1-65535.
cps = new CheapoPortScanner( );
cps.Scan( );
cps.ReportToConsole( );
}
static void cps_OpenPortFound(object sender, CheapoPortScanner.OpenPortEventArgs
args)
{
Console.WriteLine("OpenPortFound reported port {0} was open",args.PPortNumP);
}
The output for the port scanner as shown appears here:
Checking ports 1-30 on localhost...
OpenPortFound reported port 22 was open
OpenPortFound reported port 26 was open
Found 2 ports open and 28 ports closed
Discussion
Open ports on a machine are significant because they indicate the presence of a pro-
gram listening on those ports. Hackers look for “open” ports as ways to enter your
systems without permission.
CheapoPortScanner is an admittedly rudimentary mecha-
nism for checking for open ports, but it demonstrates the principle well enough to
provide a good starting point.
If you run this on a corporate network, you may quickly get a visit
from your network administrator, as you may set off alarms in some
intrusion-detection systems. Be judicious in your use of this ...