在程序开发的道路上会遇到各种各样的问题,会涉及到各个方面查电脑本机的ip地址,有时候的问题不局限于开发本身,也有可能是附带的问题,但是都是属于程序开发范畴。那么今天就来分享一下和程序开发不想管的但是有属于程序开发范畴的内容,关于Mac电脑如何查看本机IP和公网IP的方法。
一、Mac电脑查看本机IP
Mac电脑查询本机IP的方式,直接在Mac自带终端里面输入以下命令行:
ifconfig
回车,即可查看到本机的所有网络设备信息,其中也包含了本机IP的信息。通过ifconfig查询出来的信息有很多内容,具体的信息如下所示:
lo0: flags=8049
mtu 16384 options=1203
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=201
gif0: flags=8010
mtu 1280 stf0: flags=0<> mtu 1280
en0: flags=8863
mtu 1500 options=400
ether 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.15
nd6 options=201
media: autoselect
status: active
en1: flags=8963
mtu 1500 options=460
ether 82:178:fe:40
media: autoselect
status: inactive
en2: flags=8963
mtu 1500 options=460
ether 82:17:0fe:41
media: autoselect
status: inactive
bridge0: flags=8863
mtu 1500 options=63
ether 82:17:04fe:40
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x0
member: en1 flags=3
ifmaxaddr 0 port 5 priority 0 path cost 0
member: en2 flags=3
ifmaxaddr 0 port 6 priority 0 path cost 0
nd6 options=201
media:
status: inactive
p2p0: flags=8843
mtu 2304 options=400
ether 0e:bc:32a:01
media: autoselect
status: inactive
awdl0: flags=8943
mtu 1484 options=400
ether 6a::92:ad
inet6 fe80::684ac:92ad%awdl0 prefixlen 64 scopeid 0x9
nd6 options=201
media: autoselect
status: active
llw0: flags=8863
mtu 1500 options=400
ether 6a:4b:2:ad
inet6 fe80::ff:feac:92ad%llw0 prefixlen 64 scopeid 0xa
nd6 options=201
media: autoselect
status: active
utun0: flags=8051
mtu 1380 inet6 fe80::8bf7:39d5%utun0 prefixlen 64 scopeid 0xb
nd6 options=201
utun1: flags=8051
mtu 2000 inet6 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
's/.*IP Address: \([0-9\.]*\).*/\1/g' curl -s checkip.dyndns.org | sed
根据上述查询本机的公网IP,举两个有代表性的示例。
1、通过icanhazip.com 获取本机的公网IP地址
icanhazip.com是一个网址,直接在电脑浏览器中输入该网址,就可以直接获取本机的公网IP地址。
2、通过命令行curl icanhazip.com来获取本机的公网IP地址
[root@ ]# curl icanhazip.com
110.104.210.45
该获取公网IP的方法等同于在网址上获取本机的公网IP。
以上就是本章全部内容,欢迎关注三掌柜的微信公众号“程序猿by三掌柜”,三掌柜的新浪微博“三掌柜666”,欢迎关注!
版权声明
本文仅代表作者观点。
本文系作者授权发表,未经许可,不得转载。
发表评论