' Remote access wrapper and logger. ' v0.01 2007.02.07 - 1st version ' v0.02 2007.02.08 - use cmdline parameter to "solve" menu problem ' v0.03 2007.02.08 - implement process monitor [as exec don't work for cmdline rconip], add 3rd option ' v0.04 2007.02.12 - check/handle tempfile at start ' v0.05 2007.02.14 - added todos ' v0.06 2007.02.27 - done: don't ask to finish tempfile if no of running wscript processes >1 ' v0.06 2007.02.27 - done: handle/pass all other parameters passed to script ' v0.06 2007.02.27 - added "exeNotepad" (dummy application to log times) ' v0.07 2007.02.27 - added "exeIExplore" (dummy application to log times) - executes IE and opens $hostname ' v0.08 2007.03.03 - add mode "run" and "cd exeDIR" quirk for pcaw to avoid "security message". That is: ' "You have attempted to launch pcAnywhere from an invalid directory location. ' pcAnywhere files may only be run from within the pcAnywhere data directory structure." ' v0.09 2007.03.04 - fix exec ' v0.10 2007.03.20 - display varServer and varPrg at "task question" (to help identification) ' v0.11 2007.03.20 - display --||-- at "task end question", also display "todo" to help remembering... ' replaced raw text to language strings (to help localization later) ' v0.12 2007.03.29 - moved tempfile processing to very beginning (screwed up variables at wrong place...) 'Usage and samples: ' - rawl.vbs hostname/ip.address access-method [other options passed to application] ' - hostname: 192.168.0.1, localhost, WKS001, etc. ' - access-method: currently supported: vnc, pcaw, putty, rconip ' Ultravnc: http://ultravnc.sourceforge.net/ ' pcAnywhere: http://www.symantec.com/pcanywhere/Consumer/ ' Putty: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html ' Rconip (for NetWare): http://rconip.sourceforge.net ' rdp: remote desktop ' notepad: dummy application to log start/close times ' - other options such as port, username, password (Not recommended to store passwords in a link...) for example: ' vnc: /password MyPassword ' putty: -P port -l LoginName -pw MyPassword. LoginName can be also specified at hostname as: Loginname@hostname ' 'Credits: ' - Concept and "programming" by myself, gabor.funk@hunetkft.hu ' - www.google.com ' - www.w3schools.com ' - Process monitor snippet (for cmd window, like rconip) from: ' http://www.microsoft.com/technet/scriptcenter/resources/scriptshop/shop0505.mspx ' ' todo: ' - mail, webping ' - handle empty hh:mm if .tmp is used. ' - koltseghely?? (pl.: ns1.huweb.hu-n bonex related beallitas) ' esetleg hunet/uf.-en belul is kulon ktghely, pl. szerzodes/web ' - rdp password: http://www.issociate.de/board/post/264993/Terminal_Server_Question_-_Passing_Parameters.html ' - explorer / dummy ? ' ' --- FUNCTIONS and SUBS ------------------------------------------------------------------------------------------ function myISODate() mydate=now() myyear=DatePart("yyyy",mydate) mymonth=right("0"&DatePart("m",mydate),2) myday=right("0"&DatePart("d",mydate),2) myhour=right("0"&DatePart("h",mydate),2) mymin=right("0"&DatePart("n",mydate),2) mysec=right("0"&DatePart("s",mydate),2) myISODate=myyear &dd& mymonth &dd& myday &" "& myhour &":"& mymin &":"& mysec myISOtmpf=myyear & mymonth & myday & myhour & mymin & mysec end function function myLogData() strUserIn = InputBox(varMSG) On Error Resume Next Set objLogFile = objFSO.OpenTextFile(strFileName, 8, True) '1=read, 2=write, 8=append objLogFile.WriteLine datetime &t& varState &t& varServer &t& varPrg &t& strUserIn objLogFile.Close end function function do_runprocess() strCommand = exeRUN &" "& prehost & varServer & varOptions 'msgbox "run 1:"& exeRUN &" 2:"& prehost &" 3:"& varServer &" 'msgbox "run:"& strCommand &" mode:"& mode &" cd:"& objShell.CurrentDirectory Const NORMAL_WINDOW = 1 strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") '--- check run/exec method [run for pcaw] If mode = "run" then 'msgbox "run:"& strCommand &" mode:"& mode &" cd:"& objShell.CurrentDirectory Set oExec = objShell.Exec(strCommand) Do While oExec.Status = 0 : WScript.Sleep 2000 : Loop else 'msgbox "exec:"& strCommand &" mode:"& mode &" cd:"& objShell.CurrentDirectory Set objStartup = objWMIService.Get("Win32_ProcessStartup") Set objConfig = objStartup.SpawnInstance_ objConfig.ShowWindow = NORMAL_WINDOW Set objProcess = objWMIService.Get("Win32_Process") intReturn = objProcess.Create (strCommand, Null, objConfig, intProcessID) If intReturn = 0 Then Set colMonitorProcess = objWMIService.ExecNotificationQuery _ ("SELECT * FROM __InstanceDeletionEvent " & _ "WITHIN 1 WHERE TargetInstance ISA 'Win32_Process' " & _ "AND TargetInstance.ProcessId = '" & intProcessID & "'") Set objLatestEvent = colMonitorProcess.NextEvent strTimeDeleted = Now Else Wscript.Echo "Process could not be created." & vbCrLf & "Command line: " & _ strCommand & vbCrLf & "Return value: " & intReturn End If End If end function ' --- MAIN ------------------------------------------------------------------------------------ function do_main() strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objShell = wscript.createobject("wscript.shell") strFileName = objFSO.BuildPath(Wscript.ScriptFullName & "\..", logfile) strFileName = objFSO.GetAbsolutePathName(strFileName) Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process where name = 'wscript.exe' or name = 'cscript.exe'") i=0 : For Each objProcess In colProcess : i = i + 1 : Next If i < 2 Then 'check if temporary files are exist '---------------------------------------------------------------------------------- Set folderobj=objFSO.GetFolder(".") For Each tempfile in folderobj.Files If lcase(Right(tempfile.Name,3)) = "tmp" Then ' Wscript.Echo tempfile Set objTextFile = objFSO.OpenTextFile (tempfile, 1) ' Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline arrServiceList = Split(strNextLine , t) objTextFile.Close starttime=arrServiceList(0) : datetime=starttime varServer=arrServiceList(2) varPrg=arrServiceList(3) strUserPre=arrServiceList(4) varmsg= txt_unfinished & vbCrLf & vbCrLf _ & "starttime:" &t& starttime & vbCrLf _ & " server:" &t& varServer & vbCrLf _ & " access:" &t& varPrg & vbCrLf _ & " text:" &t& strUserPre & vbCrLf & vbCrLf & txt_specify strUserIn = InputBox(varMSG) pos=InStr(1,strUserIn," ",1) varDiff=strUserIn : strUserPost="" if pos >0 then varDiff=(Mid(strUserIn,1,pos-1)) : strUserPost=(Mid(strUserIn,pos)) : end if varDiff=varDiff & ":00" 'Add :00 seconds ' Wscript.Echo "-" & pos & "-" & varDiff & "-" & strUserPost ''todo: handle empty/nonnumeric/non h:mm format varDiff... (reinput) Set objLogFile = objFSO.OpenTextFile(strFileName, 8, True) objLogFile.WriteLine starttime &t& vardiff &t& varServer &t& varPrg &t& strUserPre &t& strUserPost objLogFile.Close 'wscript.echo tempfile 'Set objLogFile = objFSO.OpenTextFile(tempfile & ".tmp" , 8, True) Set objLogFile = objFSO.Getfile (tempfile) objLogFile.Delete end if Next end if : rem - end if for "check if temporary files are exist" '---------------------------------------------------------------------------------- 'After finished processing temp files varServer=WScript.Arguments(0) varPrg=WScript.Arguments(1) If WScript.Arguments.Count > 2 Then varOptions="" for i=3 to WScript.Arguments.Count : varOptions=varOptions &" "& Wscript.Arguments(i-1) : next end if mode="exec" : exeDIR="" select case varPrg case "vnc" : exeRUN=exeVNC case "rdp" : exeRUN=exeRDP : prehost="/v:" case "pcaw" : exeRUN=exePCAW : prehost="/C" : mode="run" exeDIR="C:\Documents and Settings\All Users\Application Data\Symantec\pcAnywhere" case "putty" : exeRUN=exePutty case "rconip" : exeRUN=exeRconip case "notepad" : exeRUN=exeNotepad case "iexplore" : exeRUN=exeIExplore case Else : msgbox "Unknown access type.",32 : Wscript.Quit(0) end select 'msgbox "srv:"& varServer & "prg:"& varPrg & " mode:"& mode &" argno:" & WScript.Arguments.Count '---------------------------------------------------------------------------------- 'Create temporary file on task opening varState="OPEN " : varMSG="[ " & varServer & "/" & varPrg & " ]" & vbCrLf & vbCrLf & txt_planned starttime=myISODate : datetime=starttime : tempfile=myISOtmpf strUserPre = InputBox(varMSG) On Error Resume Next Set objLogFile = objFSO.CreateTextFile (tempfile & ".tmp" , true) 'Set objLogFile = objFSO.OpenTextFile(tempfile & ".tmp" , 8, True) '1=read, 2=write, 8=append objLogFile.WriteLine datetime &t& varState &t& varServer &t& varPrg &t& strUserPre objLogFile.Close 'Run or exec process 'CD exeDir if exists. Required for PCAW to avoid display "security" message if exeDir <> "" then olddir=objShell.CurrentDirectory objShell.CurrentDirectory = exeDir do_runprocess objShell.CurrentDirectory = olddir else do_runprocess end if 'Close varState="CLOSE" : varMSG="[ " & varServer & "/" & varPrg & " ]" & vbCrLf & "[ " & strUserPre & " ]" & vbCrLf & vbCrLf & txt_taskcompl stoptime=myISODate : datetime=stoptime strUserPost = InputBox(varMSG) On Error Resume Next Set objLogFile = objFSO.OpenTextFile("debuglog" , 8, True) '1=read, 2=write, 8=append objLogFile.WriteLine datetime &t& varState &t& varServer &t& varPrg &t& strUserPost objLogFile.Close ''' todo: debuglog 'delete tempfile Set objLogFile = objFSO.Getfile (tempfile & ".tmp") objLogFile.Delete 'calculate time used varDiffSec=DateDiff("s",starttime,stoptime) varDiffMin=int(varDiffSec/60) : varDiffSec=varDiffSec-(60*varDiffMin) : varDiffSec=right("0"&varDiffSec,2) varDiffHour=int(varDiffMin/60) : varDiffMin=varDiffMin-(60*varDiffHour) : varDiffMin=right("0"&varDiffMin,2) varDiff=varDiffHour &":"& varDiffMin &":"& varDiffSec datetime=myISODate Set objLogFile = objFSO.OpenTextFile(strFileName, 8, True) ' objLogFile.WriteLine datetime &t& "SUM " &t& varServer &t& varPrg &t& varDiff objLogFile.WriteLine starttime &t& vardiff &t& varServer &t& varPrg &t& strUserPre &t& strUserPost objLogFile.Close end function ' --- START ------------------------------------------------------------------------------------ 'language strings: txt_usage = "usage: rawl.vbs server.or.ip.to access_type [options]" txt_planned = "Planned task?" txt_taskcompl = "Task completion comments?" txt_unfinished = "There is an unfinished session:" txt_specify = "Please specify time spent [hh:mm] and optional status text [eg.: 1:12 all ok]" If WScript.Arguments.Count < 2 Then WScript.Echo txt_usage 'Password is only works with applications accepting it, such as vnc and putty. 'Definitely not working with PCAnyWhere and RConIP Else ' --- variables ------ dim objWMIService, objFSO, objShell, strFileName, datetime dim varServer, varPrg, varState, varMSG, varOptions dim exeRUN, prehost, mode dim myISOtmpf na="-" : t=chr(9) : dd="." : rem empty, tab, date-delimiter logfile="logfile.txt" : prehost="" exeVNC="C:\Program Files\UltraVNC\vncviewer" exeRDP="mstsc.exe" ' exePCAW="C:\Program Files\Symantec\pcAnywhere\awrem32.exe C:\Documents and Settings\All Users\Application Data\Symantec\pcAnywhere\Network, Cable, DSL.chf" exePCAW="awrem32.exe ""Network, Cable, DSL.chf""" exePutty="C:\Program Files\Putty\Putty.exe" exeRconip="C:\Program Files\RconIP\RconIP.exe" exeNotepad="notepad.exe" exeIExplore="C:\Program Files\Internet Explorer\iexplore.exe" ' '(Ultra)Vnc is plain easy. Install and use. 'Remote Desktop is default installed in %systemroot%\system32 which is in the search path 'PCAnyWare is not making our life easier with this "forced" chf [residing in different ' directory than the exe] usage. Might need to edit accouring to your local locations. See: ' http://service1.symantec.com/SUPPORT/pca.nsf/e1aefd06d28fcec6852569e5004cef32/41bd8224a484646a882569f900728b50?OpenDocument 'SSH - Putty here - and Remote Console - RConIP for NetWare here - are "just tools", with no installation troubles and ' conveniences, so you would have to manually copy them to "C:\Program Files" or change directory here in the variables. ' ' --- end variables ------ do_main ' Clean up and exit. Set objFSO = Nothing End If Wscript.Quit(0)