将用于Internet访问的脚本列入白名单

时间:2018-09-07 07:13:40

标签: linux windows networking server firewall

我们有Windows服务器和Linux服务器。两台服务器上都没有互联网。现在,我们需要在这些服务器上部署一个python脚本,该脚本将向外部网络url发出http get请求。所以,我需要互联网。但是我们不能为所有应用程序启用Internet。有没有一种方法可以仅为此特定脚本启用Internet?

1 个答案:

答案 0 :(得分:1)

我不太了解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.