Search code examples
mysqlbashubuntuiptablesufw

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?


Solution

  • 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