.. image:: https://github.com/opencve/opencve/workflows/Tests/badge.svg :target: https://github.com/ncrocfer/whatportis/actions/workflows/tests.yml
.. image:: https://badge.fury.io/py/whatportis.svg :target: https://pypi.python.org/pypi/whatportis/
.. image:: https://img.shields.io/badge/python-3.6%2B-blue.svg :target: https://pypi.python.org/pypi/whatportis/
.. image:: https://img.shields.io/github/license/ncrocfer/whatportis.svg :target: https://github.com/ncrocfer/whatportis/blob/master/LICENSE.txt
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/python/black
搜索服务的默认端口号是一项常见任务。/etc/services 文件中提供了一些端口,但列表不完整且该解决方案不可移植。
Whatportis 是一个简单的工具,它下载 Iana.org <http://www.iana.org/assignments/port-numbers>_ 数据库并使用它来探索官方端口列表。
Whatportis 允许你查找某个服务关联的端口:
.. code-block:: shell
$ whatportis redis
+-------+------+----------+---------------------------------------+
| Name | Port | Protocol | Description |
+-------+------+----------+---------------------------------------+
| redis | 6379 | tcp | An advanced key-value cache and store |
+-------+------+----------+---------------------------------------+
或者,反过来,查找某个端口号关联的服务:
.. code-block:: shell
$ whatportis 5432
+------------+------+----------+---------------------+
| Name | Port | Protocol | Description |
+------------+------+----------+---------------------+
| postgresql | 5432 | tcp, udp | PostgreSQL Database |
+------------+------+----------+---------------------+
你也可以通过添加 :code:--like 选项来搜索模式,而无需知道确切名称:
.. code-block:: shell
$ whatportis mysql --like
+----------------+-------+----------+-----------------------------------+
| Name | Port | Protocol | Description |
+----------------+-------+----------+-----------------------------------+
| mysql-cluster | 1186 | tcp, udp | MySQL Cluster Manager |
| mysql-cm-agent | 1862 | tcp, udp | MySQL Cluster Manager Agent |
| mysql-im | 2273 | tcp, udp | MySQL Instance Manager |
| mysql | 3306 | tcp, udp | MySQL |
| mysql-proxy | 6446 | tcp, udp | MySQL Proxy |
| mysqlx | 33060 | tcp | MySQL Database Extended Interface |
+----------------+-------+----------+-----------------------------------+
.. code-block:: shell
$ pip install whatportis
Whatportis 使用本地 JSON 文件 (:code:~/.whatportis_db.json) 来查询端口列表。首次使用时会自动创建该文件,之后你可以使用 :code:--update 选项进行同步:
.. code-block:: shell
$ whatportis --update
Previous database will be updated, do you want to continue? [y/N]: y
Downloading https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv...
Populating database...
14145 ports imported.
你可以使用 :code:--json 选项将结果以 JSON 格式显示:
.. code-block:: shell
$ whatportis 5432 --json
[
{
"name": "postgresql",
"port": "5432",
"protocol": "tcp, udp",
"description": "PostgreSQL Database"
}
]
Whatportis 也可以作为 RESTful API 服务器启动。此功能默认未启用,你需要安装一个额外的包:
.. code-block:: shell
$ pip install whatportis[server]
$ whatportis --server localhost 8080
* Serving Flask app "whatportis.server" (lazy loading)
* Environment: prod
* Debug mode: off
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
端点 :code:/ports 用于获取完整列表(可能很长),:code:/ports/<search> 用于搜索特定端口:
.. code-block:: shell
$ curl http://127.0.0.1:8080/ports/3306
{"ports":[{"description":"MySQL","name":"mysql","port":"3306","protocol":"tcp, udp"}]}
$ curl http://localhost:8080/ports/redis
{"ports":[{"description":"An advanced key-value cache and store","name":"redis","port":"6379","protocol":"tcp"}]}
你可以使用 :code:?like 参数搜索模式。