通过侦听端口(Inno Setup)在LAN上发现我的SQL服务器

时间:2017-05-18 21:08:26

标签: mysql inno-setup tcp-ip tcplistener pascalscript

我需要搜索IP address Listening Port来查找其他PC上的PC(尝试发现MySQL服务器)并获得有该端口监听的结果IP

类似于此代码测试套接字,但在Inno Setup中工作:

program pfinger;

uses sockets,errors;

Var
  Addr : TInetSockAddr;
  S : Longint;
  Sin,Sout : Text;
  Line : string;

begin
  Addr.sin_family:=AF_INET;
  { port 79 in network order }
  Addr.sin_port:=79 shl 8;
  { localhost : 127.0.0.1 in network order }
  Addr.sin_addr.s_addr:=((1 shl 24) or 127);
  S:=fpSocket(AF_INET,SOCK_STREAM,0);
  If Not Connect (S,ADDR,SIN,SOUT) Then
    begin
    Writeln ('Couldn''t connect to localhost');
    Writeln ('Socket error : ',strerror(SocketError));
    halt(1);
    end;
  rewrite (sout);
  reset(sin);
  writeln (sout,paramstr(1));
  flush(sout);
  while not eof(sin) do
    begin
    readln (Sin,line);
    writeln (line);
    end;
  fpShutdown(s,2);
  close (sin);
  close (sout);
end.

我已经尝试过其他方式(Java代码)。但是当我需要解压缩JRE以运行Java jar时,安装程​​序启动缓慢。我尝试包装我的Java代码,但不知道在哪里/如何找到类似于Java的Pascal函数。

2 个答案:

答案 0 :(得分:2)

要检查服务器是否正在侦听端口,您可以使用Winsock OLE control

type
  TSocketState =
    (sckClosed, sckOpen, sckListening, sckConnectionPending, sckResolvingHost,
     sckHostResolved, sckConnecting, sckConnected, sckClosing, sckError);

type
  TMsg = record
    hwnd: HWND;
    message: UINT;
    wParam: Longint;
    lParam: Longint;
    time: DWORD;
    pt: TPoint;
  end;

const
  PM_REMOVE = 1;

function PeekMessage(var lpMsg: TMsg; hWnd: HWND; wMsgFilterMin, wMsgFilterMax,
  wRemoveMsg: UINT): BOOL; external 'PeekMessageA@user32.dll stdcall';
function TranslateMessage(const lpMsg: TMsg): BOOL;
  external 'TranslateMessage@user32.dll stdcall';
function DispatchMessage(const lpMsg: TMsg): Longint;
  external 'DispatchMessageA@user32.dll stdcall';

procedure AppProcessMessage;
var
  Msg: TMsg;
begin
  while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do
  begin
    TranslateMessage(Msg);
    DispatchMessage(Msg);
  end;
end;

function CheckPort(Host: string; Port: Integer): Boolean;
var
  Socket: Variant;
begin
  Socket := CreateOleObject('MSWinsock.Winsock');
  Socket.RemoteHost := Host;
  Socket.RemotePort := Port;
  Socket.Connect;

  { Winsock requires message pumping }
  while not (Socket.State in [sckConnected, sckError]) do 
  begin
    AppProcessMessage;
  end;

  Result := (Socket.State = sckConnected);

  if Result then
  begin
    Log(Format('Port %d on %s is open', [Port, Host]));
  end
    else
  begin
    Log(Format('Port %d on %s is NOT open', [Port, Host]));
  end;
  Socket.Close;
end;

请注意Winsock控件需要消息队列泵送。因此,您可能需要在运行检查之前禁用向导,以防止用户弄乱表单。

致谢:AppProcessMessage来自How to execute 7zip without blocking the InnoSetup UI?

答案 1 :(得分:2)

检查开放端口的目的是 Nmap 程序。

在TCP端口3306上探测地址192.168.200.133的示例命令是:

nmap -sT -sV -Pn -n -p 3306 192.168.200.133

您还可以通过提供CIDR范围而不是IP来扫描整个子网。例如,要在192.168.200.1 - 192.168.200.254上扫描MySQL实例(TCP端口3306),您可以这样做:

nmap -sT -sV -Pn -n -p 3306 192.168.200.0/24

^^这些例子直接来自https://securityblog.gr/1549/discover-open-mysql-ports/

Nmap在Unix / Linux上非常常用,但在2000年首次移植到Windows,目前在Win 7或更新版本(包括Server 2008)上受支持。见https://nmap.org/book/inst-windows.html