0

How can I extract to a variable both the address (IPv4 or IPv6) and the [number] with grep from ufw status numbered?

Without using regex to find the IP.

$ufw status numbered | grep -w 'DNS1'
[277] 53                         ALLOW       216.218.130.2              # DNS1

$ufw status numbered | grep -w 'DNS2'
[288] 53                         ALLOW       2001:470:100::2            # DNS2
Nikk
  • 7,384
  • 8
  • 44
  • 90
  • can you add the exact output you want? also, if the fields are always the same, `awk` would be better suited – Sundeep May 12 '22 at 15:17
  • @Sundeep I want to use it in a bash scrip. So it should assign both the number and the IP to a variable. Regarding the output from `ufw status numbered` is the same. Port number could be different, or ALLOW could be DENY. But they are at the same place in the string. – Nikk May 12 '22 at 15:25
  • In [the comment](https://stackoverflow.com/questions/72218132/grep-awk-extract-ipv4-or-ipv6-from-ufw-status/72218586?noredirect=1#comment127659020_72218586) you left under my question, the input you showed had `ALLOW IN` in the middle, not just `ALLOW`. If the comment was correct then [edit] your question to fix that. – Ed Morton May 16 '22 at 13:16

1 Answers1

1

Using cat file in place of ufw status numbered which I don't have:

$ portip=( $(cat file | awk -v key='DNS1' '$NF==key{gsub(/[][]/,""); print $1, $4}') )
$ echo "${portip[0]}"
277
$ echo "${portip[1]}"
216.218.130.2
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Using awk to filter doesn't return anything. Extracting the data works `awk '{gsub(/[][]/,""); print $1, $5}'` – Nikk May 14 '22 at 18:59
  • As you can see in my answer, when you run the script I provided against the input you provided it produces the output you said you wanted. If it's not doing that for you now then either you copy/pasted it wrong or your real input doesn't look like the example in your question. – Ed Morton May 14 '22 at 20:19
  • `root@dns:/home/user# ufw status numbered | awk -v key='SSH' '$NF==key{gsub(/[][]/,""); print $1, $5}' root@dns:/home/user# root@dns:/home/user# ufw status numbered | awk -v key='SSH' '$NF==key{gsub(/[][]/,""); print $1, $4}' root@dns:/home/user# ufw status numbered Status: active To Action From -- ------ ---- [ 3] 22 ALLOW IN 2.2.2.2 # SSH [ 4] 53 ALLOW IN 1.2.2.3 # PowerDNS` – Nikk May 16 '22 at 09:27
  • This is the output I get. Does not filter. Like this it works: `awk '{gsub(/[][]/,""); print $1, $5}'` – Nikk May 16 '22 at 09:27
  • Then the final string on your target line must not be `SSH`. Check for DOS line endings, see https://stackoverflow.com/questions/45772525/why-does-my-tool-output-overwrite-itself-and-how-do-i-fix-i, as those being present would mean that last input string is `SSH\r` instead of `SSH`. Meanwhile change `$NF==key` to `$NF~key` to do a partial regexp instead of fixed string match on that last field and, whether that works or not, edit your question to show the output of `ufw status numbered | cat -v` so we can see the CRs and LFs so we can provide the best answer for your actual input. – Ed Morton May 16 '22 at 13:13