We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Scan for MSSQL servers

Narusegawa
Narusegawa asked
on
Medium Priority
457 Views
Last Modified: 2010-04-05
Hi,
     How can one scan for MSSQL servers running on the current network? And return this list (preferably hostname's and version) to a stringlist?

Thanks
Comment
Watch Question

You can actually try doing a telnet to every IP in the subnet at the default MSSQL port.

However, 2 considerations
1. Some administrators set their MSSQL to not use the default port
2. Your system might be mistaken as a virus/exploit

Commented:
There is a program called SQL ping on Internet. The following is the program ported to Delphi by me. It is not a good design program just an example.

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, NMUDP, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient;

type
  TForm1 = class(TForm)
    NMUDP1: TNMUDP;
    Button1: TButton;
    Memo1: TMemo;
    IdUDPClient1: TIdUDPClient;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
      FromIP: String; Port: Integer);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var Buff : array[0..0] of char;
begin

    Buff[0] := #2;
    NMUdp1.SendBuffer(Buff,1);
end;

procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
  NumberBytes: Integer; FromIP: String; Port: Integer);
var
    Buff : array[0..65535] of char;
    i, Len, cnt : integer;
    s : string;
begin
    s := IntToStr(NumberBytes)+' bytes received from IP '+FromIP;
    NMUdp1.ReadBuffer (Buff,Len);
    s := s + #13#10; cnt := 0;
    for i := 3 to Len-1 do
    begin
        if ((Buff[i] =';') and (Buff[i+1]<> ';')) then
        begin
            if cnt mod 2 = 0 then
            begin
                s := s + ':';
                cnt := cnt + 1;
            end
            else
            begin
                s := s+#13#10;
                cnt := cnt + 1;
            end
        end
        else
        begin
            if Buff[i] <> ';' then
                s := s + char(Buff[i])
            else
                s := s + #13#10;
        end;
        //showmessage(Buff[i]+' , '+ S);
    end;
    Memo1.Lines.Add (s);
end;

procedure TForm1.Button2Click(Sender: TObject);
var Buff : array[0..0] of char;
    RxBuff : array [0..117] of char;
    ReceivedString,s : String;
    i, cnt : integer;
begin
    Buff[0] := #2;
    IdUDPClient1.SendBuffer (Buff,1);
    //ReceivedString := idUDPClient1.ReceiveString();
    idUDPClient1.ReceiveBuffer (RxBuff,117);
    s := s + #13#10; cnt := 0;
    for i := 3 to 116 do
    begin
        if ((RxBuff[i] =';') and (RxBuff[i+1]<> ';')) then
        begin
            if cnt mod 2 = 0 then
            begin
                s := s + ':';
                cnt := cnt + 1;
            end
            else
            begin
                s := s+#13#10;
                cnt := cnt + 1;
            end
        end
        else
        begin
            if RxBuff[i] <> ';' then
                s := s + char(RxBuff[i])
            else
                s := s + #13#10;
        end;
        //showmessage(Buff[i]+' , '+ S);
    end;
    Memo1.Lines.Add (s);
end;

end.

object Form1: TForm1
  Left = 341
  Top = 191
  Width = 544
  Height = 376
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 176
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Memo1: TMemo
    Left = 96
    Top = 88
    Width = 337
    Height = 177
    TabOrder = 1
  end
  object Button2: TButton
    Left = 280
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 2
    OnClick = Button2Click
  end
  object NMUDP1: TNMUDP
    RemoteHost = 'jtsnet1'
    RemotePort = 1434
    LocalPort = 0
    ReportLevel = 1
    OnDataReceived = NMUDP1DataReceived
    Left = 72
    Top = 32
  end
  object IdUDPClient1: TIdUDPClient
    Active = True
    Host = '10.20.0.22'
    Port = 1434
    ReceiveTimeout = 250
    Left = 40
    Top = 104
  end
end

You can use this technique by first check on default port. If not found then check on another port.


SoapSiam

 
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
ListServers(StringGrid1, '\\192.10.25.254', '');

How would one use that function for the current domain? Not knowing the current domain name, as this could be used on different domains and ranges of ip. Thanks

Author

Commented:
Nevermind, leaving the 2nd 2 parameters blank works.

Thanks ever so much for this. I've modified it slightly and added it to the onDropDown event of a ComboBox. Works a treat.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.