上上篇说了一下linux平台下程序的自动安装,利用的是shell脚本,而同理在windows平台上,利用的则是vbs,当然单纯的vbs并不是很强大,同时我还利用了cmd的一些命令。同上一篇一样,都是利用ftp进行下载文件,然后在解压,在执行程序,同时会判断指定的端口是否被占用,以及一些业务逻辑处理。详见代码:
ftpIp="10.22.2.21" ftpPort=2121 agentIp="10.22.2.13" ftpUserName="test" ftpUserPwd="123456" ftpAgentPath="/main/v1.0" ftpAgentFileName="hyperagent.zip" ftpJrePath="/java/windows/x86" ftpJreName="jre.zip" ftpErrorPath="/error" agentSaveDir="E:\agentworkhome\dalp" agentContextName="agent" installAgentScriptPathName="agentscript" installAgentScriptFileName="installAgent.vbs" startAgentScriptName="bin\install.bat" uninstallAgentScriptName="bin\uninstall.bat" tempPortFileName="tempPortFileName.txt" agentPort=4850 Function FTPUpload(sSite, sUsername, sPassword, sLocalFile, sRemotePath) Const OpenAsDefault = -2 Const FailIfNotExist = 0 Const ForReading = 1 Const ForWriting = 2 Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject") Set oFTPScriptShell = CreateObject("WScript.Shell") sRemotePath = Trim(sRemotePath) sLocalFile = Trim(sLocalFile) If InStr(sRemotePath, " ") > 0 Then If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then sRemotePath = """" & sRemotePath & """" End If End If If InStr(sLocalFile, " ") > 0 Then If Left(sLocalFile, 1) <> """" And Right(sLocalFile, 1) <> """" Then sLocalFile = """" & sLocalFile & """" End If End If If Len(sRemotePath) = 0 Then sRemotePath = "\" End If If InStr(sLocalFile, "*") Then If InStr(sLocalFile, " ") Then FTPUpload = "Error: Wildcard uploads do not work if the path contains a " & _ "space." & vbCRLF FTPUpload = FTPUpload & "This is a limitation of the Microsoft FTP client." Exit Function End If ElseIf Len(sLocalFile) = 0 or Not oFTPScriptFSO.FileExists(sLocalFile) Then FTPUpload = "Error: File Not Found." Exit Function End If '--------END Path Checks--------- sFTPScript = sFTPScript & "OPEN " & sSite & " " & ftpPort & vbCRLF sFTPScript = sFTPScript & "USER " & sUsername & vbCRLF sFTPScript = sFTPScript & sPassword & vbCRLF sFTPScript = sFTPScript & "cd " & sRemotePath & vbCRLF sFTPScript = sFTPScript & "binary" & vbCRLF sFTPScript = sFTPScript & "prompt n" & vbCRLF sFTPScript = sFTPScript & "put " & sLocalFile & vbCRLF sFTPScript = sFTPScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLF sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%") sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName sFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName 'Write the input file for the ftp commandto a temporary file. Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True) fFTPScript.WriteLine(sFTPScript) fFTPScript.Close Set fFTPScript = Nothing oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & sFTPTempFile & " " & " > " & sFTPResults, 0, TRUE Wscript.Sleep 1000 'Check results of transfer. Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, _ FailIfNotExist, OpenAsDefault) sResults = fFTPResults.ReadAll fFTPResults.Close oFTPScriptFSO.DeleteFile(sFTPTempFile) oFTPScriptFSO.DeleteFile (sFTPResults) If InStr(sResults, "226 Transfer complete.") > 0 Then FTPUpload = True ElseIf InStr(sResults, "File not found") > 0 Then FTPUpload = "Error: File Not Found" ElseIf InStr(sResults, "password incorrect") > 0 Then FTPUpload = "Error: Login Failed." Else FTPUpload = "Error: Unknown." End If Set oFTPScriptFSO = Nothing Set oFTPScriptShell = Nothing End Function Function FTPDownload(sSite, sUsername, sPassword, sLocalPath, sRemotePath, sRemoteFile) Const OpenAsDefault = -2 Const FailIfNotExist = 0 Const ForReading = 1 Const ForWriting = 2 Set oFTPScriptFSO = CreateObject("Scripting.FileSystemObject") Set oFTPScriptShell = CreateObject("WScript.Shell") sRemotePath = Trim(sRemotePath) sLocalPath = Trim(sLocalPath) '----------Path Checks--------- If InStr(sRemotePath, " ") > 0 Then If Left(sRemotePath, 1) <> """" And Right(sRemotePath, 1) <> """" Then sRemotePath = """" & sRemotePath & """" End If End If If Len(sRemotePath) = 0 Then sRemotePath = "\" End If 'If the local path was blank. Pass the current working direcory. If Len(sLocalPath) = 0 Then sLocalpath = oFTPScriptShell.CurrentDirectory End If If Not oFTPScriptFSO.FolderExists(sLocalPath) Then 'destination not found FTPDownload = "Error: Local Folder Not Found." Exit Function End If sOriginalWorkingDirectory = oFTPScriptShell.CurrentDirectory oFTPScriptShell.CurrentDirectory = sLocalPath '--------END Path Checks--------- 'build input file for ftp command sFTPScript = sFTPScript & "OPEN " & sSite & " " & ftpPort & vbCRLF sFTPScript = sFTPScript & "USER " & sUsername & vbCRLF sFTPScript = sFTPScript & sPassword & vbCRLF sFTPScript = sFTPScript & "cd " & sRemotePath & vbCRLF sFTPScript = sFTPScript & "binary" & vbCRLF sFTPScript = sFTPScript & "prompt n" & vbCRLF sFTPScript = sFTPScript & "mget " & sRemoteFile & vbCRLF sFTPScript = sFTPScript & "quit" & vbCRLF & "quit" & vbCRLF & "quit" & vbCRLF 'msgbox sFTPScript sFTPTemp = oFTPScriptShell.ExpandEnvironmentStrings("%TEMP%") sFTPTempFile = sFTPTemp & "\" & oFTPScriptFSO.GetTempName sFTPResults = sFTPTemp & "\" & oFTPScriptFSO.GetTempName 'Write the input file for the ftp command to a temporary file. Set fFTPScript = oFTPScriptFSO.CreateTextFile(sFTPTempFile, True) fFTPScript.WriteLine(sFTPScript) fFTPScript.Close Set fFTPScript = Nothing 'msgbox sFTPTempFile oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & sFTPTempFile & " " & _ " > " & sFTPResults, 0, TRUE Wscript.Sleep 1000 'Check results of transfer. Set fFTPResults = oFTPScriptFSO.OpenTextFile(sFTPResults, ForReading, _ FailIfNotExist, OpenAsDefault) sResults = fFTPResults.ReadAll fFTPResults.Close ' msgbox "sResults:" & sResults If InStr(sResults, "226 Transfer complete.") > 0 Then FTPDownload = True ElseIf InStr(sResults, "File not found") > 0 Then FTPDownload = "Error: File Not Found" ElseIf InStr(sResults, "password incorrect") > 0 Then FTPDownload = "Error: Login Failed." Else FTPDownload = "Error: Unknown." End If Set oFTPScriptFSO = Nothing Set oFTPScriptShell = Nothing End Function 'get the name of sourcefile Function getDefMD5File(sourceFile) getDefMD5File =sourceFile & ".md5" End Function Function checkFileExist(filePath) Dim Objectfs Set Objectfs = CreateObject("Scripting.FileSystemObject") If Objectfs.FileExists(filePath) Then checkFileExist = True Else checkFileExist = False End If End Function 'only read first line Function readFile(sourceFile) Const ForReading = 1, ForWriting = 2 Dim fso, f On Error Resume Next Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile(sourceFile, ForReading) readFile = f.ReadLine if Err.number<>0 then Wscript.Echo Err.Number & Err.description Err.clear readFile="" end if End Function Function GetFileHash(file_name) Dim file_hash Dim hash_value Dim i Set wi = CreateObject("WindowsInstaller.Installer") Set file_hash = wi.FileHash(file_name, 0) hash_value = "" For i = 1 To file_hash.FieldCount hash_value = hash_value & BigEndianHex(file_hash.IntegerData(i)) Next GetFileHash = hash_value Set file_hash = Nothing Set wi = Nothing End Function Function BigEndianHex(int) Dim result Dim b1, b2, b3, b4 result = Hex(int) b1 = Mid(result, 7, 2) b2 = Mid(result, 5, 2) b3 = Mid(result, 3, 2) b4 = Mid(result, 1, 2) BigEndianHex = b1 & b2 & b3 & b4 End Function Function MD5Check(sourceFile) Dim retval,file_hash,server_hash retval= FTPDownload(ftpIp, ftpUserName , ftpUserPwd, agentSaveDir, ftpAgentPath,getDefMD5File(sourceFile)) If retval = true Then Wscript.Echo "download " & getDefMD5File(sourceFile) & " success" Wscript.Sleep 1000 file_hash = GetFileHash(sourceFile) server_hash = readFile(getDefMD5File(sourceFile)) Wscript.Echo "current md5:" & file_hash Wscript.Echo "download md5:" & server_hash If InStr(server_hash, file_hash) > 0 Then Wscript.Echo "md5 check success file:" & sourceFile MD5Check=True Else Wscript.Echo "md5 check fail file:" & sourceFile End If Else Wscript.Echo "download " & getDefMD5File(sourceFile) & " fail " & retval 'msgbox "update error:" & retval End if End Function Function deleteFile(sourceFile) set fso=createobject("scripting.filesystemobject") On Error Resume Next fso.deleteFile sourceFile End Function Function deleteDirectory(sourceDirectory) set fso=createobject("scripting.filesystemobject") On Error Resume Next fso.deletefolder sourceDirectory End Function Function deleteDirectoryBat(sourceDirectory) set objShell=wscript.createObject("wscript.shell") 'iReturn=objShell.Run("cmd.exe /C unzip -d D:\test\agent_install D:\test\agent_install\dir2.zip", 0, TRUE) deleteDirectoryBat=objShell.Run("cmd.exe /C del /s /q " & sourceDirectory , 0, TRUE) deleteDirectoryBat=objShell.Run("cmd.exe /C rd /s /q " & sourceDirectory , 0, TRUE) End Function Function downloadFile(sourceFile) Dim retval,checkResult retval= FTPDownload(ftpIp, ftpUserName , ftpUserPwd, agentSaveDir, ftpAgentPath,sourceFile) If retval = true Then checkResult = MD5Check(sourceFile) If checkResult = True Then Wscript.Echo "md5check success" & ftpAgentFileName Else Wscript.Echo "md5check fail" & ftpAgentFileName & retval End If downloadFile = checkResult Else Wscript.Echo "downloadFile error:" & ftpAgentFileName & retval downloadFile = false End If End Function Function unzipFile(sourcefile,destPath) ' Wscript.Echo "unzipFile " ' Wscript.Echo "cmd.exe /C unzip -d " & destPath & " " & sourcefile set objShell=wscript.createObject("wscript.shell") 'iReturn=objShell.Run("cmd.exe /C unzip -d D:\test\agent_install D:\test\agent_install\dir2.zip", 0, TRUE) unzipFile=objShell.Run("cmd.exe /C unzip -d " & destPath & " " & sourcefile , 0, TRUE) 'Wscript.Echo "unzipFile " & sourcefile & unzipFile End Function Function checkPortIsInUser(port) Dim retval ,command,tempPortFullName,portLine tempPortFullName = agentSaveDir & "\" & installAgentScriptPathName & "\" & tempPortFileName set objShell=wscript.createObject("wscript.shell") command ="netstat -ano -p TCP | findstr /i " & chr(34) & "^TCP [0-9]:" & port & "[^0-9]" & chr(34) & " > " & tempPortFullName Wscript.Echo "run command" & command retval=objShell.Run("cmd.exe /C " & command , 0, TRUE) Wscript.Sleep 1000 retval=checkFileExist(tempPortFullName) If retval = true Then portLine = readFile(tempPortFullName) If InStr( portLine,port) > 0 Then Wscript.Echo "port busy" checkPortIsInUser = True Else Wscript.Echo "port free" checkPortIsInUser = False End If Else checkPortIsInUser = False End If End Function Function uploadAgentlog(msg) Dim retval ,tempLogFullName,fs,f tempLogFullName = agentSaveDir & "\" & installAgentScriptPathName & "\" & agentIp & ".log" set fs =createobject("scripting.filesystemobject") set f=fs.opentextfile(tempLogFullName,2, true) f.write msg f.close retval = FTPUpload(ftpIp, ftpUserName , ftpUserPwd,tempLogFullName,ftpErrorPath) End Function Function uninstallAgent() Dim retval ,uninstallScriptFullName uninstallScriptFullName = agentSaveDir & "\" & agentContextName & "\" & uninstallAgentScriptName retval=checkFileExist(uninstallScriptFullName) If retval = true Then set objShell=wscript.createObject("wscript.shell") Wscript.Echo "begin uninstall " & uninstallScriptFullName retval=objShell.Run("cmd.exe /C " & uninstallScriptFullName , 0, TRUE) Wscript.Echo "end uninstall " & uninstallScriptFullName & " " & retval End If End Function Function installAgent() On Error Resume Next Dim retval retval =checkPortIsInUser(agentPort) If retval = true Then uninstallAgent() Wscript.Sleep 3000 retval =checkPortIsInUser(agentPort) If retval = true Then 'upload log uploadAgentlog("port " & agentPort & " busy and cannot be removed,stop install agent") Else installAgent2() End If Else installAgent2() End If End Function Function installAgent2() Dim retval , agentPath,downloadTempPath,startScriptFullName On Error Resume Next '1 first delete hyperagent directory and zipfile '2 then download the hyperagent.zip and jre.zip '3 then unzip hyperagent.zip and jre.zip to related path '4 invoke the start script of agent '5 upload the log file '1 downloadTempPath = agentSaveDir & "\" & installAgentScriptPathName agentPath = agentSaveDir & "\" & agentContextName startScriptFullName= agentPath & "\" & startAgentScriptName Wscript.Echo "begin delete file " & downloadTempPath & "\" & ftpAgentFileName retval=deleteFile(downloadTempPath & "\" & ftpAgentFileName) Wscript.Echo "end delete file "& downloadTempPath & "\" & ftpAgentFileName & " " & retval Wscript.Echo "begin delete file " & downloadTempPath & "\" & ftpJreName retval=deleteFile(downloadTempPath & "\" & ftpJreName) Wscript.Echo "end delete file "& downloadTempPath & "\" & ftpJreName & " " & retval Wscript.Sleep 1000 Wscript.Echo "begin delete directory " & agentPath retval=deleteDirectoryBat(agentPath) Wscript.Echo "end delete directory " & agentPath & " " & retval Wscript.Sleep 1000 '2 Wscript.Echo "begin download " & ftpAgentFileName retval= FTPDownload(ftpIp, ftpUserName , ftpUserPwd, downloadTempPath , ftpAgentPath,ftpAgentFileName) Wscript.Echo "end download " & ftpAgentFileName & " " & retval Wscript.Sleep 1000 Wscript.Echo "begin download " & ftpJreName retval= FTPDownload(ftpIp, ftpUserName , ftpUserPwd, downloadTempPath , ftpJrePath,ftpJreName) Wscript.Echo "end download " & ftpJreName & " " & retval Wscript.Sleep 1000 '3 Wscript.Echo "begin unzip " & ftpAgentFileName retval=unzipFile(downloadTempPath & "\" & ftpAgentFileName ,agentSaveDir) Wscript.Echo "end unzip " & ftpAgentFileName & " " & retval Wscript.Sleep 1000 Wscript.Echo "begin unzip " & ftpJreName retval=unzipFile(downloadTempPath & "\" & ftpJreName ,agentPath) Wscript.Echo "end unzip " & ftpJreName & " " & retval Wscript.Sleep 1000 '4 set objShell=wscript.createObject("wscript.shell") Wscript.Echo "begin start " & startScriptFullName retval=objShell.Run("cmd.exe /C " & startScriptFullName , 0, TRUE) Wscript.Echo "end start " & startScriptFullName & " " & retval '5 if Err.number<>0 then Wscript.Echo Err.Number & Err.description Err.clear uploadAgentlog("install script error " & Err.Number & Err.description & " ,stop install") Else Wscript.Sleep 3000 retval =checkPortIsInUser(agentPort) If retval = true Then Wscript.Echo "agent start success on port " & agentPort Else uploadAgentlog("agent installed but start error on port" & agentPort) End If end if End Function Function updateAgent() Dim retval,i Wscript.Sleep 4000 Call deleteFile(ftpAgentFileName) Wscript.Sleep 3000 i=1 do While i<=3 and Not retval Wscript.Echo i retval= downloadFile(ftpAgentFileName) i=i+1 Loop If retval = true Then 'msgbox "begin start jar file" 'Wscript.Sleep 1000 'CreateObject("WScript.Shell").Run "java -cp testagent.jar com.boco.agent.test.Main" ,vbhide 'set ws = createobject("wscript.shell") 'ws.run "cmd /c java -cp testagent.jar com.boco.agent.test.Main" ,vbhide 'msgbox "end start jar file" Wscript.Echo "agent update success" Else Wscript.Echo "update error:" & retval 'msgbox "update error:" & retval End if End Function 'Call uploadAgentlog("hello baby bdgfd") Call installAgent() 'Call checkPortIsInUser(21) 'Call unzipFile("D:\test\agent_install\dir2.zip" ,"D:\test\agent_install") 'unzipFile "D:\test\agent_install" "D:\test\agent_install\dir2.zip" 'agentPath = agentSaveDir & "\" & agentContextName ' Wscript.Echo "begin delete directory " & agentPath 'Call deleteDirectoryBat(agentPath) 'myline =checkPortIsInUser(22) 'myline = readFile("d:\test3\nn.txt") 'Wscript.Echo "myline:" & myline 'If InStr( myline,21) > 0 Then ' Wscript.Echo "port in user" 'Else ' Wscript.Echo "port not in user" 'End If