检查IP是否在白名单中的脚本函数

发布于 2024-11-05 21:21:44 字数 514 浏览 0 评论 0原文

我有一个 Python 脚本,如果某些 IP 执行某些操作,则会阻止它们。该脚本包含一个检查 IP 是否已被阻止的函数。除此之外,我希望该函数还检查 IP 是否存储在数据库表中,如果是,则不要阻止它(白名单)

该函数如下所示:

def check_ip(ip_address):
    cmd = "/sbin/iptables -L INPUT -n|grep " + ip_address
    signal,output = commands.getstatusoutput(cmd)
    if signal is 0:
        return True
    else:
    return False

我不太精通 Python,所以我不确定如何去做,但我想这很简单。我很感谢您提出的任何建议。谢谢 !

稍后编辑:我想使用 MySQL 数据库,因为我将编写一个 PHP 接口来手动添加 IP。我将如何搜索它?该表只有2个字段:id和whitelist_ip,后者存储需要列入白名单的IP。

I have a Python script which blocks some IPs if they perform some actions. This script contains a function to check if the IP is already blocked. In addition to this, I'd like the function to also check if the IP is stored in a database table and if it is, do not block it ( whitelist )

The function looks like this :

def check_ip(ip_address):
    cmd = "/sbin/iptables -L INPUT -n|grep " + ip_address
    signal,output = commands.getstatusoutput(cmd)
    if signal is 0:
        return True
    else:
    return False

I'm not that versed in Python so I'm not sure on how to go with this but I imagine it's pretty simple. I'm thankful for any suggestions you might have. Thank you !

Later Edit : I want to use a MySQL database as I'll be writing a PHP interface to manually add IPs. How would I search for that ? The table only has 2 fields : id and whitelist_ip, the latter storing the IP which needs to be whitelisted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-11-12 21:21:44

如果您计划手动编辑此白名单,则只需使用包含 IP 地址列表的文件即可。 (每行一个)然后你可以使用类似这样的代码来检查它:

def check_ip(ip_address):
    cmd = "/sbin/iptables -L INPUT -n|grep " + ip_address
    signal,output = commands.getstatusoutput(cmd)
    if signal is 0:
        return True
    whitelist = open('<filename>')
    for ip in whitelist.readlines():
        if ip_address == ip:
            return True
    return False

这将首先使用你所做的检查进行检查,如果失败,它将检查 ip 白名单。因为函数从 if 子句返回,所以不需要 else 子句。

如果你想使用sql数据库来存储白名单数据,我建议使用mysql或sqlite。这两个都有可用的 python 包装器。

If you're planning on editing this whitelist manually, you could just use a file with your list of IP addresses. (one per line) You could then use code something like this to check it:

def check_ip(ip_address):
    cmd = "/sbin/iptables -L INPUT -n|grep " + ip_address
    signal,output = commands.getstatusoutput(cmd)
    if signal is 0:
        return True
    whitelist = open('<filename>')
    for ip in whitelist.readlines():
        if ip_address == ip:
            return True
    return False

This will first check using the check you've made, and if that fails, it will check the ip whitelist. Because the function returns from the if-clause, the else clause is not needed.

If you want to use an sql database to store the whitelist data, I'd recommend using mysql or sqlite. Both of these have python wrappers available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文