我正在使用 Linux 命令行,当我运行以下命令时:
tcpdump -n dst host destsitename
tcpdump -n dst host stackoverflow.com
查看我的服务器作为源是否与该域通信,我如何找出哪个进程从我的服务器作为源进行通信。
我的问题是我应该在“tcpdump”中使用哪个选项。
在 Linux 上,您还可以使用
ss
命令(它取代了已弃用的 netstat
命令):
$ ss -p dst stackoverflow.com
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
tcp ESTAB 0 0 192.168.2.5:50676 151.101.65.69:https users:(("firefox",pid=4657,fd=251))
netstat -avnp
并获取 <pid>
(最后一列)ps -ef | fgrep <pid>
看看 <pid>
属于什么如果你知道端口,你可以尝试:
lsof -i :1234
使用
lsof
代替 netstat
的好处是 Unix/OS X 不支持 -p
。
按站点名称使用 lsof 和 grep:
$: lsof -i |grep mapscii.me
$: telnet 16678 zersh 3u IPv4 1789302 0t0 TCP 192.168.21.180:43148->mapscii.me:telnet (ESTABLISHED)
或网络统计:
$ netstat anlpt |grep mapscii.me
tcp 0 0 192.168.21.180:43168 mapscii.me:telnet ESTABLISHED
尝试使用下一个脚本:
LOCAL_IP="src_ip"
TARGET_IP="..."
while read x; do
port=$( echo $x | grep "IP ${LOCAL_IP}" | awk '{print $3}' | sed "s/${LOCAL_IP}.//" )
if [ ! -z ${port} ]; then
lsof -Pni :${port}
fi
done <<< "$( tcpdump -nn -c1 host ${TARGET_IP} )"
PS。就我而言,它仅在后台起作用。挂在进程中超过10个小时寻找问题根源:
while read x; do port=$(echo $x | grep "IP ${LOCAL_IP}" | awk '{print $3}' | sed "s/${LOCAL_IP}.//"); if [ ! -z ${port} ]; then lsof -Pni :${port}; fi; done <<< "$( tcpdump -nn -c2 host ${TARGET_IP} )" >> /tmp/result &
ptcpdump 可以为您做到这一点:
$ sudo ptcpdump -i any -c 2 dst host stackoverflow.com
2024/05/18 14:13:58 capturing...
14:14:23.563846 wlp4s0 Out IP (tos 0x0, ttl 64, id 56758, offset 0, flags [DF], ip_proto TCP (6), length 60)
192.168.1.50.48686 > 104.18.32.7.443: Flags [S], cksum 0x4a22, seq 2370274194, win 64240, options [mss 1460,sackOK,TS val 3456162718 ecr 0,nop,wscale 7], length 0
Process (pid 1100440, cmd /usr/bin/curl, args curl https://stackoverflow.com)
14:14:23.767565 wlp4s0 Out IP (tos 0x0, ttl 64, id 56759, offset 0, flags [DF], ip_proto TCP (6), length 52)
192.168.1.50.48686 > 104.18.32.7.443: Flags [.], cksum 0x4a1a, seq 2370274195, ack 149658868, win 502, options [nop,nop,TS val 3456162921 ecr 3104647973], length 0
Process (pid 1100440, cmd /usr/bin/curl, args curl https://stackoverflow.com)
2 packets captured
2 packets received by filter
0 packets dropped by kernel