VBScript - 验证输入是 IP,每八位字节 3 位
VBScript - Verify input is IP, 3-digits per octet
我是一名学习 VBScript 的学生。前几天[grin],才开始接触BASIC、DOS和Machine Code编程,所以基本概念不是很陌生,只是有点。
警告 - 这是评分作业的一部分,主要要求指导和理解代码。
正在尝试为用户输入 IP 地址编写脚本,其中每个八位字节均为 3 位格式(如有必要,前导零)。这是起始输入
' Get IP address to convert
WScript.StdOut.Write("Please Enter your IP address (3 digits per octet) -> ")
strIP = WScript.StdIn.ReadLine()
正在尝试进行错误检查并遇到问题。尝试了各种方法,但 none 似乎提供了所需的输出,如果在 运行.
时没有失败并出现各种 VBScript 错误
版本 1:
Dim sMatch(15)
if sMatch = strIP Like "###.###.###.###" Then
Wscript.Echo "The strings are equal."
Else
Wscript.Echo "The strings are not equal."
' WScript.Echo sMatch
End If
版本 2:
String str = StrComp("strIP", ""\###.\###.\###.\###"",vbTextCompare)
If str < 0 or str > 0 then WScript.StdOut.WriteLine "Improper IP format."
版本 3:
iFirstOct = Left(strIP,3)
z1 = strComp(iFirstOct, "###", 1)
WScript.Echo "z1 -> " & z1
WScript.Echo "IP -> " & iFirstOct
If z1 < 1 or z1 > 1 Then
WScript.Echo " Error in input !"
WScript.quit
End If
我有点喜欢版本 3,因为我可以将其作为子例程,但不确定哪个是可行的选择。任何帮助确定我在哪里出错以及如何出错的帮助,我们将不胜感激。
警告 - 这是评分作业的一部分,主要要求指导和理解代码。
-- 编辑 - 21 年 2 月 16 日 --
埃蒂安,
这是最后的脚本。我有 运行 不同的错误场景,它似乎按照我需要的方式运行。感谢您的协助。
' IP_func.vbs
'
' By: Étienne Laneville
' Minor modifications by DragonRider
' From:
'
'
WScript.StdOut.Write("Please Enter your IP address (3 digits per octet) -> ")
strIP = WScript.StdIn.ReadLine()
WScript.Echo vbCrLf & " Input -> " & vbTab & strIP & vbCrlf
' Call error-checking function
CheckIP(strIP)
Function CheckIP(strIP)
Dim arrOctets
Dim iCounter
Dim sOctet
' Check for periods
If InStr(strIP, ".") = 0 Then
CheckIP = False
WScript.Echo " Incorrect number of octets. Please try again."
Exit Function
End If
' Split string on periods
arrOctets = Split(strIP, ".")
' Check that correct number of octets has been received
If UBound(arrOctets) <> 3 Then
CheckIP = False
WScript.Echo " Incorrect number of octets. Please try again."
Exit Function
End If
' Check each octet
For iCounter = 1 To 4
sOctet = arrOctets(iCounter - 1)
' Check if it's numeric
If Not IsNumeric(sOctet) Then
CheckIP = False
WScript.Echo " Improper IP Address. Please try again."
Exit Function
End If
' Check if 3 digits
' Dim three As String
If Not Len(sOctet) = 3 Then
CheckIP = False
WScript.Echo " 3-Digits per octet. Please try again."
Exit Function
End If
' Check for proper values
If CInt(sOctet) > 255 Then
CheckIP = False
WScript.Echo " Octet out of range. Please try again."
Exit Function
End If
If CInt(sOctet) < 0 Then
CheckIP = False
WScript.Echo " Improper IP Address. Please try again."
Exit Function
End If
Next
CheckIP = True
End Function
这是一个简单的函数,可以将事物分解为简单的操作:
Function CheckIP(strIP)
Dim arrOctets
Dim iCounter
Dim sOctet
' Check for periods
If InStr(strIP, ".") = 0 Then
CheckIP = False
Exit Function
End If
' Split string on periods
arrOctets = Split(strIP, ".")
' Check that correct number of octets has been received
If UBound(arrOctets) <> 3 Then
CheckIP = False
Exit Function
End If
' Check each octet
For iCounter = 1 To 4
sOctet = arrOctets(iCounter - 1)
' Check if it's numeric
If Not IsNumeric(sOctet) Then
CheckIP = False
Exit Function
End If
' Check for proper values
If CInt(sOctet) > 255 Then
CheckIP = False
Exit Function
End If
If CInt(sOctet) < 0 Then
CheckIP = False
Exit Function
End If
Next
CheckIP = True
End Function
您还可以通过此功能使用正则表达式检查 IP 地址的有效性:
Option Explicit
Dim Title,IP
Title = "Test the validity of an IP address"
IP = InputBox("Please enter an IP Address",Title,"172.16.18.21")
If Is_Valid(IP) = True Then
MsgBox IP & " is a Valid IP Address",64,Title
Else
MsgBox IP & " is Not a Valid IP Address",16,Title
End if
'------------------------------------------------------------------------------------
Function Is_Valid(ip)
Dim RegularExpressionObject
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"
.IgnoreCase = False
If .Test(ip)= True then
Is_Valid = True
end if
End With
End Function
'------------------------------------------------------------------------------------
我是一名学习 VBScript 的学生。前几天[grin],才开始接触BASIC、DOS和Machine Code编程,所以基本概念不是很陌生,只是有点。
警告 - 这是评分作业的一部分,主要要求指导和理解代码。
正在尝试为用户输入 IP 地址编写脚本,其中每个八位字节均为 3 位格式(如有必要,前导零)。这是起始输入
' Get IP address to convert
WScript.StdOut.Write("Please Enter your IP address (3 digits per octet) -> ")
strIP = WScript.StdIn.ReadLine()
正在尝试进行错误检查并遇到问题。尝试了各种方法,但 none 似乎提供了所需的输出,如果在 运行.
时没有失败并出现各种 VBScript 错误版本 1:
Dim sMatch(15)
if sMatch = strIP Like "###.###.###.###" Then
Wscript.Echo "The strings are equal."
Else
Wscript.Echo "The strings are not equal."
' WScript.Echo sMatch
End If
版本 2:
String str = StrComp("strIP", ""\###.\###.\###.\###"",vbTextCompare)
If str < 0 or str > 0 then WScript.StdOut.WriteLine "Improper IP format."
版本 3:
iFirstOct = Left(strIP,3)
z1 = strComp(iFirstOct, "###", 1)
WScript.Echo "z1 -> " & z1
WScript.Echo "IP -> " & iFirstOct
If z1 < 1 or z1 > 1 Then
WScript.Echo " Error in input !"
WScript.quit
End If
我有点喜欢版本 3,因为我可以将其作为子例程,但不确定哪个是可行的选择。任何帮助确定我在哪里出错以及如何出错的帮助,我们将不胜感激。
警告 - 这是评分作业的一部分,主要要求指导和理解代码。
-- 编辑 - 21 年 2 月 16 日 --
埃蒂安,
这是最后的脚本。我有 运行 不同的错误场景,它似乎按照我需要的方式运行。感谢您的协助。
' IP_func.vbs
'
' By: Étienne Laneville
' Minor modifications by DragonRider
' From:
'
'
WScript.StdOut.Write("Please Enter your IP address (3 digits per octet) -> ")
strIP = WScript.StdIn.ReadLine()
WScript.Echo vbCrLf & " Input -> " & vbTab & strIP & vbCrlf
' Call error-checking function
CheckIP(strIP)
Function CheckIP(strIP)
Dim arrOctets
Dim iCounter
Dim sOctet
' Check for periods
If InStr(strIP, ".") = 0 Then
CheckIP = False
WScript.Echo " Incorrect number of octets. Please try again."
Exit Function
End If
' Split string on periods
arrOctets = Split(strIP, ".")
' Check that correct number of octets has been received
If UBound(arrOctets) <> 3 Then
CheckIP = False
WScript.Echo " Incorrect number of octets. Please try again."
Exit Function
End If
' Check each octet
For iCounter = 1 To 4
sOctet = arrOctets(iCounter - 1)
' Check if it's numeric
If Not IsNumeric(sOctet) Then
CheckIP = False
WScript.Echo " Improper IP Address. Please try again."
Exit Function
End If
' Check if 3 digits
' Dim three As String
If Not Len(sOctet) = 3 Then
CheckIP = False
WScript.Echo " 3-Digits per octet. Please try again."
Exit Function
End If
' Check for proper values
If CInt(sOctet) > 255 Then
CheckIP = False
WScript.Echo " Octet out of range. Please try again."
Exit Function
End If
If CInt(sOctet) < 0 Then
CheckIP = False
WScript.Echo " Improper IP Address. Please try again."
Exit Function
End If
Next
CheckIP = True
End Function
这是一个简单的函数,可以将事物分解为简单的操作:
Function CheckIP(strIP)
Dim arrOctets
Dim iCounter
Dim sOctet
' Check for periods
If InStr(strIP, ".") = 0 Then
CheckIP = False
Exit Function
End If
' Split string on periods
arrOctets = Split(strIP, ".")
' Check that correct number of octets has been received
If UBound(arrOctets) <> 3 Then
CheckIP = False
Exit Function
End If
' Check each octet
For iCounter = 1 To 4
sOctet = arrOctets(iCounter - 1)
' Check if it's numeric
If Not IsNumeric(sOctet) Then
CheckIP = False
Exit Function
End If
' Check for proper values
If CInt(sOctet) > 255 Then
CheckIP = False
Exit Function
End If
If CInt(sOctet) < 0 Then
CheckIP = False
Exit Function
End If
Next
CheckIP = True
End Function
您还可以通过此功能使用正则表达式检查 IP 地址的有效性:
Option Explicit
Dim Title,IP
Title = "Test the validity of an IP address"
IP = InputBox("Please enter an IP Address",Title,"172.16.18.21")
If Is_Valid(IP) = True Then
MsgBox IP & " is a Valid IP Address",64,Title
Else
MsgBox IP & " is Not a Valid IP Address",16,Title
End if
'------------------------------------------------------------------------------------
Function Is_Valid(ip)
Dim RegularExpressionObject
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"
.IgnoreCase = False
If .Test(ip)= True then
Is_Valid = True
end if
End With
End Function
'------------------------------------------------------------------------------------