Search code examples
stringbashoutputverifyufw

2 equal strings (expected status and current status) evaluate to false


I have a **bash script to setup UFW with an expected output as follows:

user@host:~$ sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     LIMIT IN    Anywhere                  
80/tcp                     ALLOW IN    Anywhere                  
443/tcp                    ALLOW IN    Anywhere                  
22/tcp (v6)                LIMIT IN    Anywhere (v6)             
80/tcp (v6)                ALLOW IN    Anywhere (v6)             
443/tcp (v6)               ALLOW IN    Anywhere (v6) 

I created a test function to verify the output is as expected. I copied the ouput manually into a variable and stored the output in another variable so i could compare the 2 as follows :

function test() {
    # Verify that output is as excpeted
    local output="$(sudo ufw status verbose)"
    local expected="Status: active Logging: on (low) Default: deny (incoming), allow (outgoing), disabled (routed) New profiles: skip To Action From -- ------ ---- 22/tcp LIMIT IN Anywhere 80/tcp ALLOW IN Anywhere 443/tcp ALLOW IN Anywhere 22/tcp (v6) LIMIT IN Anywhere (v6) 80/tcp (v6) ALLOW IN Anywhere (v6) 443/tcp (v6) ALLOW IN Anywhere (v6)"

    # echo
    # echo $output
    # echo
    # echo $expected
    # echo

    if [[ "$output" == "$expected" ]]; then
        echo "UFW setup: output was as expected"
    else
        echo "UFW setup: output NOT as expected!"
        echo "Please check UFW status before coninuing:"
        echo
        sudo ufw status verbose
    fi

Output:

luc@E580-debian:~$ test
UFW setup: output NOT as expected!
Please check UFW status before coninuing:

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     LIMIT IN    Anywhere                  
80/tcp                     ALLOW IN    Anywhere                  
443/tcp                    ALLOW IN    Anywhere                  
22/tcp (v6)                LIMIT IN    Anywhere (v6)             
80/tcp (v6)                ALLOW IN    Anywhere (v6)             
443/tcp (v6)               ALLOW IN    Anywhere (v6)             

As far as i know they are exactly the same, but I also dont know if there is a way to check if hidden characters are causing this. Or maybe if there is a way to remove/replace them.


Solution

  • You are comparing the hard way. You don't know anything about /tabs /spaces in UFW output. This would be more professional way to do it. Please adjust for your needs:

    vim ./check_ufw.sh
    
    #!/usr/bin/env bash
    #:: Author: pr0logas
    #:: Date: 2020-08-20
    #:: Description: This program checks if the UFW firewall rules have not changed.
    
    expected_output_file='/tmp/ufw_original.txt'
    current_output_file='/tmp/ufw_current.txt'
    
    echo "Just pushing some text as original UFW input to make FAILURE" > $expected_output_file
    
    function check_diff() {
            ufw status numbered verbose > $current_output_file
            diff -q $expected_output_file $current_output_file 1> /dev/null
    
            if [[ $?  == "0" ]]; then
                    echo "SUCCESS! UFW rules are the same."
            else
                    echo "FAIL! UFW rules differ, attention needed!"
                    exit 1
            fi
    }
    
    check_diff
    

    To be 100% sure for the desired output I suggest you copy current output as original input for the first time like this:

    cat /tmp/ufw_current.txt > /tmp/ufw_original.txt
    

    Then comment:

    #echo "Just pushing some text as original UFW input to make FAILURE" > $expected_output_file