Windows 2003 Shell Scripting
Shell Scripting im Netzwerk
Port-Liste aus netstat
Dieses Script stammt aus einem Projekt zur Überwachung eines Servers. Es sollte festgestellt werden, zu welchen Prozessen von außen Verbindungen bestanden. Die Ausgabe liefert eine Liste aller Prozesse mit aktiven TCP-Verbindungen, der entfernten Adresse (IP und Port) und der im Prozess laufenden Services. So bekommen Sie schnell einen Überblick, wer von außen mit welchen Prozessen und Diensten in Verbindung steht.
Nachfolgend finden Sie einen (aus Platzgründen gekürzten) Auszug aus der Ausgabe des Shell Scripts.
==== Process ====
dfssvc.exe 1324 Dfs
Connections:
bytebag.shellbook.com:1025
bytebag.shellbook.com:1025
bytebag.shellbook.com:epmap
==== Process ====
dns.exe 1360 DNS
Connections:
bytebag.shellbook.com:ldap
==== Process ====
ntfrs.exe 1464 NtFrs
Connections:
bytebag.shellbook.com:1025
bytebag.shellbook.com:1025
bytebag.shellbook.com:ldap
Code
@echo off
setlocal
netstat -p TCP -a -o | find /i "established" > %temp%\
x.x
if exist %temp%\y.y del %temp%\y.y
for /F "tokens=3,5" %%f in (%temp%\x.x) do echo %%g %%f
>> %temp%\y.y
sort < %temp%\y.y > %temp%\x.x
set pid=-1
for /F "tokens=1,2" %%f in (%temp%\x.x) do call
:procline %%g %%f
endlocal
if exist %temp%\x.x del %temp%\x.x
if exist %temp%\y.y del %temp%\y.y
goto :eof
:procline
if %pid% == %2 goto havePID
echo.
echo.
echo ==== Process ====
tasklist /FI "pid eq %2" /NH /SVC
echo.
echo Connections:
:havePID
set pid=%2
echo %1
goto :eof