1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#!/bin/bash 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 return $stat } if [[ "$(basename $0 .sh)" == 'valida_ip' ]]; then ips=' 4.2.2.2 a.b.c.d 192.168.1.1 0.0.0.0 255.255.255.255 125.e.12.45 255.255.255.256 192.168.0.1 192.168.0 1234.123.123.123 ' for ip in $ips do if valid_ip $ip; then stat='correcto'; else stat='malo'; fi printf "%-20s: %s\n" "$ip" "$stat" done fi |