Network - pdorobisz/cheatsheets GitHub Wiki

Checking open ports

Using lsof:

# list all TCP ports in 'LISTEN' state
sudo lsof -nP -iTCP -sTCP:LISTEN

# check process listening on TCP port 3306
sudo lsof -nP -iTCP:3306 -sTCP:LISTEN

# list all network connections for process 98765
# -a - AND, causes all selection options to be ANDed
# -p - PID
# -i - selects listing of network files
# -n - prevent hostname resolution
# -P - prevent port name resolution 
lsof -ai -p 98765 -n -P

Using netstat:

# list all TCP and UDP ports
sudo netstat -tunlp

# list all TCP connections (MacOS)
# -a - show sockets in all states
# -n - prevent hostname resolution
# -p - protocol (as in /etc/protocols)
netstat -an -p tcp

Scanning network and ports

# find open ports on 192.168.1.1
nc -z -v 192.168.1.1 1-65535  2>&1|grep succeeded

# find hosts in network
sudo nmap -sn 192.168.1.0/24

# more detailed host scanning
sudo nmap -Pn 192.168.1.0/24