0

I need a way to identify whether a given port at a given address is running an instance of Microsoft SQL Server, IBM DB2 Server, or neither, from Python.

My first thought is that I can use Python's telnet library, telnetlib, like this:

import telnetlib
import socket

def checkDBServerType(address, port):
    try:
        tn = telnetlib.Telnet(address, port)
    except socket.error:
        return None
    tn.write(<something>)
    if <something-else> in tn.read_eager():
        return "MSSQL"
    else:
        return "IBMDB2"

The issue is, I have no idea what to send. The user will also be providing my program with a username, password, and database name, so those are also available if that helps.

Also, this is my first post on ServerFault although I've used StackOverflow regularly and SuperUser sometimes. Is this the proper venue for my question, or would it be more appropriate on StackOverflow? (I can't decide if server admin type people or programmer type people would be more likely to be able to help.)

1
  • Personally I feel that, yes, this is more of a SO question, but there's that gray area. Guess it's fine. Commented Aug 11, 2014 at 17:38

2 Answers 2

3

Since you're just looking for a heuristic, I'd say that merely being able to connect to the default port would be a good first cut. So, for instance, if you can connect to TCP 1433, you can reasonably say that that machine is running a default instance of SQL Server. It's not perfect of course (i.e. you could get false positives or false negatives), but it's pretty good. Only you can answer whether it's good enough for you.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 and accepted: That's an amazingly good point that I hadn't considered. For the record, MSSQL is normally on port 1433 while IBMDB2 is normally on port 50000.
@ArtOfWarfare DB2's default port is 50000, but unlike MSSQL or Oracle, it is extremely common to find DB2 listening on other ports.
@IanBjorhovde - Right now, I just have it set to check if the user says they're using DB2 but they pick port 1433, or they say they're using MSSQL but they pick port 50000, then it prompts the user to let them know that they may have picked the wrong options. The issue is this is a script that installs an environment using a configuration file, and if they made the wrong choices, it can be difficult to undo those wrong choices (which they won't even realize they made until the script finishes running, about an hour later). It performs this simple check at the start to avoid the common mistake.
3

You can't simply "talk" to a database server and expect it to tell you what kind of software it's running; there is no standard common protocol to connect to database servers, and although the query language (SQL) is quite standardized, the underlying connection is based on a protocol which is specific to each database system; these protocols are also generally not text-based, thus you can't simply open a socket to a database server and write something on it; also, they are usually never used directly by client applications: every DBMS provides a set of connection libraries which neatly encapsulate them, so that you don't have to understand how to talk to the database server and can focus on actually querying its data.

Your best bet would be to grab the client connection libraries for SQL Server and DB2 and ask each of them to connect to the remote server; whetever one succeeds first will tell you what kind of server is sitting on the remote end.

2 Comments

Thank you for your response - I will keep it in mind, but I'm hoping for a simpler solution with fewer dependencies. IE, maybe I can just send a few bytes rather than a string where one would respond and the other would remain silent.
That would require you to study and understand the connection protocols for both DBs. I can't really see how this is "simpler" than using their client libraries, which are both available for Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.