IpCalc.vbs

Sample script for using the functions GetNetwork, GetBroadcast, DecMask2Dotted and DottedMask2dec. GetNetwork will return the Network address for an IP address and subnet mask. GetBroadcast will return the Broadcast address. DecMask2Dotted will convert a /24 subnet mask in to the dotted form 255.255.255.0 an DottedMask2Dec will convert a subnet mask in the dotted form to the decimal form.


Output :
D:\>cscript IpCalc.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Ip Address : 192.168.1.101
Subnetmask : 255.255.255.0
Network Address : 192.168.1.0
Broadcast Address : 192.168.1.255
/22 Subnet : 255.255.252.0
255.255.255.0 : /24 Subnet

'******************************************************************************
'** Script: IpCalc.vbs
'** Version: 1.0
'** Created: 8:16 11-6-2013
'** Author: Adriaan Westra
'**
'** Purpose / Comments:
'** Sample script for Ip calculations
'**
'** Naming Convention :
'** Constants : Starts with Lowercase c
'** String : Starts with Lowercase s
'** integer/long : Starts with LowerCase i
'** single/double : Starts with LowerCase f
'** array : Starts with Lowercase a
'** object : Starts with Lowercase o
'** boolean : Starts with Lowercase b
'** date : Starts with Lowercase d
'**
'** Define default variables :
'**
'** strPath Startup path for the script with trailing backslash
'** strScript Script name without extension
'**
'** Functions used :
'**
'** ExitScript
'** DisplayHelp
'** GetNetwork
'** GetBroadcast
'** DecMask2Dotted
'** Bin2Dec
'** DottedMask2Dec
'** Dec2Bin
'**
'** Changelog :
'** 8:16 11-6-2013 : Initial version
'**
'******************************************************************************

'******************************************************************************
'** Define error handling
On Error Resume next

'******************************************************************************
'** Declaration of constants
'******************************************************************************
'** Script constants
Const cVersion = "1.0" '** Script version
Const cAuthor = "A. Westra" '** Author of the script
'******************************************************************************
'** FileAcces
Const cForReading = 1
Const cForWriting = 2
Const cForAppend = 8
'******************************************************************************
'** debugging on/off
bDebug = True
'******************************************************************************
'** Declare oLogFile to be able to use subroutine for open and close of logfile
Dim oLogFile

'******************************************************************************
'** Get startup path from scriptfullname
iPos = InStr(wscript.ScriptFullName, wscript.ScriptName)
sPath = Mid(wscript.ScriptFullName,1,iPos - 1)

'******************************************************************************
'** Get the script name from the script
iPos = InStr(wscript.ScriptName, ".")
sScript = Mid(wscript.ScriptName,1,iPos - 1)

if bDebug Then
StartLogging
End If
'******************************************************************************
'** Make sure the script is started with cscript
If InStr(wscript.FullName, "wscript.exe") > 0 Then
if bDebug Then
sLogLine = Now & " ==> Started with wscript.exe, exit script. "
oLogFile.WriteLine sLogLine
oLogFile.Close
End If
MsgBox "Please run this script with cscript.exe." & Chr(13) & _
"For example : cscript " & WScript.ScriptName & " /?", _
vbExclamation, WScript.ScriptName
WScript.Quit(1)
End If

'******************************************************************************
'** Get commandline parameters
Set oArgs = Wscript.Arguments
'******************************************************************************
'** Check if there are any command line parameters
If oArgs.Count <> 0 Then
if bDebug Then
'******************************************************************************
'** Debug is on write parameters to log
For iCount = 0 to oArgs.Count - 1
sLogLine = Now & " ==> Commandline argument " & iCount & _
" : " & oArgs(iCount)
oLogFile.WriteLine sLogLine
Next
End If
'******************************************************************************
'** Check if help needs to be displayed
If InStr(oArgs(0),"/?") > 0 Or InStr(UCase(oArgs(0)),"/H") > 0 _
Or InStr(UCase(oArgs(0)),"/HELP") > 0 Then
DisplayHelp
ExitScript
'******************************************************************************
'** Else process command line parameters
ElseIf InStr(UCase(oArgs(0)),"/LOG") Then
If not bDebug Then
StartLogging
End If
bLog = True
'******************************************************************************
'** Commandline parameters expected but not entered
'Else
'******************************************************************************
'** Ask for the parameters
' strImageDir = InputBox("Please enter parameter " & _
' "to process : ",wscript.scriptname, strPath)
End if
End if

