Monitoring Ports on Remote System

5 views
Skip to first unread message

TechieGirl

unread,
Apr 17, 2006, 12:41:49 PM4/17/06
to
I would like to be able to monitor open ports for service monitoring on
remote systems. Is there a way to script this? Normally I would manually
do this using the telnet command "open server.mydomain.com 80" or "open
server.mydomain.com 25" and so on. Is there a better way?


William Stacey [MVP]

unread,
Apr 17, 2006, 3:32:26 PM4/17/06
to
Here is some c# code to get your started checking tcp ports.  You could probably convert this to a script or leave as an exe and call it via script:

using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.Threading;

 

namespace OpenPorts

{

    class Program

    {

        static void Main(string[] args)

        {

            if ( args.Length != 3 )

                Usage();

            string host = args[0];

            int min = ParseInt(args[1]);

            int max = ParseInt(args[2]);

            if ( min < IPEndPoint.MinPort || max > IPEndPoint.MaxPort || min > max )

                Usage();

            Console.WriteLine("Searching open ports...");

            int totPorts = (max - min) + 1;

            int totTested = 0;

            int openedPorts = 0;

            AutoResetEvent waitEvent = new AutoResetEvent(false);

            ThreadPool.SetMaxThreads(200, 1000);

            ThreadPool.SetMinThreads(200, 100);

            for(int port = min; port <= max; port++)

            {

                ThreadPool.QueueUserWorkItem(delegate(object state)

                {

                    int p = (int)state;

                    TcpClient client = new TcpClient();

                    try

                    {

                        client.Connect(host, p);

                        Interlocked.Increment(ref openedPorts);

                        Console.WriteLine("{0} open.", p);

                    }

                    catch

                    {

                        //Console.Write(".");

                    }

                    finally

                    {

                        client.Close();

                        int tested = Interlocked.Increment(ref totTested);

                        if ( tested >= totPorts )

                            waitEvent.Set();

                    }

                }, port);

            }

            waitEvent.WaitOne();

            Console.WriteLine("Ports tested: {0}", totTested);

            Console.WriteLine("Ports opened: {0}", openedPorts);

        }

 

        private static void Usage()

        {

            Console.WriteLine("Usage: OpenPorts <host> <minport> <maxport>");

            Environment.Exit(1);

        }

 

        private static int ParseInt(string s)

        {

            int i;

            if ( ! int.TryParse(s, out i) )

                return -1;

            return i;

        }

    }

}


--
William Stacey [MVP]
 
"TechieGirl" <nos...@nospam.net> wrote in message news:edJqz3jY...@TK2MSFTNGP05.phx.gbl...

/\/\o\/\/

unread,
Apr 18, 2006, 3:31:28 PM4/18/06
to
William Stacey [MVP] wrote:
> Here is some c# code to get your started checking tcp ports. You could
> probably convert this to a script or leave as an exe and call it via script:
>

Working from your example I did a basic Monad version

http://mow001.blogspot.com/2006/04/simple-port-scanning-using-monad.html

gr /\/\o\/\/

William Stacey [MVP]

unread,
Apr 18, 2006, 5:02:28 PM4/18/06
to
Cool. I might make the banner optional to speed it up even more as some
servers don't send a hello msg. If your goal is to just check for listening
ports, then a Connect would show that much. Cheers.

--
William Stacey [MVP]

"/\/\o\/\/" <n...@spam.mow> wrote in message
news:uaZk76xY...@TK2MSFTNGP03.phx.gbl...

/\/\o\/\/

unread,
Apr 18, 2006, 5:37:12 PM4/18/06
to
William Stacey [MVP] wrote:
> Cool. I might make the banner optional to speed it up even more as some
> servers don't send a hello msg. If your goal is to just check for listening
> ports, then a Connect would show that much. Cheers.
>

Thanks,

Yes making banner info optional would be a good thing,
but mostly the banners are more quick as a TimeOut on a closed
port.(even with the Sleep statement), but it will improve on the open
ports with no banner

also I kept the script simple (for big scans I just use LineScan ;-) .

Greetings /\/\o\/\/

PS. if anyone knows why the MS telnet banner is garbage, plz reply ;-)

Reply all
Reply to author
Forward
0 new messages