Solved

Scan for MSSQL servers

Posted on 2004-03-31
5
383 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
0
Comment
Question by:Narusegawa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 10726176
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
0
 
LVL 1

Expert Comment

by:soapsiam
ID: 10738397
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

 
0
 

Accepted Solution

by:
Doronl earned 50 total points
ID: 11027139
Use NetServerEnum with SV_TYPE_SQLSERVER.

Example:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls, Grids, ShellApi;

type
  PServerInfo101 = ^TServerInfo101;
  TServerInfo101 = packed record
    sv101_Platform_ID   : DWord;
    sv101_Name          : PWideChar;
    sv101_Version_Major : DWord;
    sv101_Version_Minor : DWord;
    sv101_Type          : DWord;
    sv101_Comment       : PWideChar;
  end;

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

const
  MAX_PREFERRED_LENGTH = DWord(-1);
  SV_TYPE_SQLSERVER           = $00000004;

  function NetServerEnum(ServerName: PWideChar;
                         level: DWord;
                         var Buffer: Pointer;
                         PrefMaxLen: DWord;
                         var EntriesRead,
                             TotalEntries: DWord;
                         ServerType: DWord;
                         Domain: PWideChar;
                         var resume_handle: DWord): LongWord; stdcall;
external 'netapi32.dll';

  function NetApiBufferFree(Buffer: Pointer): LongWord; stdcall;
external 'netapi32.dll';


implementation

{$R *.DFM}


procedure ListServers(aStringGrid: TStringGrid; aServer, aDomain:
string);
var
  EntriesRead  : DWord;
  I            : Integer;
  ResumeHandle : DWord;
  Rslt         : LongWord;
  SessionInfo,
  P            : PServerInfo101;
  TotalEntries : DWord;
  TotalSoFar   : LongWord;
  CName,
  CDomain      : PWideChar;
  aString      : string;
  intIPAdres   : integer;
  strIPAdres   : string;
begin
  ResumeHandle := 0;
  TotalSoFar   := 0;

  aString := aServer;
  CName   := StringToOleStr(aString);
  aString := aDomain;
  CDomain := StringToOleStr(aString);
  repeat
    Rslt := NetServerEnum(CName,
                          101,
                          Pointer(SessionInfo),
                          MAX_PREFERRED_LENGTH,
                          EntriesRead,
                          TotalEntries,
                          SV_TYPE_SQLSERVER,
                          NIL, //CDomain,
                          ResumeHandle);
    if Rslt <> 0 then
      begin
        showmessage('Fout : ' + IntToStr(Rslt) + ' meaning "' +
SysErrorMessage(Rslt) + '"');
      end;

    P := SessionInfo;
    for I := 0 to EntriesRead - 1 do
      begin
        if WideCharToString(P.sv101_Name) <> '' then
          begin
            aStringGrid.Cells[0, aStringGrid.RowCount - 1] :=
IntToStr(P.sv101_Platform_ID);
            aStringGrid.Cells[1, aStringGrid.RowCount - 1] :=
WideCharToString(P.sv101_Name);
            aStringGrid.Cells[2, aStringGrid.RowCount - 1] :=
IntToStr(P.sv101_Version_Major);
            aStringGrid.Cells[3, aStringGrid.RowCount - 1] :=
IntToStr(P.sv101_Version_Minor);
            aStringGrid.Cells[4, aStringGrid.RowCount - 1] :=
IntToStr(P.sv101_Type);
            aStringGrid.Cells[5, aStringGrid.RowCount - 1] :=
WideCharToString(P.sv101_Comment);

            aStringGrid.RowCount := aStringGrid.RowCount + 1;
          end;
        Inc(LongWord(P), SizeOf(TServerInfo101))
      end;
    Inc(TotalSoFar, EntriesRead);
  until TotalSoFar >= TotalEntries;

  Rslt := NetApiBufferFree(SessionInfo);
  if Rslt <> 0 then
    showmessage('Fout : ' + IntToStr(Rslt) + ' meaning "' +
SysErrorMessage(Rslt) + '"');
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  ListServers(StringGrid1, '\\192.10.25.254', '');
end;

end.
0
 

Author Comment

by:Narusegawa
ID: 11097846
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
0
 

Author Comment

by:Narusegawa
ID: 11097972
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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Firemonkey Need Sample for Online Shopping Example. 2 193
delphi parse string to params 3 156
PHP preg_replace code convert to Delphi 14 81
Browsing a TTreeView in Delphi 5 32
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question