linux - 将上网脚本列入白名单

标签 linux windows networking server firewall

我们有一个 Windows 服务器和一个 Linux 服务器。两台服务器上都没有互联网。现在,我们需要在这些服务器上部署一个 python 脚本,它将向外部网络 url 发出 http get 请求。所以,我需要互联网。但我们无法为所有应用程序启用互联网。有没有办法只为这个特定的脚本启用互联网?

最佳答案

我不太了解 Linux 平台,但您可以允许程序使用命令行发出传出请求,像这样从 python 中:

import sys
import os

def allow_outbound_connections(program_path):
    """
    Allow program outbound connections.
    """
    if "win" in sys.platform:
        command = f'netsh advfirewall firewall add rule name="{program_path}" '\
              'dir=out action=block program= "{os.path.basename(program_path}"'\
              ' enable=yes profile=any'
    if "linux" in sys.platform:
        # *Add a similar command in Linux*

    with os.popen(command) as stream:
        return stream.read()


def main():
    # First allow this program to make outbound connections.
    output = allow_outbound_connections(__file__)
    # (Eventually you could handle the output)

    # Now make request to an outside network url.

关于linux - 将上网脚本列入白名单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52217194/

相关文章:

c++ - 重新连接到通过 COM 启动的进程

java - 通过网络将 RSA 公钥从 C 发送到 Java

linux - docker + Elasticsearch : access is denied when creating the log file

linux - Mercurial Web 推送权限被拒绝

windows - LDAP 服务器签名协议(protocol)

networking - CRC计算卡住

java - IP 数据报中有效负载/数据的大小是如何决定的

c - 使用程序名称查找进程 ID

linux - 系统重启时自动启动 Asterisk

python - 使用 npm-install --save 时出现错误 : Can't find Python executable . ..(系统变量已设置 python)