Skip to content
Advertisement

Bash parse output in route command

I am writing a bash script to register the remote hosts in a wireless network. I am getting the remote IPs, but I need help to parse the output. Here’s my output:

$ IPS=`route -n | grep -e '10.0.0' | awk '{ print $1 }'`
$ echo $IPS
10.0.0.2
10.0.0.3

I need to get these IPs and insert into a command like ... <remote_ip_1> ...

How can it be done? I couldn’t find this case in the answers.

Thank you

Advertisement

Answer

You could use something like

route -n | awk '/10.0.0/ { system("command to be executed with "$1) }'
                                                                  ^
                                                             added missing )

We are negating the use of grep and using // to search for the text required and then the awk system function to execute a command along with $1

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement