0

    Mac电脑使用:Mac电脑查看本机的IP和公网IP的方法

    2023.08.10 | admin | 137次围观

    在程序开发的道路上会遇到各种各样的问题,会涉及到各个方面查电脑本机的ip地址,有时候的问题不局限于开发本身,也有可能是附带的问题,但是都是属于程序开发范畴。那么今天就来分享一下和程序开发不想管的但是有属于程序开发范畴的内容,关于Mac电脑如何查看本机IP和公网IP的方法。

    一、Mac电脑查看本机IP

    Mac电脑查询本机IP的方式,直接在Mac自带终端里面输入以下命令行:

    ifconfig

    回车,即可查看到本机的所有网络设备信息,其中也包含了本机IP的信息。通过ifconfig查询出来的信息有很多内容,具体的信息如下所示:

    lo0: flags=8049 mtu 16384options=1203inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 nd6 options=201gif0: flags=8010 mtu 1280stf0: flags=0<> mtu 1280en0: flags=8863 mtu 1500options=400ether ac:b1:1a:01 inet6 fe80::c3e:2c0c7%en0 prefixlen 64 secured scopeid 0x4 inet 172.20.1.2 netmask 0xfffffff0 broadcast 172.20.1.15nd6 options=201media: autoselectstatus: activeen1: flags=8963 mtu 1500options=460ether 82:178:fe:40 media: autoselect status: inactiveen2: flags=8963 mtu 1500options=460ether 82:17:0fe:41 media: autoselect status: inactivebridge0: flags=8863 mtu 1500options=63ether 82:17:04fe:40 Configuration:id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0ipfilter disabled flags 0x0member: en1 flags=3        ifmaxaddr 0 port 5 priority 0 path cost 0member: en2 flags=3        ifmaxaddr 0 port 6 priority 0 path cost 0nd6 options=201media: status: inactivep2p0: flags=8843 mtu 2304options=400ether 0e:bc:32a:01 media: autoselectstatus: inactiveawdl0: flags=8943 mtu 1484options=400ether 6a::92:ad inet6 fe80::684ac:92ad%awdl0 prefixlen 64 scopeid 0x9 nd6 options=201media: autoselectstatus: activellw0: flags=8863 mtu 1500options=400ether 6a:4b:2:ad inet6 fe80::ff:feac:92ad%llw0 prefixlen 64 scopeid 0xa nd6 options=201media: autoselectstatus: activeutun0: flags=8051 mtu 1380inet6 fe80::8bf7:39d5%utun0 prefixlen 64 scopeid 0xb nd6 options=201utun1: flags=8051 mtu 2000inet6 fe80::4c64:b6:c22f%utun1 prefixlen 64 scopeid 0xc nd6 options=201


    主要选项解释:

    lo0:loopback回送地址,是一种广泛的虚接口;

    gif0:软件网络接口;

    stf0:ipv6—>ipv4通道接口;

    en0:以太网0,有线网卡查电脑本机的ip地址,电脑本机IP就包含在此处;

    en1:~en2:同上,都是有线网卡,以太网1~2;

    bridge0:第二层桥接;

    p2p0:Point-to-Point协议;

    awdl0:airdroppeertopeer,苹果设备特有的;

    llw0:暂无解释;

    utun0~utun1:networksetup-listallhardwareports;

    查询本机的IP地址,也可以直接通过命令行:

    $ ifconfig en0 

    直接查询本机当前的第一块网卡的IP地址,也就是本机的IP地址,这样可以直接查看对应的IP即可,其他的无关网络设置信息就忽略了,方便查阅。

    二、Mac电脑查看公网IP

    为什么要查看本机的公网IP?使用场景是什么?

    使用场景:测试ES被攻击,需要通过添加IP白名单才能访问的场景。

    查看公网IP方法:在Mac电脑下查看本机的公网IP,常用的方法很简单,直接在Mac电脑自带的终端下面输入以下命令行:

    curl ifconfig.me 

    回车,即可查到本机的公网IP。Linux操作系统下查看本机公网IP的方法也是同上所述,直接输入命令行即可查到。

    知识点延伸

    Mac OS/Linux操作系统通过命令行获取本机外网IP的命令行汇总:

    $ curl ifconfig.me$ curl icanhazip.com$ curl ident.me$ curl ipecho.net/plain$ curl whatismyip.akamai.com$ curl tnx.nl/ip$ curl myip.dnsomatic.com$ curl ip.appspot.com$ curl -s checkip.dyndns.org | sed 's/.*IP Address: \([0-9\.]*\).*/\1/g'

    根据上述查询本机的公网IP,举两个有代表性的示例。

    1、通过icanhazip.com 获取本机的公网IP地址

    icanhazip.com是一个网址,直接在电脑浏览器中输入该网址,就可以直接获取本机的公网IP地址。

    2、通过命令行curl icanhazip.com来获取本机的公网IP地址

    [root@ ]# curl icanhazip.com110.104.210.45

    该获取公网IP的方法等同于在网址上获取本机的公网IP。

    以上就是本章全部内容,欢迎关注三掌柜的微信公众号“程序猿by三掌柜”,三掌柜的新浪微博“三掌柜666”,欢迎关注!

    版权声明

    本文仅代表作者观点。
    本文系作者授权发表,未经许可,不得转载。

    发表评论