简体   繁体   English

将 Internet 访问脚本列入白名单

[英]Whitelist a script for Internet Access

We've a windows server and a linux server.我们有一个 Windows 服务器和一个 linux 服务器。 There is no internet on both servers.两台服务器上都没有互联网。 Now, we need to deploy a python script on these servers which will make an http get request to an outside network url.现在,我们需要在这些服务器上部署一个 python 脚本,它将向外部网络 url 发出 http get 请求。 So, I would need internet for that.所以,我需要互联网。 But we can't enable internet for all applications.但是我们无法为所有应用程序启用互联网。 Is there a way we can enable internet only for this particular script?有没有办法只为这个特定的脚本启用互联网?

I don't know well Linux platform, but you could allow the program to make outgoing requests with the command line, from python like so:我不太了解 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM