Results 1 to 14 of 14

Thread: Interesting requirement! How to query port numbers from 120 servers!

  1. #1
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80

    Interesting requirement! How to query port numbers from 120 servers!

    I need to document the port numbers from nearly 120 sql servers in the network. Is there any query that I can use to get this info??

    Thanks,

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    You can get that in registry.

  3. #3
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You can run this in each server (non clustered)

    DECLARE @test varchar(20), @key varchar(100)

    set @key = 'SOFTWARE\MICROSOFT\MSSQLServer\MSSQLServer\Supers ocketnetlib\TCP'
    EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
    @key=@key,
    @value_name='Tcpport',
    @value=@test OUTPUT
    SELECT @test

    If it is clustered run this

    DECLARE @test varchar(20), @key varchar(100)

    set @key = 'SOFTWARE\MICROSOFT\Microsoft SQL Server\'+@@servicename+'\MSSQLServer\Supersocketne tlib\TCP'
    EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
    @key=@key,
    @value_name='Tcpport',
    @value=@test OUTPUT
    SELECT @test


    You can make it more intelligent by querying fn_virtualservernodes() to list nodes in a cluster, if it returns null then it is a stand alone server.

  4. #4
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    1. Create and list all the servernames on C:\servers.txt

    Server1
    Server2
    Server3
    Server4
    Server5

    2. Create C:\port.sql as below
    set nocount on
    DECLARE @test varchar(20), @key varchar(100)
    set @key = 'SOFTWARE\MICROSOFT\MSSQLServer\MSSQLServer\Supers ocketnetlib\TCP'
    EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
    @key=@key,
    @value_name='Tcpport',
    @value=@test OUTPUT
    SELECT 'Server Name: '+@@servername + ' Port Number:'+convert(varchar(10),@test)


    3. Create c:\findport.bat
    REM Type: Batch File
    REM Created by: MAK
    REM Contact: mak_999@yahoo.com
    REM Execute osql Servers.txt

    for /f "tokens=1,2,3" %%i in (C:\Servers.txt) do OSQL -S%%i -t0 -E -dMASTER -ic:\PORT.sql

    4. Run the batch file

    c:\findport.bat > C:\Findport.log

    5. Open the c:\Findport.log file.

  5. #5
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    Great! Before I try these options, is there a way to run a single script which will find the sql servers by itself across the network and generate the port numbers, as we have no list of servers available ready..:-)

  6. #6
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    Skhanal,

    When I ran your scripts both of them are giving 'NULL' as output, any idea?

  7. #7
    Join Date
    Sep 2002
    Posts
    5,938
    You can get server list with following command in dos prompt:

    osql -L

  8. #8
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    Hi MAk,

    When I ran the bat file I am getting the following error:

    bat file:

    for /f "tokens=1,2,3" %%i in (C:\Servers.txt) do OSQL -S%%i -t0 -E -dMASTER -ic:\PORT.sql

    error:
    output
    --------------------------------
    %%i was unexpected at this time.
    NULL

    Any idea MAK?

  9. #9
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    a. I wrote 2 articles on how to find sql server on the network.

    http://www.databasejournal.com/article.php/3300441

    2. when you copied and pasted skhanal's script it added a carriage return between

    Supers and ocketnetlib\TCP'

    Bring every broken line to a single line.

    3. Follow No.2 in my batch file and sql statements.

  10. #10
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    Hi MAK,
    I did correct the script to bring it
    into one line when I got this error. Any thing else missing here?

  11. #11
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    Hi Skhanal? Any help please?

  12. #12
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    please find attached file
    Attached Files Attached Files

  13. #13
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    Thanks MAK, but I am getting the same error again..any idea?

  14. #14
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Check the registry setting in one of your servers to see if the tcpport key exists and if the location is correct in the script.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •