简体   繁体   中英

Read IP address from text file to IPTABLES

Currently I am using ufw to manually add the ip rules by typing

sudo ufw allow from (my ip address)

I have retrieve the ip address of my client ip by doing some script calling from my database and save it to a .txt file.

this is the value i get

  ipaddress
 10.1.100.90
 10.1.100.91

Is it possible to read the ip address in the .txt file and save it to the rule instead of typing manually?

您可以在python中编写一个简单的脚本,该脚本可以读取包含IP地址列表的文本文件,然后在每个IP地址上依次执行Linux命令。简单指南Python执行Unix / Linux命令示例

You could use a bash script like something below

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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