netstat ports code example

Example 1: linux how to see ports in use

sudo netstat -tulpn | grep LISTEN

sudo lsof -i:22 # see a specific port such as 22

Example 2: linux see used ports

netstat -tulpn

Example 3: netstat check port

sudo netstat -plnt

Example 4: netstat command ports

$netstat = netstat -aon | Select-String -pattern "(TCP|UDP)"
$processList = Get-Process

foreach ($result in $netstat) {
   $splitArray = $result -split " "
   $procID = $splitArray[$splitArray.length – 1]
   $processName = $processList | Where-Object {$_.id -eq $procID} |    select processname
   $splitArray[$splitArray.length – 1] = $procID + " " +      $processName.processname
   $splitArray -join " "
}