Link to home
Start Free TrialLog in
Avatar of NIS_Longfellow
NIS_Longfellow

asked on

Run Netstat Remotely using WMI

I need to run the netstat command on a remote box and output the info to a text file.  I currently have everything except how to run the command remotely.  See below.

' Variable Declaration
Const ForAppending = 8            ' file mode for appending
' Error Checking
On Error Resume Next
' Variable Init
strUser = "iusr_metro"            ' administrator id
strPassword = "MIOrchid#1"      ' administrator password
' Injector Machine List
strMachines = "OH309APL2008.cdc.ent.nwie.net"
' Create a Text File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\temp\comprept.txt", ForAppending, True)
aMachines = split(strMachines, ";")

For Each machine in aMachines
      ' Remote Login
      Set objLocator = CreateObject( "WbemScripting.SWbemLocator" )
      Set objWMIService = objLocator.ConnectServer (machine, "root/cimv2", strUser, strPassword)
      objWMIService.Security_.impersonationlevel = 3
      
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
          
      objTextFile.WriteLine("### Injector Report: " & machine & " ###")

      'remote netstat code here... I hope
   
          ' Writes 2 Blank Lines after each injector report.
          objTextFile.WriteBlankLines(2)
Next
' Text Log File Close
objTextFile.Close
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi,
I have tried to get the SWBemLocator to do the job, but could not.
The only way I could get it to work was to use PSExec.  Try the following script, replacing the relevant paths in it with paths of your own...

'=============
' Variable Declaration
' Error Checking
'On Error Resume Next
' Variable Init
strUser = "domain/username"            ' administrator id
strPassword = "password"      ' administrator password
' Injector Machine List
strMachines = "machine1;machine2"
' Create a Text File
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileName = "\\localpc\c$\temp\z_comprept.txt"
strPSExecPath = "\\server\share\psexec.exe"
If objFSO.FileExists(strFileName) = False Then
      Set objOutputFile = objFSO.CreateTextFile(strFileName, True)
      objOutputFile.Close
      Set objOutputFile = Nothing
End If
strFileName = objFSO.GetFile(strFileName).ShortPath
strPSExecPath = objFSO.GetFile(strPSExecPath).ShortPath

strRemoteCommands = "cmd /c netstat -a >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
      
aMachines = split(strMachines, ";")

For Each machine in aMachines
      ' Remote Login
      Set objOutputFile = objFSO.CreateTextFile("\\" & machine & "\c$\Temp\nbtstat_batch.bat", True)
      objOutputFile.Write strRemoteCommands
      objOutputFile.Close
      Set objOutputFile = Nothing
      strCommand = "cmd /c " & strPSExecPath & " -accepteula -d -i -u " & strUser & " -p " & strPassword & " \\" & machine & " C:\Temp\nbtstat_batch.bat"
      objShell.Run strCommand, 0, True
      objFSO.DeleteFile "\\" & machine & "\c$\Temp\nbtstat_batch.bat", True
Next

MsgBox "Done"
'=============

Regards,

Rob.
Avatar of NIS_Longfellow
NIS_Longfellow

ASKER

When I attempt to run the following two lines give me problems.  
I get a path not found error.  The temp folder does exist on the remote machine.  So I'm not sure what it issue is.

      Set objOutputFile = objFSO.CreateTextFile("\\" & machine & "\c$\Temp\nbtstat_batch.bat", True)
      objFSO.DeleteFile "\\" & machine & "\c$\Temp\nbtstat_batch.bat", True
MY FULL CODE

'=============
' Variable Declaration
' Error Checking
'On Error Resume Next
' Variable Init
strUser = "iusr_metro"            ' administrator id
strPassword = "MIOrchid#1"      ' administrator password
' Injector Machine List
strMachines = "oh309apl2008"
' Create a Text File
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileName = "\\localpc\c$\temp\z_comprept.txt"
strPSExecPath = "\\localpc\c$\Documents and Settings\longfer\My Documents\LONGFER\SysInternals\psexec.exe"
If objFSO.FileExists(strFileName) = False Then
      Set objOutputFile = objFSO.CreateTextFile(strFileName, True)
      objOutputFile.Close
      Set objOutputFile = Nothing
End If
strFileName = objFSO.GetFile(strFileName).ShortPath
strPSExecPath = objFSO.GetFile(strPSExecPath).ShortPath

strRemoteCommands = "cmd /c netstat -a >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
strRemoteCommands = strRemoteCommands & VbCrLf & "echo "" >> " & strFileName
     
aMachines = split(strMachines, ";")

For Each machine in aMachines
      ' Remote Login
      Set objOutputFile = objFSO.CreateTextFile("\\" & machine & "\c$\Temp\nbtstat_batch.bat", True)
      objOutputFile.Write strRemoteCommands
      objOutputFile.Close
      Set objOutputFile = Nothing
      strCommand = "cmd /c " & strPSExecPath & " -accepteula -d -i -u " & strUser & " -p " & strPassword & " \\" & machine & " C:\Temp\nbtstat_batch.bat"
      objShell.Run strCommand, 0, True
      objFSO.DeleteFile "\\" & machine & "\c$\Temp\nbtstat_batch.bat", True
Next

MsgBox "Done"
1st Error - Line 16 "Path Not found" -
Set objOutputFile = objFSO.CreateTextFile(strFileName, True)
psexec.exe is located on my local machine
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin