ใช้netstat -a -o -n ฉันสามารถรับรายการพอร์ตและ PID
ฉันต้องไปที่ตัวจัดการงานและเพิ่ม PID แล้วดูว่ามันคือใคร (น่าผิดหวังมาก)

ผมก็สงสัยว่ามีคำสั่ง CMD ซึ่งไม่ได้ทั้งหมด (โดยใช้find, for, powershell)
เพื่อที่ฉันจะได้รับชื่อกระบวนการ
ใช้netstat -a -o -n ฉันสามารถรับรายการพอร์ตและ PID
ฉันต้องไปที่ตัวจัดการงานและเพิ่ม PID แล้วดูว่ามันคือใคร (น่าผิดหวังมาก)

ผมก็สงสัยว่ามีคำสั่ง CMD ซึ่งไม่ได้ทั้งหมด (โดยใช้find, for, powershell)
เพื่อที่ฉันจะได้รับชื่อกระบวนการ
คำตอบ:
ใช้-bพารามิเตอร์:
-b Displays the executable involved in creating each connection or
listening port. In some cases well-known executables host
multiple independent components, and in these cases the
sequence of components involved in creating the connection
or listening port is displayed. In this case the executable
name is in [] at the bottom, on top is the component it called,
and so forth until TCP/IP was reached. Note that this option
can be time-consuming and will fail unless you have sufficient
permissions.
หมายเหตุnetstat -bคำสั่งจะล้มเหลวเว้นแต่วิ่งจากพรอมต์คำสั่งยกระดับ
กรองรายการกระบวนการและค้นหา PID ที่คุณสนใจ:
tasklist | findstr /c:"PID"
คุณสามารถใช้Tcpvcon.exeแทน ไม่มีสิทธิ์ของผู้ดูแลระบบ
การใช้Tcpvconนั้นคล้ายคลึงกับ
netstatยูทิลิตีWindows ในตัว
Usage: tcpvcon [-a] [-c] [-n] [process name or PID]
-a Show all endpoints (default is to show established TCP connections).
-c Print output as CSV.
-n Don't resolve addresses.
ฉันคิดว่าคุณกำลังมองหาTCPViewจาก SysInternals
นี่คือตัวอย่างสำหรับหน้าต่างที่ใช้FORในการแยกวิเคราะห์netstatผลลัพธ์จากนั้นDO tasklistมี/fiตัวกรองบน pid เพื่อแสดงชื่อกระบวนการ
การค้นหาล่าสุดคือการลบtasklistส่วนหัว
FOR /F "usebackq tokens=5 delims= " %i IN (`netstat -ano ^|find "443"`) DO @tasklist /fi "pid eq %i" | find "%i"
พิมพ์บันทึกผลลัพธ์เช่น
tomcat8.exe.x64 4240 Services 0 931,864 K
สามารถเพิ่มฟิลด์เพิ่มเติมnetstatได้โดยเพิ่มโทเค็น
findเพื่อกรองพอร์ต (ในทางตรงกันข้ามแม้ว่าnetstat -bสามารถระบุชื่อกระบวนการได้โดยตรง แต่การค้นหาผลลัพธ์ด้วยตนเองเพื่อค้นหาด้วยตนเอง 2. ใช้คำสั่งดั้งเดิมของ Windows เท่านั้นซึ่งยืดหยุ่นและเป็นอิสระมากกว่า
findstrกับ/Rตัวเลือกแทนที่จะfindใช้ regex เพื่อการค้นหาที่ดีขึ้น 2. เพื่อใช้:443 *[[0-9]"เป็นรูปแบบในการกรองพอร์ตในเครื่องเท่านั้น คำสั่งทั้งหมดอาจเป็นFOR /F "usebackq tokens=5 delims= " %i IN (`netstat -ano ^|findstr /R /C:":443 *[[0-9]"`) DO @tasklist /fi "pid eq %i" | findstr "%i"
netstatสามารถเพิ่มได้โดยการเพิ่มโทเค็น" หรือไม่?
หากคุณชื่นชอบการใช้ PS คุณสามารถแยกรหัสนี้ได้ (หมายเหตุ: เป็นขั้นพื้นฐาน)
$nets = netstat -ano | select-string LISTENING
foreach($n in $nets){
# make split easier PLUS make it a string instead of a match object:
$p = $n -replace ' +',' '
# make it an array:
$nar = $p.Split(' ')
# pick last item:
$pname = $(Get-Process -id $nar[-1]).ProcessName
$ppath = $(Get-Process -id $nar[-1]).Path
# print the modified line with processname instead of PID:
$n -replace "$($nar[-1])","$($ppath) $($pname)"
}
โปรดทราบว่าคุณสามารถลองPathแทนที่จะProcessNameใช้เส้นทางแบบเต็มได้ซึ่งไม่สามารถทำงานกับบริการของระบบได้ นอกจากนี้คุณอาจต้องการผนวกส่วนProcessNameท้ายของบรรทัดแทนการแทนที่ค่า PID
สนุกกับมัน ;)
ลองใช้สิ่งนี้ ...
ชื่อกระบวนการที่มีการประทับเวลา :) ใน oneliner ... ไม่จำเป็นต้องมีสคริปต์ที่ง่ายและรวดเร็ว ...
คุณสามารถเปลี่ยนพารามิเตอร์ SYN_SENT โดย ESTABLISHED หรือ LISTENING
filter timestamp {"$(Get-Date -Format G): $_"};netstat -abno 1 | Select-String -Context 0,1 -Pattern LISTENING|timestamp
filter timestamp {"$(Get-Date -Format G): $_"};netstat -abno 1 | Select-String -Context 0,1 -Pattern SYN_SENT|timestamp
Erik Bitemo ดีมาก! ฉันคิดว่าจะเพิ่มตัวแปรสำหรับเส้นทางแล้วฉันก็รู้ว่าคุณมีอยู่แล้วถึงแม้ว่ามันจะไม่ได้กำหนดไว้ ดังนั้นรหัสที่ฉันนำมาใช้ใหม่คือ:
$nets = netstat -ano |select-string LISTENING;
foreach ($n in $nets)
{
# make split easier PLUS make it a string instead of a match object
$p = $n -replace ' +',' ';
# make it an array
$nar = $p.Split(' ')
# pick last item...
$pname = $(Get-Process -id $nar[-1]).ProcessName
$ppath = $(Get-Process -id $nar[-1]).Path;
# print the modified line with processname instead of PID
$n -replace "$($nar[-1])","$($ppath) $($pname)" | where {$pname -like "*GMSVP*"}
}
ฉันพยายามค้นหากระบวนการและบริการสำหรับแอปพลิเคชันที่ฉันใช้ 2 ซับที่แตกต่างกันบ้าง
Get-Service | select status,name,displayname,servicename | where {($_.DisplayName -like "myserv*") -or ($_.servicename -like "post*")} | ft -auto
Get-Process | select id, processname,cpu,path,description | where {$_.path -like "*myserv*"} | ft -auto
GetService Get-Process