'******************************************************************************
'** Start of script
'******************************************************************************
sIpAddress = "192.168.1.101"
sSubnetMask = "255.255.255.0"

wscript.echo "Ip Address : " & sIpAddress
wscript.echo "Subnetmask : " & sSubnetMask
wscript.echo "Network Address : " & GetNetwork(sIpAddress,sSubnetMask)
wscript.echo "Broadcast Address : " & GetBroadcast(sIpAddress,sSubnetMask)
wscript.echo "/22 Subnet : " & DecMask2Dotted("/22")
wscript.echo "255.255.255.0 : /" & DottedMask2Dec("255.255.255.0") & " Subnet"

'******************************************************************************
'** End of script
'******************************************************************************
ExitScript
wscript.quit
'******************************************************************************
'** Put Functions and Subroutines here !
'******************************************************************************
'** Sub: ExitScript
'** Version: 1.0
'** Created: 11:53 22-3-2012
'** Author: Adriaan Westra
'**
'** Purpose / Comments:
'** Handle default actions for closing script. For example close logfile.
'**
'** Arguments :
'**
'** Changelog :
'** 11:55 22-3-2012 : Initial version
'**
'******************************************************************************
Sub ExitScript
if bDebug or bLog Then
'******************************************************************************
'** Log the end time of the script
sLogLine = Now & " ==> End of script : " & Wscript.scriptname
oLogFile.WriteLine sLogLine
End If
wscript.quit
End Sub
'******************************************************************************
'** Sub: StartLogging
'** Version: 1.0
'** Created: 11:53 22-3-2012
'** Author: Adriaan Westra
'**
'** Purpose / Comments:
'** Create folder structure for logging and open log file.
'**
'** Arguments :
'**
'** Changelog :
'** 11:55 22-3-2012 : Initial version
'**
'******************************************************************************
Sub StartLogging
'******************************************************************************
'** Add logging to the script
If Not IsObject(oFS) Then
Set oFS = CreateObject("Scripting.FileSystemObject")
End If
sLogDir = sScript & "\logs"
'******************************************************************************
'** Create folder structure for logging
if Not (oFS.FolderExists(sScript)) Then
oFS.CreateFolder(sScript)
End If
if Not (oFS.FolderExists(sLogDir)) Then
oFS.CreateFolder(sLogDir)
End If
sLogFile = sLogDir & "\" & sScript & ".log"
Set oLogFile = oFS.OpenTextFile(sLogFile, cForAppend, True)
'******************************************************************************
'** Log the start time of the script
sLogLine = Now & " ==> Start of script : " & Wscript.scriptname
oLogFile.WriteLine sLogLine
End Sub
'******************************************************************************
'** Sub: DisplayHelp
'** Version: 1.0
'** Created: 24-03-2003 8:22
'** Author: Adriaan Westra
'**
'** Purpose / Comments:
'** Display help for script
'**
'** Arguments :
'**
'** Changelog :
'** 24-03-2003 8:22 : Initial version
'**
'******************************************************************************
Sub DisplayHelp()
sComment = string(2,"*")
sCmntLine = String(79, "*")
wscript.echo sCmntline
wscript.echo sComment
wscript.echo sComment & " Online help for " & _
Wscript.scriptname & " version : " & cVersion
wscript.echo sComment
wscript.echo sComment & " Usage : cscript " & _
Wscript.scriptname & " parameters"
wscript.echo sComment
wscript.echo sComment & " Purpose : Sample to calculate Network " & _
"address and broadcast address."
wscript.echo sComment
wscript.echo sComment & " Author : " & cAuthor
wscript.echo sComment
wscript.echo sCmntline
End Sub
'******************************************************************************
'** Function : GetNetwork
'** Version : 1.0
'** Created : 8:18 11-6-2013
'** Author : Adriaan Westra
'**
'** Purpose / Comments:
'** Get the network address from an IP address and Subnet Mask
'**
'** Arguments :
'** sIp : Ipaddress to get the network part from
'** sSM : Subnet Mask needed to calculate the network address
'**
'** Returns : Network Address
'**
'** Changelog :
'** 8:19 11-6-2013 : Initial version
'**
'******************************************************************************
Function GetNetwork(sIp, sSM)
Dim aNetwork(3)
aIP = Split(sIP,".")
aSM = Split(sSM,".")
For iCount = 0 To 3
aNetwork(icount) = aIP(iCount) And aSM(icount)
Next
GetNetwork = Join(aNetwork,".")
End Function
'******************************************************************************
'** Function : GetBroadcast
'** Version : 1.0
'** Created : 8:18 11-6-2013
'** Author : Adriaan Westra
'**
'** Purpose / Comments:
'** Get the Broadcast address from an IP address and Subnet Mask
'**
'** Arguments :
'** sIp : Ipaddress to get the Broadcast address from
'** sSM : Subnet Mask needed to calculate the Broadcast address
'**
'** Returns : Broadcast Address
'**
'** Changelog :
'** 8:19 11-6-2013 : Initial version
'**
'******************************************************************************
Function GetBroadcast(sIp, sSM)
Dim aNetwork(3)
aIP = Split(sIP,".")
aSM = Split(sSM,".")
For iCount = 0 To 3
aNetwork(icount) = aIP(iCount) Or (255 - aSM(icount))
Next
GetBroadcast = Join(aNetwork,".")
End Function
'******************************************************************************
'** Function : DecMask2Dotted
'** Version : 1.0
'** Created : 8:18 11-6-2013
'** Author : Adriaan Westra
'**
'** Purpose / Comments:
'** Convert decimal subnet mask to dotted subnetmask
'**
'** Arguments :
'** iSM : Subnet Mask in decimal form ( 24 or "/24")
'**
'** Returns : Dotted subnet mask ("255.255.255.0")
'**
'** Changelog :
'** 8:19 11-6-2013 : Initial version
'**
'******************************************************************************
Function DecMask2Dotted(iSM)
if Instr(iSM,"/") > 0 Then
iSM = mid(iSM, 2, Len(iSM) - 1)
End If
If Not TypeName(iSM) = "Integer" Then
iSM = CInt(iSM)
End If
sSM = String(iSM,"1") & String(32 - iSM,"0")
sResult = ""
For iCount = 1 to 32 step 8
If iCount\ 8 < 3 Then
sResult = sResult & Bin2Dec( Mid(sSM,iCount,8) ) & "."
Else
sResult = sResult & Bin2Dec( Mid(sSM,iCount,8) )
End If
Next
DecMask2Dotted = sResult
End Function
'******************************************************************************
'** Function : Bin2Dec
'** Version : 1.0
'** Created : 8:18 11-6-2013
'** Author : Adriaan Westra
'**
'** Purpose / Comments:
'** convert binary number in to a decimal number
'**
'** Arguments :
'** sBin : Binary number in string format ("11111111")
'**
'** Returns : Decimal number (255)
'**
'** Changelog :
'** 8:19 11-6-2013 : Initial version
'**
'******************************************************************************
Function Bin2Dec( sBin )
iResult = 0
iLen = Len(sBin)
For iCount = 0 to iLen - 1
iResult = iResult + CInt(Mid(sBin,iLen - iCount, 1 )) * (2^iCount)
Next
Bin2Dec = iResult
End Function
'******************************************************************************
'** Function : DottedMask2Dec
'** Version : 1.0
'** Created : 8:18 11-6-2013
'** Author : Adriaan Westra
'**
'** Purpose / Comments:
'** Convert dotted subnetmask to decimal subnet mask
'**
'** Arguments :
'** sSM : Dotted subnet mask ("255.255.255.0")
'**
'** Returns : Subnet Mask in decimal form ( 24 )
'**
'** Changelog :
'** 8:19 11-6-2013 : Initial version
'**
'******************************************************************************
Function DottedMask2Dec(sSM)
Do
sBin = Sbin & Dec2Bin(Mid(sSM, 1, Instr(sSM, ".") - 1))
sSM = Mid(sSM, Instr(sSM, ".") + 1, Len(sSM) - Instr(sSM, "."))
Loop Until Instr(sSM, ".") = 0
sBin = sBin & Dec2Bin(sSM)
iResult = 0
For iCount = 1 To Len( sBin )
iResult = iResult + Cint(Mid(sBin,iCount,1))
Next
DottedMask2Dec = iResult
End Function
'******************************************************************************
'** Function : Dec2Bin
'** Version : 1.0
'** Created : 8:18 11-6-2013
'** Author : Adriaan Westra
'**
'** Purpose / Comments:
'** convert binary number in to a decimal number
'**
'** Arguments :
'** iDec : Decimal number (255)
'**
'** Returns : Binary number in string format ("11111111")
'**
'** Changelog :
'** 8:19 11-6-2013 : Initial version
'**
'******************************************************************************
Function Dec2Bin( iDec )
Do
sBin = CStr(iDec Mod 2) & sBin
iDec = iDec \ 2
Loop Until iDec = 0
Dec2Bin = sBin
End Function

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *