Bash-check-if-ip-address: Difference between revisions

From I Will Fear No Evil
Jump to navigation Jump to search
(Created page with "this is a VERY simple check to see if you have an IP address as your variable <pre> function valid_ip() { local ip=$1 local stat=1 if [[ $ip =~ ^[0-9]{1,3}\.[0-...")
 
mNo edit summary
 
Line 1: Line 1:
<metadesc>A simple check to confirm if something is an IP address or not.</metadesc>
this is a VERY simple check to see if you have an IP address as your variable
this is a VERY simple check to see if you have an IP address as your variable
<pre>
<pre>

Latest revision as of 15:01, 25 April 2024

this is a VERY simple check to see if you have an IP address as your variable

function valid_ip() {
    local  ip=$1
    local  stat=1

    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
        OIFS=$IFS
        IFS='.'
        ip=($ip)
        IFS=$OIFS
        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
            && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
        stat=$?
    fi
    echo "$stat"
    return $stat
}