Useful Commands/Networking

From Fundamental Ramen
Jump to navigation Jump to search

https://www.opencli.com/linux/ip-command

情境 指令
Which service bound tcp port?
sudo netstat -tunlp
sudo netstat -tnlp | grep :22
sudo ss -tunlp
sudo lsof -nP -iTCP -sTCP:LISTEN
顯示執行中的網路服務
# List listening tcp ports and what program used.
lsof -i -P | grep -i "listen"

# OSX, FreeBSD 專用
netstat -anf inet | grep 'LISTEN'  # 只看 ipv4
netstat -anf inet6 | grep 'LISTEN' # 只看 ipv6
netstat -anf inet | grep '127\..*LISTEN'     # 只看 ipv4 本地服務
netstat -anf inet | grep '\*\.[0-9].*LISTEN' # 只看 ipv4 開放服務
netstat -anf inet | grep '127\..*LISTEN' | sort -t. -nk5     # 只看 ipv4 本地服務, 依 port 排序
netstat -anf inet | grep '\*\.[0-9].*LISTEN' | sort -t. -nk2 # 只看 ipv4 開放服務, 依 port 排序
netstat -anf inet -p udp | awk '{ print $4 }' | grep '[0-9]$' # List udp4 services

# Linux 專用 (參數 t 只看 TCP)
ss -lnt4
netstat -lnt4 # 只看 ipv4
netstat -lnt6 # 只看 ipv6
netstat -lnt4 | grep '127\.' | sort -t: -nk2  # 只看 ipv4 本地服務, 依 port 排序
netstat -lnt4 | grep '0:[^*]' | sort -t: -nk2 # 只看 ipv4 開放服務, 依 port 排序

# Busybox 專用 (不支援 -4 -6 參數)
netstat -lnt | grep '127\.' | sort -t: -nk2  # 只看 ipv4 本地服務, 依 port 排序
netstat -lnt | grep '0:[^*]' | sort -t: -nk2 # 只看 ipv4 開放服務, 依 port 排序

# 通用
netstat -an | grep 'LISTEN\s*$'
取得 HTTP 回應代碼
curl -s -w '%{http_code}\n' -o /dev/null <URL>
wget --server-response --spider <URL> 2>&1 | awk '/^  HTTP/{print $2}'
執行遠端的 Shell Script
curl -s <URL> | bash
wget -qO- <URL> | bash
下載檔案,由伺服器命名
curl -sOJ <URL>
wget --trust-server-names <URL>
片段下載
Server 應該回傳代碼 206
(wget 不支援)
# 簡易用法
curl -r 0-99 -X HEAD -I <URL>      # 檢查 response header
curl -r 0-99 -s <URL>              # 直接輸出片段內文
curl -r 0-99 -sO <URL>             # 輸出到檔案,使用 <URL> 結尾命名
curl -r 0-99 -sOJ <URL>            # 輸出到檔案,使用 Content-Disposition: attachment; filename=... 命名
curl -r 0-99 -o part1.bin -s <URL> # 輸出到檔案,自己命名

# 檢查往來 header
curl -r 0-99 -X HEAD -svI <URL> 2>&1 | grep '^[<>]'

# 檢查是否支援片段下載 (只取 header 不取 body)
# * 206: 支援
# * 200: 不支援
curl -r 0-99 -X HEAD -w '%{http_code}\n' -o /dev/null -sI <URL>
取 HTTP Header
(如:確認 Last-Modified)
curl -I http://data.openstreetmapdata.com/land-polygons-split-4326.zip
wget -qS --spider http://data.openstreetmapdata.com/land-polygons-split-4326.zip
壓力測試
ulimit -n 10000; ab -c 500 -n 1000 <URL>
LDAP 簡易搜尋
ldapsearch \
   -H ldap://<host> \
   -D '<user>' \
   -w '<password>' \
   -b '<basedn>' \
   '(objectClass=*)'
sendmail 打信
sendmail -Am -f"<123@123.com>" "<haha@softnext.com.tw>" < /home/adm/151103/mail20151103060502002.eml
顯示遠端 samba 檔案
smbclient -U smail%123 //192.168.90.154/smail -c 'dir'