mysql - 从文本文件读取 IP 地址到 IPTABLES

标签 mysql bash ubuntu iptables ufw

目前我正在使用 ufw 通过键入手动添加 ip 规则

sudo ufw allow from (my ip address)

我通过从数据库调用一些脚本来检索客户端 IP 地址并将其保存到 .txt 文件中。

这是我得到的值

  ipaddress
 10.1.100.90
 10.1.100.91

是否可以读取 .txt 文件中的 IP 地址并将其保存到规则中,而不是手动输入?

最佳答案

您可以使用如下所示的 bash 脚本

#!/bin/bash

{ 
    # Skip the header line containing 'name  ipaddress'
    read skipLine;                     

    # Read the rest of the lines till end of the file
    while IFS= read -r name IP
    do
        # All the IP's are stored in the variable $IP. Use it
        # however you want
        printf  "%s %s\n" "$name" "$IP"

        # To print only the IPs, do
        # printf  "%s\n" "$IP"

    done
} <IPFile 

# IPFile - Input file name containing IP addresses. Replace it with your file

关于mysql - 从文本文件读取 IP 地址到 IPTABLES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41997248/

相关文章:

mysql - 如何使用 DBGrid 从第 1 行、第 2 行、第 3 行第 3 列中获取值?

MySQL:如何在不运行多个查询的情况下从由预设前缀和父表字段值组成的子表名中选择行

linux - bash脚本参数所有以指定字符串开头的文件

linux - 在版本名称和版本号之间进行过滤

java - 如果列表已经存在,如何将列表插入到表中而不重复它们(使用 Spring 和 Hibernate)

linux - 如果没有停止,需要在 1 小时内重新启动服务器脚本

ubuntu - ipTables 阻止 memcached

linux - CVS 无法识别在 .bashrc 和 .bash_profile 中设置了 CVSROOT

Django寻找模板文件的错误路径

mysql - 'SELECT' 语句中的 'IF' - 根据列值选择输出值