1

I am trying to compare two strings. One value is coming from iptables and the other is from a file where I saved previous records.

I want to identify if there is a new user appear in iptables. If so I will add that record to my file.

Here what I have tried so far.

chain=$(/usr/sbin/iptables -vnL | grep "references" | awk '{print $2}')

file=/data/..../2011-11.txt


for i in $chain
do
         while IFS=',' read -r f1 f2 f3 f4 f5
         do
              user=$f1

              if [ $i == "$user" ]; then
                  echo "user matched $user"
              elif [ $i != $user ]; then      #a new user
                  echo "new user $i"
                  break
              fi   #user matching if     

         done < "$file"

done    #reading data from the file for loop

My iptables looks like (chain)

NalindaChainksSdt
KasunikaChainyCehf
KivinduChainmzWeN
KumudikaChainsXcAp
LocalServerChainRyqvv

File values are (file)

NalindaChainksSdt
KasunikaChainyCehf

Here script should ignore NalindaChainksSdt and KasunikaChainyCehf names from chains. (user matched)

But this does not do the trick. It just identify all users in the file as new users.

What am I missing here?

2
  • 1
    Can you give the content examples of $chain and $file variables? Nov 10, 2017 at 6:32
  • @EgorVasilyev Yes, question was edited and add some samples. Nov 10, 2017 at 6:40

2 Answers 2

2

First of all, it never makes sense to say

if condition; then
    do something
elif condition is false; then
    do something different
fi

Because, if condition isn’t true, then you know that it’s false.  You can just say

if condition; then
    do something
else
    do something different
fi

Now think about the logic of your script.  It’s an n2 algorithm — or, to be more precise, if $chain is m words long, and the file is n lines long, then the code can potentially loop m×n times.  Now think about what that means for your problem:

You’re saying that $i is a new user if you find any user ($user) in the file that’s different.  But $i is a new user only if every user in the file is different from $i.

1
  • Yes, that's what I want to do, if user from chain and file matched, then need to exit from that comparison and goes to the next user from chain. Nov 10, 2017 at 7:02
1

I can offer the simpliest solution with comm util:

#!/bin/bash

/usr/sbin/iptables -vnL | grep "references" | awk '{print $2}' > /tmp/chain

chain="/tmp/chain"
file="full path to 2011-11.txt"

comm -13 "$file" "$chain"

If 2011-11.txt contains:

NalindaChainksSdt
KasunikaChainyCehf

And chain contains:

NalindaChainksSdt
KasunikaChainyCehf
KivinduChainmzWeN
KumudikaChainsXcAp
LocalServerChainRyqvv

Then output will be:

KivinduChainmzWeN
KumudikaChainsXcAp
LocalServerChainRyqvv
7
  • I tried, but no luck. Nov 10, 2017 at 8:27
  • @Sachith, what problems did you get? Nov 10, 2017 at 8:30
  • Nothing is happening. Can you give me a working example. Nov 10, 2017 at 9:44
  • @Sachith, you need to replace full path to 2011-11.txt to real file path. i edited my answer to remove relative file path. Execute it again. Nov 10, 2017 at 9:57
  • comm: : No such file or directory Nov 10, 2017 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.