I need to verify if a string is a valid IPv4 or IPv6 address in a Batch script, but apparently Batch doesn't have an easy way to parse an IP address.
How can I do this in Batch without using external tools? By "external tools" I mean things that aren't already present on a normal Windows installation.
By "valid IPv4 or IPv6 address" I mean a string that is in the format of an IP address, false positives with strings like 999.999.999.999
are OK (even if some basic filtering is welcome). As a rule of thumb, a solution should at least be able to distinguish an error message from an address.
I know that a similar question already exists, but that question doesn't take IPv6 into consideration and it's more strict in its definition of "valid IPv4 address" (something like 999.999.999.999
isn't acceptable).
Given a string S consisting of N characters, the task is to check if the given string S is IPv4 or IPv6 or Invalid. If the given string S is a valid IPv4, then print “IPv4”, if the string S is a valid IPv6, then print “IPv4”. Otherwise, print “-1”. A valid IPv4 address is an IP in the form “x1.
It is relatively easy: a vaild IPv6 address has no characters other than the 16 hex-digits 0-9 and a-f or A-F in it. A valid IPv6 address consists of exactly 8 blocks of hex-digits with a ":" between the blocks.
The simplest way to validate if a string represents an IP address is by using the Python ipaddress module. Let's open the Python shell and see what the ipaddress. ip_address() function returns when we pass to it strings that represent a valid and an invalid IPv4 address.
We can use InetAddressValidator class that provides the following validation methods to validate an IPv4 or IPv6 address. isValid(inetAddress) : Returns true if the specified string is a valid IPv4 or IPv6 address. isValidInet4Address(inet4Address) : Returns true if the specified string is a valid IPv4 address.
Check for valid IPv4:
@if (@X)==(@Y) @end /* JScript comment @echo off cscript //E:JScript //nologo "%~f0" %* exit /b %errorlevel% @if (@X)==(@Y) @end JScript comment */ WScript.Quit(ValidateIPaddress(WScript.Arguments.Item(0))); function ValidateIPaddress(ipaddress) { return !(/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress)) }
For valid IPv6 address:
@if (@X)==(@Y) @end /* JScript comment @echo off cscript //E:JScript //nologo "%~f0" %* exit /b %errorlevel% @if (@X)==(@Y) @end JScript comment */ WScript.Quit(ValidateIPaddress(WScript.Arguments.Item(0))); function ValidateIPaddress(ipaddress) { return !(/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/gm.test(ipaddress)) }
Both are relying on the exit code, so here's how they can be used:
call validIPV4.bat 12.12.12.12 && ( echo valid ipv4 ) || ( echo invalid ipv4 ) call validIPV4.bat 12.12.12.6000 && ( echo valid ipv4 ) || ( echo invalid ipv4 )
The first will print valid ipv4
the second invalid ipv4
.
Or you can check the errorlevel:
call validIPV6.bat "1200:0000:AB00:1234:0000:2552:7777:1313" if %errorlevel% equ 0 ( echo valid ipv6 ) else ( echo invalid ipv6 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With