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

 
(사용자 2명의 중간 판 3개는 보이지 않습니다)
7번째 줄: 7번째 줄:
[[category: Bash]]
[[category: Bash]]
* 순수 bash
* 순수 bash
<source lang='bash'>
<syntaxhighlight lang='bash'>
function is_ip() {
function is_ip() {
IFS='.' read -ra nums <<< "$1"
IFS='.' read -ra nums <<< "$1"
29번째 줄: 29번째 줄:
is_ip 0.0.0.00
is_ip 0.0.0.00
is_ip 135.79.246.080
is_ip 135.79.246.080
</source>
</syntaxhighlight>
* 신버전 [[ipcalc]] 활용(v0.41)
* 신버전 [[ipcalc]] 활용(v0.41)
<source lang='bash'>
<syntaxhighlight lang='bash'>
echo $((`ipcalc 135.79.246.80 | grep INVALID | wc -l`^1))
echo $((`ipcalc 135.79.246.80 | grep INVALID | wc -l`^1))
# 1
# 1
echo $((`ipcalc 135.79.246.280 | grep INVALID | wc -l`^1))
echo $((`ipcalc 135.79.246.280 | grep INVALID | wc -l`^1))
# 0
# 0
</source>
</syntaxhighlight>
* 구버전 [[ipcalc]] 활용(2008)
* 구버전 [[ipcalc]] 활용(2008)
<source lang='bash'>
<syntaxhighlight lang='bash'>
ipcalc -s -c 135.79.246.80; echo $(($?^1))
ipcalc -s -c 135.79.246.80; echo $(($?^1))
# 1
# 1
ipcalc -s -c 135.79.246.280; echo $(($?^1))
ipcalc -s -c 135.79.246.280; echo $(($?^1))
# 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()}}
{{참고|PHP is_ip()}}
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php' run>
function is_ip($ip) { return ip2long($ip)?true:false; }
function is_ip($ip) { return ip2long($ip)?true:false; }


59번째 줄: 85번째 줄:
var_dump( is_ip('1.1.1.256') );
var_dump( is_ip('1.1.1.256') );
var_dump( is_ip('135.79.256.080') );
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>


==같이 보기==
==같이 보기==

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 }}