0

I would like a script to look for an IP address among others.

I use the following command to generate the list:

ip -4 neighbor show

But I don't know how to look.

command | awk ...

or

for i in command ; do ... done

Finally this seems to be correct:

LIST=$(the regex of Reda Salih)  
for i in $LIST
do
  if [ "${i}" == $my_ip]
  then
    echo found
    exit 0
  fi
done
echo not found

But is there a more elegant solution?

2
  • It would be easier if you show us the output of the command and an example of the desired output of the script. Please edit your question. – BANJOSA Nov 17 '20 at 14:41
  • The command "ip -4 neighbor show" provides a list of IP. My desired output is True or false according to the presence or not of a specific IP. – jpc Nov 17 '20 at 16:04
0

You can use grep with following regex :

ip -4 neighbor show | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" | grep -oh <ip_addr>

Just replace <ip_addr> with your IP..

5
  • Ok but how to find a specific IP is present in this list ? – jpc Nov 17 '20 at 16:06
  • I have updated the post. Don't forgot to make it as resolved / answered. Thanks – Reda Salih Nov 17 '20 at 16:50
  • if <ip_addr> is 192.168.1.1 your solution find 192.168.1.1 and 192.168.1.10 – jpc Nov 17 '20 at 17:04
  • I have added '-oh' flags to the last grep, so it will show only the matched word. It will be fine now ! – Reda Salih Nov 17 '20 at 18:17
  • OK Thank you for your solution – jpc Nov 17 '20 at 21:56

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.