How to check if any IP address is present in a file using shell scripting?

Yes , You have lot of options/tools to use. I just tried this , it works:

ifconfig | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"

so you can use grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" to grep the ip addresses from your output.


If your file is called e.g ips you can write somethinng like:

while read -r ip
    do
        if [[ $ip == "$1" ]]; then
            shift
            printf '%s\n' 'action to take if match found'
        else
            printf '%s\n' 'action to take if match not found'
        fi
    done < ips

Then you can pass the parameters as follow the the script

./myscript 159.143.23.12 134.12.178.131 124.143.12.132 124.143.12.132

starting my answer based on this answer:

Yes , You have lot of options/tools to use. I just tried this , it works:

ifconfig | grep -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" a so you can use grep -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b" to grep the ip addresses from your output.

and converting the answer to full length IPv6, etc...:

fgrep -oE "\b([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}\b" -- file

if you want to keep the /nnn if it's there:

fgrep -oE "\b([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}(/[0-9]{1,3}){0,1}\b" -- file

and also there's the shortened version of IPv6 that includes '::'.

for more IPv6 answers you can look here: https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses