检查Postgresql是否在监听 [英] Check if Postgresql is listening

查看:463
本文介绍了检查Postgresql是否在监听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出IP地址和端口号,是否可以检查具有该IP地址的机器是否在指定端口上侦听了Postgresql?如果可以,怎么办?

Given an IP Address and port number, is it possible to check if the machine with that IP address has Postgresql listening on the specified port? If so, how?

我只想获取一个布尔值,表明Postgresql是否在指定计算机的指定端口上进行监听.

I just want to obtain a boolean value of whether Postgresql is listening on the specified port of the specified machine.

推荐答案

我认为您需要定义自己想要实现的目标.您是否只想知道什么在某个点上正在听?是否 PostgreSQL 在给定端口上侦听?如果PostgreSQL正在运行并且实际上接受连接?如果您可以连接到PostgreSQL,是否成功进行身份验证并发出查询?

I think you need to define what you're trying to achieve better. Do you just want to know if anything is listening on a certain point? If PostgreSQL is listening on a given port? If PostgreSQL is running and actually accepting connections? If you can connect to PostgreSQL, authenticate successfully and issue queries?

一种选择是调用psql连接到它并检查结果代码.不要尝试解析输出文本,因为它可能会翻译成不同的语言.

One option is to invoke psql to connect to it and check the result code. Do not attempt to parse the output text, since that's subject to translation into different languages.

更好地使用客户端库来选择语言-Python的psycopg2,Java的PgJDBC,Ruby的Pg gem,Perl的DBD::Pg,C#的nPgSQL,等等. d推荐.来自任何连接错误的SQLSTATE或异常详细信息将告诉您有关连接失败的原因的更多信息-您可以通过这种方式分辨出服务器未监听,身份验证失败等之间的区别.例如,在Python中:

Better, use the client library for the language of your choice - psycopg2 for Python, PgJDBC for Java, the Pg gem for Ruby, DBD::Pg for Perl, nPgSQL for C#, etc. This is the approach I'd recommend. The SQLSTATE or exception details from any connection error will tell you more about why the connection failed - you'll be able to tell the difference between the server not listening, authentication failure, etc this way. For example, in Python:

import psycopg2
try:
    conn = psycopg2.connect("host=localhost dbname=postgres")
    conn.close();
except psycopg2.OperationalError as ex:
    print("Connection failed: {0}".format(ex))

ex.pgcode(SQLSTATE)中有异常详细信息,以向您详细说明服务器端生成的错误,例如身份验证失败;对于客户端错误,它将为空.

There are exception details in ex.pgcode (the SQLSTATE) to tell you more about errors that're generated server-side, like authentication failures; it'll be empty for client-side errors.

如果只想查看某物是否正在侦听给定的IP和TCP端口,则可以使用netcat(仅* nix)或使用所选语言的简单脚本它创建一个套接字并执行connect(),如果获得成功的响应,则关闭该套接字.例如,以下简单的Python脚本:

If you just want to see if something is listening on a given IP and TCP port, you can use netcat (*nix only), or a simple script in the language of your choice that creates a socket and does a connect() then closes the socket if it gets a successful response. For example, the following trivial Python script:

import socket                                                                                                                                                              
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect(('localhost',5432))
    s.close()
except socket.error as ex:
    print("Connection failed with errno {0}: {1}".format(ex.errno, ex.strerror))            

同一方法适用于任何编程语言,只是套接字库的详细信息和错误处理有所不同.

The same approach applies in any programming language, just the details of the socket library and error handling vary.

出于某些目的,使用netstat工具被动列出哪些进程正在侦听哪些网络套接字也可能很有用. Windows上内置的netstat非常令人讨厌,因此与其他平台上的netstat相比,您必须执行更多的输出解析,但仍然可以完成.但是,在netstat中存在套接字并不意味着可以成功连接.如果该进程以某种方式导致失败但仍然无法运行(陷入无限循环,被调试器阻止,SIGSTOP ed等),则它将不会响应实际的连接尝试.

For some purposes it can also be useful to use the netstat tool to passively list which processes are listening on which network sockets. The built-in netstat on Windows is pretty brain-dead so you have to do more parsing of the output than with netstat for other platforms, but it'll still do the job. The presence of a socket in netstat doesn't mean that connecting to it will succeed, though; if the process has failed in some way that leaves it broken but still running (stuck in an infinite loop, blocked by a debugger, SIGSTOPed, etc) then it won't respond to an actual connection attempt.

这篇关于检查Postgresql是否在监听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