1

My goal is to ensure that email connections are limited to outbound only

So I have two shell commands one is

netstat -an | grep ':25' | grep -v '127.0.0.1' | wc -l

which prints out 1

The command up above is finding all instances of port 25 besides localhost but does not exclude ipv6

The other command is

netstat -an | grep ':25' | grep -v '127.0.0.1\|::1' | wc -l

which prints out 0

This command up above is finding all instances of port 25 and excluding localhost and ipv6

I need an if else that compares the two and says if it comes back 0 print out compliant else print out non-compliant

What is the best approach in doing that through either ansible or shell?

  • And what is the point of this? The connection is already established when you find out about the connection. The connection may be even already closed and the mail sent. – RalfFriedl Aug 6 '18 at 17:17
  • Its more for the person looking at and giving info about the connections. So that the person can see that when it finds every instance of port 25 and excludes localhost and ipv6 its compliant and else its not. – W.Smith Aug 6 '18 at 17:22
0

Using awk:

netstat -an | grep ':25' | grep -v '127.0.0.1\|::1' | wc -l | awk '{if($0==0) print "compliant"; else print "non-compliant"}'
  • will print complaint if output $0 is 0
  • else print non-complaint
2

Here is how I would handle it.

#!/bin/bash

if netstat -an | grep ':25' | grep -q -v '127.0.0.1\|::1'
then echo non-compliant
else echo compliant
fi

Bash if statements can handle return codes. If anything makes it through it will return a positive status and echo non-compliant, otherwise it's compliant

If that logic seems confusing you could also invert the status with a !

if ! netstat -an | grep ':25' | grep -q -v '127.0.0.1\|::1'
then echo compliant
else echo non-compliant
fi
  • @Kusalananda, nifty. Thanks for that one. – Zachary Brady Aug 6 '18 at 16:27

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.