Met dit script kun je de ip-configuratie van HP printers aanpassen. getest met HP Laserjet 3035 en HP 4345. De webserver in de printer moet aan staan.
‘******************************************************************************
‘** Script: ChgIpHpPrt.vbs
‘** Version: 1.0
‘** Created: 9:53 22-3-2012
‘** Author: Adriaan Westra
‘**
‘** Purpose / Comments:
‘** Change the IP address on a HP Printer
‘**
‘** 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
‘** ReadIni
‘**
‘** Changelog :
‘** 9:54 22-3-2012 : 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
‘******************************************************************************
‘** IP configuration mode constants
Const cDHCP = 0
Const cBootp = 1
Const cManual = 2
‘******************************************************************************
‘** debugging on/off
bDebug = False
‘******************************************************************************
‘** 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
‘******************************************************************************
‘******************************************************************************
‘** Current Ip address of printer
sPrinter = “192.168.1.21”
‘******************************************************************************
‘** New ip config
sNewIP = “192.168.1.11”
sNewMask = “255.255.255.0”
sNewGW = “192.168.1.1”
‘******************************************************************************
‘** Networkconfiguration page on the printer
sNetworkPage = “/tcpipv4.htm”
‘******************************************************************************
‘** HTTP Prefix
sHttp = “HTTP://”
‘******************************************************************************
‘** Compose start URL
sURL = sHttp & sPrinter
‘******************************************************************************
‘** Check for printer on the network
If Ping(sPrinter) Then
‘******************************************************************************
‘** Write to logfile
If bLog Then
sLogLine = Now & ” ==> ” & sPrinter & ” is responding”
oLogFile.WriteLine sLogLine
End If
‘******************************************************************************
‘** Check if new IP address not in use.
If Ping(sNewIp) Then
‘******************************************************************************
‘** Write to logfile
If bLog Then
sLogLine = Now & ” ==> ” & sNewIp & ” is already in use.”
oLogFile.WriteLine sLogLine
End If
ExitScript
Else
‘******************************************************************************
‘** Write to logfile
If bLog Then
sLogLine = Now & ” ==> ” & sNewIp & ” not in use”
oLogFile.WriteLine sLogLine
End If
End If
‘******************************************************************************
‘** Create internet explorer object
Set oIE = CreateObject(“InternetExplorer.Application”)
if bdebug Then
oIE.Visible = True
End If
‘******************************************************************************
‘** NAvigate to printer homepage
oIE.Navigate sURL
‘******************************************************************************
‘** Wait for Internet Explorer to load page.
WaitForLoad(oIE)
‘******************************************************************************
‘** Write printermodel to logfile
If bLog Then
sLogLine = Now & ” ==> Printer model ” & oIE.document.Title
oLogFile.WriteLine sLogLine
End If
sURL = sHttp & sPrinter & sNetworkPage
oIE.Navigate sURL
WaitForLoad(oIE)
‘******************************************************************************
‘** Write current config to logfile
If bLog Then
sLogLine = Now & ” ==> Current IP cfg method : ” & _
oIE.Document.getElementsByName(“ipv4config_type”).Item(0).value
oLogFile.WriteLine sLogLine
sLogLine = Now & ” ==> Current IP address : ” & _
oIE.Document.getElementsByName(“IPv4_InternetAddress”).Item(0).value
oLogFile.WriteLine sLogLine
sLogLine = Now & ” ==> Current NetMask : ” & _
oIE.Document.getElementsByName(“IPv4_NetMask”).Item(0).value
oLogFile.WriteLine sLogLine
sLogLine = Now & ” ==> Current default Gateway : ” & _
oIE.Document.getElementsByName(“IPv4_DefaultRouter”).Item(0).value
oLogFile.WriteLine sLogLine
End If
‘******************************************************************************
‘** Change current config
oIE.Document.all(“ipv4config_type”).options(cManual).selected = true
oIE.Document.getElementsByName(“IPv4_InternetAddress”).Item(0).value = sNewIp
oIE.Document.getElementsByName(“IPv4_NetMask”).Item(0).value = sNewMask
oIE.Document.getElementsByName(“IPv4_DefaultRouter”).Item(0).value = sNewGW
‘******************************************************************************
‘** apply changes.
oIE.Document.getElementsByName(“Apply”).Item(0).Click()
‘******************************************************************************
‘** Close Internet explorer
oIe.Quit
Else
‘******************************************************************************
‘** Write to logfile
If bLog Then
sLogLine = Now & ” ==> ” & sPrinter & ” is NOT responding”
oLogFile.WriteLine sLogLine
End If
End If
‘******************************************************************************
‘** 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 : Change ip config of HP ” & _
” LaserJet M3035 MFP.”
wscript.echo sComment
wscript.echo sComment & ” Author : ” & cAuthor
wscript.echo sComment
wscript.echo sCmntline
End Sub
‘******************************************************************************
‘** Function: ReadIni
‘** Version: 1.1
‘** Created: 19-03-2003 11:02
‘** Author: Adriaan Westra
‘** E-mail:
‘**
‘** Purpose / Comments:
‘** Reads a value from an iniFile
‘**
‘** Arguments :
‘** sINIFile :Name of the IniFile to read
‘** sSection :Section in the inifile to read from
‘** sKey :Key of the value to read
‘**
‘** Changelog :
‘** 19-03-2003 11:15 : Initial version
‘** 19-03-2003 11:25 : Built in UpperCase Check
‘**
‘******************************************************************************
Function ReadINI(sINIFile, sSection, sKey)
Dim oFSO, oTextFile, sLine
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
Set oTextFile = oFSO.OpenTextFile(sINIFile)
‘loop through each line and check for key value
Do While Not oTextFile.AtEndOfStream
sLine = oTextFile.ReadLine
‘wscript.echo strLine
If UCase(sLine) = UCase(“[” & sSection & “]”) Then
Do While Not oTextFile.AtEndOfStream
sLine = oTextFile.ReadLine
If UCase(Left(sLine, Len(sKey) + 1)) = _
UCase(sKey & “=”) Then
ReadINI = Mid(sLine, InStr(sLine, “=”) + 1)
Exit do
End If
Loop
Exit Do
End If
Loop
oTextFile.Close
End Function
‘******************************************************************************
‘** Function: Ping
‘** Version: 1.0
‘** Created: 10/01/2010 21:55
‘** Author: Adriaan Westra
‘** E-mail:
‘**
‘** Purpose / Comments:
‘**
‘** Ping a computer
‘**
‘**
‘** Change Log :
‘**
‘** 10/01/2010 21:55 : Initial Version
‘**
‘** Arguments :
‘**
‘** strPC : Name of the the PC to ping
‘**
‘** Returns :
‘**
‘** 0 : Destination ureachable
‘** 1 : Destination responding normal
‘**
‘******************************************************************************
Function Ping(strPc)
Set objPing = GetObject(“winmgmts:{impersonationLevel=impersonate}”)._
ExecQuery(“select * from Win32_PingStatus where address = ‘”_
& strPc & “‘”)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
Ping = 0
Else
Ping = 1
End If
Next
End Function
‘******************************************************************************
‘** Sub : WaitForLoad
‘** Version: 1.0
‘** Created: 10/01/2010 21:55
‘** Author: Adriaan Westra
‘** E-mail:
‘**
‘** Purpose / Comments:
‘**
‘** Wait for Internet Explorer to load a page
‘**
‘**
‘** Change Log :
‘**
‘** 10/01/2010 21:55 : Initial Version
‘**
‘** Arguments :
‘**
‘** obj : internet explorer object
‘**
‘******************************************************************************
Sub WaitForLoad(obj)
iTeller = 0
Do While obj.Busy
If bDebug Then
WScript.StdOut.Write( “.”)
End If
wscript.sleep(100)
iTeller = iTeller + 1
If iTeller > 50 Then
Exit Do
End If
Loop
iTeller = 0
wscript.echo
Do While obj.readyState <> 4
if bDebug Then
WScript.StdOut.Write(“.”)
End If
wscript.sleep(100)
iTeller = iTeller + 1
If iTeller > 50 Then
Exit Do
End If
Loop
wscript.sleep(100)
End Sub