"함수 is ip()"의 두 판 사이의 차이

 
(사용자 3명의 중간 판 21개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: IP]]
[[category: validation]]
[[category: validation]]
;is_ip
{{DISPLAYTITLE:함수 is_ip()}}
;함수 is_ip()


==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
* 순수 bash
IP=135.79.246.80
<syntaxhighlight lang='bash'>
IS_IP=`ipcalc -s -c $IP; echo $(($?^1))`
function is_ip() {
echo $IS_IP
IFS='.' read -ra nums <<< "$1"
if [ ${#nums[@]} -ne 4 ]; then echo 0; return; fi
for num in "${nums[@]}"; do
if [ "$num" != "$((10#$num))" ]||[ $num -lt 0 ]||[ $num -gt 255 ]; then echo 0; return; fi
done
echo 1
}
 
# 1
is_ip 0.0.0.0
is_ip 0.0.0.1
is_ip 0.0.1.0
is_ip 255.255.255.255
is_ip 135.79.246.80
 
# 0
is_ip 0.0.0.256
is_ip 0.0.0.0.0
is_ip 0.0.0.00
is_ip 135.79.246.080
</syntaxhighlight>
* 신버전 [[ipcalc]] 활용(v0.41)
<syntaxhighlight lang='bash'>
echo $((`ipcalc 135.79.246.80 | grep INVALID | wc -l`^1))
# 1
echo $((`ipcalc 135.79.246.280 | grep INVALID | wc -l`^1))
# 0
</syntaxhighlight>
* 구버전 [[ipcalc]] 활용(2008)
<syntaxhighlight lang='bash'>
ipcalc -s -c 135.79.246.80; echo $(($?^1))
# 1
# 1
</source>
ipcalc -s -c 135.79.246.280; echo $(($?^1))
<source lang='bash'>
IP=135.79.246.080
IS_IP=`ipcalc -s -c $IP; echo $(($?^1))`
echo $IS_IP
# 0
# 0
</source>
</syntaxhighlight>
 
==Go==
{{참고|Go isIP()}}
[[분류: Go]]
<syntaxhighlight lang='go' run>
package main
 
import (
"fmt"
"net"
)
 
func isIP(ip string) bool {
return net.ParseIP(ip) != nil
}
 
func main() {
// true
fmt.Println(isIP("10.40.210.253"))
fmt.Println(isIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))
 
// false
fmt.Println(isIP("1000.40.210.253"))
fmt.Println(isIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334:3445"))
}
</syntaxhighlight>


==PHP==
==PHP==
{{참고|PHP is_ip()}}
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php' run>
echo ip2long('135.79.246.80') === false ? 'NO' : 'YES';
function is_ip($ip) { return ip2long($ip)?true:false; }
// YES
 
echo ip2long('135.79.246.080') === false ? 'NO' : 'YES';
# true
// NO
var_dump( is_ip('1.1.1.1') );
</source>
var_dump( is_ip('135.79.246.80') );
<source lang='php'>
 
function is_ip($ip) {
# false
  if( ip2long($ip) === false) return false;
var_dump( is_ip('1.1.1.1.') );
  return true;
var_dump( is_ip('1.1.1.256') );
}
var_dump( is_ip('135.79.256.080') );
</source>
</syntaxhighlight>
 
==Python==
{{참고|파이썬 is_ip()}}
[[분류: Python]]
<syntaxhighlight lang='python'>
import ipaddress
def is_ip(ip):
    try:
        ipaddress.ip_address(ip)
    except ValueError:
        return False
    return True
   
# True
print( is_ip('1.1.1.1') )
print( is_ip('192.168.0.1') )
print( is_ip('135.79.246.80') )
 
# False
print( is_ip('1.1.1.1.') )
print( is_ip('1.1.1.256') )
print( is_ip('192.168.0.256') )
print( is_ip('135.79.256.080') )
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[ip2long]]
* [[함수 is_private_ip()]]
*[[ipcalc]]
* [[함수 is_ip_substring()]]
*[[IP 주소]]
* [[함수 ip2long()]]
* [[ipcalc]]
* [[IP 주소]]

2023년 4월 7일 (금) 19:31 기준 최신판


함수 is_ip()

1 Bash[ | ]

  • 순수 bash
function is_ip() {
	IFS='.' read -ra nums <<< "$1"
	if [ ${#nums[@]} -ne 4 ]; then echo 0; return; fi
	for num in "${nums[@]}"; do
		if [ "$num" != "$((10#$num))" ]||[ $num -lt 0 ]||[ $num -gt 255 ]; then echo 0; return; fi
	done	
	echo 1
}

# 1
is_ip 0.0.0.0
is_ip 0.0.0.1
is_ip 0.0.1.0
is_ip 255.255.255.255
is_ip 135.79.246.80

# 0
is_ip 0.0.0.256
is_ip 0.0.0.0.0
is_ip 0.0.0.00
is_ip 135.79.246.080
  • 신버전 ipcalc 활용(v0.41)
echo $((`ipcalc 135.79.246.80 | grep INVALID | wc -l`^1))
# 1
echo $((`ipcalc 135.79.246.280 | grep INVALID | wc -l`^1))
# 0
ipcalc -s -c 135.79.246.80; echo $(($?^1))
# 1
ipcalc -s -c 135.79.246.280; echo $(($?^1))
# 0

2 Go[ | ]

package main

import (
	"fmt"
	"net"
)

func isIP(ip string) bool {
	return net.ParseIP(ip) != nil
}

func main() {
	// true
	fmt.Println(isIP("10.40.210.253"))
	fmt.Println(isIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"))

	// false
	fmt.Println(isIP("1000.40.210.253"))
	fmt.Println(isIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334:3445"))
}

3 PHP[ | ]

function is_ip($ip) { return ip2long($ip)?true:false; }

# true
var_dump( is_ip('1.1.1.1') );
var_dump( is_ip('135.79.246.80') );

# false
var_dump( is_ip('1.1.1.1.') );
var_dump( is_ip('1.1.1.256') );
var_dump( is_ip('135.79.256.080') );

4 Python[ | ]

import ipaddress
def is_ip(ip):
    try:
        ipaddress.ip_address(ip)
    except ValueError:
        return False
    return True
    
# True
print( is_ip('1.1.1.1') )
print( is_ip('192.168.0.1') )
print( is_ip('135.79.246.80') )

# False
print( is_ip('1.1.1.1.') )
print( is_ip('1.1.1.256') )
print( is_ip('192.168.0.256') )
print( is_ip('135.79.256.080') )

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}