Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IP Address format

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi I have a Sub which checks the validity of a textbox input.
The textbox takes an ip address feed and if cancel is pressed the script will quit.


sub EnterIP

'textbox for entering value
IPAddress = inputbox( "Please Enter IP Address:" )
'Username = inputbox( "username:" )
'password = inputbox( "password:" )
'validation of textbox input
'if not a numeric value
If Not IsNumeric (IPAddress) Then
wscript.echo "This needs to be an IP address like 1.1.1.1"
call enterip
else
'if cancel is pressed
if IPAddress = "" then
wscript.quit
end if
end if
End Sub

The problem I have is that the IsNumeric function is incorrect for an ip addres function, presumably because it has '.' in.

Does anyone know how or if there is a function for an IP address format. I though maybe using regular expressions and the instr function...
#thanks

 
However, you cannot validate IP-adresses with IsNumeric function,
but rather create your own function for validating of IP-adresses.
...something like the function is_ip() here:
Code:
[COLOR=#0000ff]' test the function is_ip[/color]
ip_adresses_string [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"10.0.1.127 1.a.2.3 1.1.1 1.2.3.4.5"[/color]
ip_adresses [COLOR=#804040][b]=[/b][/color] [COLOR=#a020f0]split[/color][COLOR=#804040][b]([/b][/color]ip_adresses_string[COLOR=#804040][b])[/b][/color]
[COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] ip_adress [COLOR=#804040][b]in[/b][/color] ip_adresses
 [COLOR=#0000ff] 'test if valid[/color]
  [COLOR=#804040][b]if[/b][/color] is_ip[COLOR=#804040][b]([/b][/color]ip_adress[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]
    wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"'"[/color] [COLOR=#804040][b]&[/b][/color] ip_adress [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"' is valid IP-adress."[/color]
  [COLOR=#804040][b]else[/b][/color]
    wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"Error: '"[/color] [COLOR=#804040][b]&[/b][/color] ip_adress [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"' is invalid IP-adress !"[/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]next[/b][/color]

[COLOR=#0000ff]' now calling the subroutine[/color]
[COLOR=#804040][b]call[/b][/color] EnterIP

[COLOR=#0000ff]' ------------------------ Functions/Subroutines --------------------[/color]
[COLOR=#804040][b]function[/b][/color] is_ip[COLOR=#804040][b]([/b][/color]ip_adress[COLOR=#804040][b])[/b][/color]
  is_ip [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]True[/color]
 [COLOR=#0000ff] ' split string into list[/color]
  ip_adress_lst [COLOR=#804040][b]=[/b][/color] [COLOR=#a020f0]split[/color][COLOR=#804040][b]([/b][/color]ip_adress[COLOR=#804040][b],[/b][/color][COLOR=#ff00ff]"."[/color][COLOR=#804040][b])[/b][/color]
 [COLOR=#0000ff] ' if number of IP adress parts is 4[/color]
  [COLOR=#804040][b]if[/b][/color] [COLOR=#008080]uBound[/color][COLOR=#804040][b]([/b][/color]ip_adress_lst[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]3[/color] [COLOR=#804040][b]then[/b][/color]
   [COLOR=#0000ff] ' validate if every IP part is numeric[/color]
    [COLOR=#804040][b]for[/b][/color] [COLOR=#804040][b]each[/b][/color] ip_part [COLOR=#804040][b]in[/b][/color] ip_adress_lst
      [COLOR=#804040][b]if[/b][/color] [COLOR=#804040][b]not[/b][/color] [COLOR=#008080]isNumeric[/color][COLOR=#804040][b]([/b][/color]ip_part[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color] 
        is_ip [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]False[/color]
        [COLOR=#804040][b]exit[/b][/color] [COLOR=#804040][b]for[/b][/color]
      [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
    [COLOR=#804040][b]next[/b][/color]
  [COLOR=#804040][b]else[/b][/color]
   [COLOR=#0000ff] ' if number of IP adress parts is not 4[/color]
    is_ip [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]False[/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]function[/b][/color]

[COLOR=#804040][b]sub[/b][/color] EnterIP
 [COLOR=#0000ff] 'textbox for entering value[/color]
  IPAddress [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]inputbox[/color][COLOR=#804040][b]([/b][/color] [COLOR=#ff00ff]"Please Enter IP Address:"[/color] [COLOR=#804040][b])[/b][/color]
 [COLOR=#0000ff] 'if cancel is pressed [/color]
  [COLOR=#804040][b]if[/b][/color] IPAddress [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""[/color] [COLOR=#804040][b]then[/b][/color]
    wscript[COLOR=#804040][b].[/b][/color][COLOR=#a020f0]quit[/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]

  [COLOR=#804040][b]if[/b][/color] [COLOR=#804040][b]not[/b][/color] is_ip [COLOR=#804040][b]([/b][/color]IPAddress[COLOR=#804040][b])[/b][/color] [COLOR=#804040][b]then[/b][/color]  
    wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"This needs to be an IP address like 1.1.1.1"[/color]
    [COLOR=#804040][b]call[/b][/color] enterip
  [COLOR=#804040][b]else[/b][/color]
    wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"This is IP-adress '"[/color] [COLOR=#804040][b]&[/b][/color] IPAddress [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"'"[/color]
  [COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]if[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]sub[/b][/color]
...but I'm not sure if every IP-adress should have 4 parts or not :)
 
Hi,

Thanks that worked a treat and I have integrated it into my script. Here is is:

' Constants for type of event log entry
const EVENTLOG_SUCCESS = 0
const EVENTLOG_ERROR = 1
const EVENTLOG_WARNING = 2
const EVENTLOG_INFORMATION = 4
const EVENTLOG_AUDIT_SUCCESS = 8
const EVENTLOG_AUDIT_FAILURE = 16

'Echo constants

const FailedConnect = "Failed to connect, check if device is on and functioning, or Contact TSS"


Set WSHNetwork = CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
UserString = WSHNetwork.UserName
usertime = now




' test the function is_ip
ip_adresses = split(IPAddress)

for each ip_adress in ip_adresses
'test if valid
if is_ip(ip_adress) then
wscript.echo "'" & ip_adress & "' is valid IP-adress."
else
wscript.echo "Error: '" & ip_adress & "' is invalid IP-adress !"
end if
next

' now calling the subroutine
call Enterip

'write username, date & time and IP Address to eventlog

WshShell.LogEvent EVENTLOG_INFORMATION, "Username " & userstring &", " & "Date " & usertime & " logged on via Terminal Services and attempted to restart " & IPAddress


'==========================================================================
' The following function will test if a machine is reachable via a ping
' using WMI and the Win32_PingStatus
'==========================================================================
If Reachable(IPAddress) Then
call TelnetFunc
Else
reachable(IPAddress)
wscript.sleep 1000
reachable(IPAddress)
wscript.sleep 1000
reachable(IPAddress)
wscript.echo FailedConnect
'wscript.quit
End If

Function Reachable(strComputer)
' On Error Resume Next

Dim wmiQuery, objWMIService, objPing, objStatus

wmiQuery = "Select * From Win32_PingStatus Where " & _
"Address = '" & strComputer & "'"

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)

For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
Reachable = False 'if computer is unreacable, return false
Else
Reachable = True 'if computer is reachable, return true
End If
Next
End Function

'============================================================================
' The function fires up telnet (or whatever) using the specified port #
'============================================================================



'change port for different signs\busses
Sub Telnetfunc
wshshell.run("telnet " & IPAddress & " 21")
end sub

'============================================================================
' The following procedure will validate the textbox input and act accordingly
'============================================================================


sub EnterIP
'textbox for entering value
IPAddress = inputbox( "Please Enter IP Address:", "Ping", "10.190.?.?" )
'if cancel is pressed
if IsNull(IPAddress) or ipaddress = "" then
wscript.quit
end if
if not is_ip (IPAddress) then
wscript.echo "This needs to be an IP address like 10.190.23.13"
call enterip
'else
'wscript.echo "This is IP-adress '" & IPAddress & "'"
end if
end sub


'===================================================================================
' Function to split and identify the format of the IP address entered into checkbox
'===================================================================================



function is_ip(ip_adress)
is_ip = True
' split string into list
ip_adress_lst = split(ip_adress,".")
' if number of IP adress parts is 4
if uBound(ip_adress_lst) = 3 then
' validate if every IP part is numeric
for each ip_part in ip_adress_lst
if not isNumeric(ip_part) then
is_ip = False
exit for
end if
next
else
' if number of IP adress parts is not 4
is_ip = False
end if
end function


James



 
>' validate if every IP part is numeric...

... and is between 0 and 255; would be a little more rubust.
 
... and that is the minimum I would qualify for any kind of "validating" "ip address".
 
HughLerwill said:
>' validate if every IP part is numeric...

... and is between 0 and 255; would be a little more rubust.
Yes I forgot this fact, but IMHO it could be added to the function by the OP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top