【问题标题】:automated retrieval of the external ip address of all of my current connections自动检索我所有当前连接的外部 IP 地址
【发布时间】:2021-12-30 10:05:08
【问题描述】:

我正在尝试制作一个程序来自动列出从路由器外部到我的计算机的所有连接。该脚本的最终目标是,我希望能够获得我连接到的每个服务器/网站的外部 IP 地址的干净列表。我也试图以此作为一种方式来了解更多关于网络、网站和服务器如何工作的信息,因此对于我在术语和一般知识方面所犯的任何错误,我深表歉意!

我的 tcpdump bash 脚本:

while :
do
# get myip and assign it to a variable
myip="$(ifconfig wlp2s0 | grep -E -o -m 1 "inet................" | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)")"


# tcpdump on my ip for all packets going to or from my ip address. the ipaddress of the packets is placed in IP Address.txt
sudo tcpdump -c 1 -nn host "$myip" | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" >> IPaddress.txt


done

我认为 tcpdump 将是解决此问题的工具,但我承认我不知道 tcpdump 是如何工作的。该脚本是我通过 ubuntu 运行的 bash 文件。我将如何使用 tcpdump 收集我连接到的每个网站的 IP 地址?我阅读了 tcpdump 文档,相信它可以帮助我实现目标,但是如果有更好的工具,我很想听听!目前,此代码仅显示内部 IP 地址。 ;(

【问题讨论】:

    标签: bash networking tcpdump


    【解决方案1】:

    我更倾向于使用ssnetstat

    ss --all --ipv4 
    

    将显示所有 IPv4 连接。

    当然,IPv6 也是如此;如果需要,您可以添加多个参数之一以获取更详细的信息,例如 --processes--extended--info

    还有一些参数可以控制输出格式,使其更适合解析:

    ss --all --ipv4 --processes --no-header --oneline
    

    【讨论】:

      【解决方案2】:

      建议遵循ss 命令。 了解ss 命令here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-14
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多