我在 Azure 上创建了一个 Ubuntu 16.04LTS VM,我需要访问其面向公众的 IP,以便配置我尝试在服务器上运行的 docker 容器。但是,当我从虚拟机中的终端执行以下命令时:
ip addr show
我只取回私有(private)本地 IP,而不取回面向公众的 IP。我需要采取哪些步骤才能从虚拟机的终端访问公共(public)IP?
我希望能够编写获取公共(public)IP的脚本的原因是我们正在VM上运行一个docker容器(我们正在创建多个VM,这是我们在JMeter中开发的负载测试框架的一部分)并且如果我们可以编写脚本获取 IP 并将其传递到服务器启动脚本中的 docker 容器,那么我们将能够自动化服务器启动,以便当我们从 Azure 按下启动服务器时,jmeter-server 可以使用正确的 IP 正常运行门户网站。如果无法从 bash 脚本访问公共(public) IP,我们就必须手动 SSH 到每台机器并运行 docker,并将公共(public) IP 作为参数传递给 docker 来启动每个 jmeter 服务器,这将更加耗时。
最佳答案
AWS 和 Azure 中都有这个虚拟机元数据:
https://azure.microsoft.com/en-us/blog/what-just-happened-to-my-vm-in-vm-metadata-service/
但目前该 feed 返回的信息非常少:
UbuntuInJapan:~$ curl -s http://169.254.169.254/metadata/latest/InstanceInfo
{"ID":"_UbuntuInJapan","UD":"0","FD":"0"}
...只是升级和故障域,对您的努力毫无用处。
对于公共(public) IP,我通常这样做:
#!/bin/bash
PUBLIC_IP=$(curl -s http://checkip.amazonaws.com || printf "0.0.0.0")
# Then you either get the public IP address in the variable or you get 0.0.0.0
# which means you could not make the call to Amazon or Amazon is under DDoS.
# amirite?
# Uncomment to echo (AKA debug mode)
echo $PUBLIC_IP
您可以链接多个我的 ip 是什么? 服务来提高您的 SLA。 即:
# First one that works wins.
# You can improve this by regex checking the output.
#
# Something like:
#
# if [[ $PUBLIC_IP =~ [[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\/ ]]
# then echo 'Good. Valid IPv4. Continue.'
# else echo 'Bad. Stop.'
# fi
UbuntuInJapan:~$ curl -s http://checkip.amazonaws.FAILS || curl -s http://canihazip.com/s
13.78.92.21
如果您与某些服务交谈 IPv6,某些服务将返回 IPv6,请确保将地址系列显式传递给 curl
:
$ curl -4 http://l2.io/ip
13.78.92.21
$ curl -6 http://l2.io/ip
2a02:2f0b:4xxx:fxxx:4xxx:xxxx:xxxx:1072
关于linux - 访问 Linux VM 内的 Azure 公共(public) IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39578179/