Saturday 8 June 2013

Validating an IP address using vb script

The following VB Script shows how can you validate an IP address by using vb scripting. Just copy and paste in a notepad and save it as .vbs file and then run this.

This script may be used in automation scripts where you want the user to enter an IP address to proceed with the test case.



' ------------------------ Functions/Subroutines --------------------
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

sub EnterIP
  'textbox for entering value
  UserIP = inputbox( "Please Enter the IP Address you wish to" )
  'if cancel is pressed
  if UserIP = "" then
MsgBox("No Input, Closing the Tool")
    wscript.quit
  end if

  if not is_ip (UserIP) then
    wscript.echo "This needs to be an IP address like 1.1.1.1"
    call EnterIP
  else
    wscript.echo "This is the IP-address submitted by you - '" & UserIP & "'"
  end if
end sub
Do
' now calling the subroutine
call EnterIP

No comments:

Post a Comment