Bash not interpreting line as usable string for UFW

20 views Asked by At

I am trying to block Digital Ocean IP addresses automatically based on an API request. It creates a text file that simply looks like:

103.253.144.0/22
104.131.0.0/18
104.131.128.0/20
104.131.144.0/20

If I manually type the command:

$ sudo ufw insert 1 deny from 103.253.144.0/22 to any

The result is

Rule inserted

In my simple bash script when running the following:

#!/bin/bash

while IFS= read -r line; do
    echo "Inserting: $line"
    ufw insert 1 deny from "$line" to any
    sleep 2
    exit 1
done < ocean.txt

I get

Inserting: 103.253.144.0/22
ERROR: Bad source address

I am assuming that bash is interpreting $line incorrectly .. I even thought it might be an array element, so I tried "{{ line }}" as well to no avail. What am I doing wrong here? this seems so basic!

Here is the output of bash -x

+ IFS=
+ read -r line
' echo 'Inserting: 103.253.144.0/22
Inserting: 103.253.144.0/22
+ ufw insert 1 deny from $'103.253.144.0/22\r' to any
ERROR: Bad source address
+ sleep 2
+ exit 1
0

There are 0 answers