【问题标题】:Read IP address from text file to IPTABLES从文本文件中读取 IP 地址到 IPTABLES
【发布时间】:2017-02-02 08:18:58
【问题描述】:

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

sudo ufw allow from (my ip address)

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

这是我得到的价值

  ipaddress
 10.1.100.90
 10.1.100.91

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

【问题讨论】:

  • 问题要求发布一次后不得再修改,已提供答案

标签: mysql bash ubuntu iptables ufw


【解决方案1】:

您可以使用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

【讨论】:

  • 最后的&lt;IPFile是什么?
  • @Fouzy: 那是文件,你有 IP 存储在里面。把它改成你的实际文件名
  • @Fouzy:没错。您需要根据需要使用$IP
  • 如何忽略“名称”列?
  • @Fouzy:你为什么要这么做?只需访问 $IP 变量中的那些内容
【解决方案2】:

你可以用python编写一个简单的脚本,可以读取包含IP地址列表的文本文件,然后对每个IP地址一一执行Linux命令。简单指南Python Execute Unix / Linux Command Examples

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 2012-02-17
相关资源
最近更新 更多