Avatar of Narusegawa
Narusegawa asked on

Scan for MSSQL servers

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
Delphi

Avatar of undefined
Last Comment
Narusegawa

8/22/2022 - Mon
DragonSlayer

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
soapsiam

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

 
ASKER CERTIFIED SOLUTION
Doronl

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Narusegawa

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
Narusegawa

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.