0

I have at my ubuntu server an iptables based firewall, which is running good. Now I want to see (echo) at my screen (CLI) certain related output. F.i. I give some persons access through port 22 and want to echo their names and ip in the CLI (when I run the firewall). Let's say:

peter="123.456.78.90"
mike="123.456.78.91"
port_ssh="22"
access-port-ssh="$peter $mike"

Using the firewall code, this is all ok and they have access through port 22. I just want to echo a line in the CLI, which shows:

IP 123.456.78.90 (peter) has access to server, port 22

and during the next run of the loop in my firewall:

IP 123.456.78.91 (mike) has access to server, port 22

The only thing I want to know is how do I get the name "peter" nad "mike" at my screen since the rest I know. The loop is as follows, and the question is: which code should I use to replace the "XXXX" (in second-last line) by "peter" and next run by "mike"

ALLOWED=access-port-ssh; ALLOWED_IPS=${!ALLOWED} 
for IP in ${ALLOWED_IPS}; do
  ${IPTABLES} -A INPUT  -i ${LAN_IFACE} -p tcp -s ${IP} --dport ${port-ssh} --sport ${port-ssh} -j ACCEPT
  ${IPTABLES} -A OUTPUT -o ${LAN_IFACE} -p tcp -d ${IP} --sport ${port-ssh} --dport ${port-ssh} -j ACCEPT
  echo "${IP} (XXXX) has access to server, port: ${port-ssh}"
done

Edit post to make it more clear (without the whole context): how can I echo the variable names ("peter" and "mike"), when using only variable "access-port-ssh". I tried:

peter="123.456.78.90"
mike="123.456.78.91"
access="$peter $mike"
allowed=access; allowed=${!allowed}

for i in ${allowed}; do variable_name=(${!i@}); echo $variable_name; done
ni_hao
  • 404
  • 2
  • 5
  • 16
  • Does the user's name appear in the output of `${IPTABLES} ...`? Then you can pipe the cmd to `awk '{print $6}'` (or which ever field number contains the value). Otherwise, it's not clear how you can get those names. Grep a log file for the IP address and `awk` to isolate the output? Good luck. – shellter Sep 14 '18 at 15:04
  • they are not in $IPTABELS. I use their first names as variable which includes the IP. Then I create a new variable (access-port-ssh) containing those who will have access: $peter $mike. In $IPTABLES I use that variable (access-port-ssh) and not the ones with the first name. Now I want to echo those first names. – ni_hao Sep 14 '18 at 17:56
  • Sorry, but for me,(and it seems other readers) verbal descriptions are often ambiguous. I find it more helpful to include 1. small sets of sample input (in this case the output of your `${IPTABLES} -A ...` etc commands (what is the output from them), 2. Your required output given those sample inputs. 3. your current code, your current output and error messages. Consider updating your Qs with actual evidence, and we may be able to help. Good luck! – shellter Sep 14 '18 at 19:31
  • It has nothing to do with iptables; it is an echo issue. Sorry for maybe not clear enough. I added at the bottom of the post a part to make it more clear (I hope) – ni_hao Sep 15 '18 at 07:14
  • You have a mapping of name to IP. You need a mapping of IP to name. You could use env parsing gymnastics to get it, but better if you stored the mapping in an array like structure: array proper, config file, etc. As is, with individual variables, the solution will be hackey. – bishop Sep 15 '18 at 13:02
  • A possible solution is, that you use [associative arrays](https://stackoverflow.com/questions/3112687/how-to-iterate-over-associative-arrays-in-bash) with "Peter" and "MIke" as the keys and the IPs as the values – JGK Sep 16 '18 at 09:46

0 Answers0