2016-09-19 106 views
1

我在Azure上创建了一个Ubuntu 16.04LTS虚拟机,我需要访问其面向公众的IP才能配置我试图在服务器上运行的Docker容器。然而,当我做下面的命令从VM中的终端:在Linux VM中访问Azure公共IP

ip addr show 

我只拿回私人本地IP的,而不是面向公众的IP。我需要采取哪些措施才能从VM终端访问公共IP?

我希望能够获取公共IP的脚本的原因是我们在虚拟机上运行一个docker容器(我们正在创建多个虚拟机,这是我们在JMeter中开发的一个负载测试框架的一部分)如果我们可以编写脚本获取IP并将它传递给服务器启动脚本中的docker容器,那么我们将能够自动启动服务器启动,以便当我们按下启动服务器时,使用正确的IP运行jmeter-server。 Azure门户。如果不能从bash脚本访问公共IP,我们必须手动SSH到每台机器上并运行docker,并将公共IP作为参数传递给docker以启动每个jmeter服务器,这将耗时更多。

+0

你需要击出了一些外面的网址告诉你连接来自的IP是什么,例如,击中“什么是我的IP”网站。也许天青有一个webservice,你可以打到告诉你,而不需要“走出去”。 –

+0

感谢您的建议!我在Azure的文档中花费了最后一两个小时,似乎并没有像这样的东西。基于你的建议,我能够找到“http://www.linuxtrainingacademy.com/determine-public-ip-address-command-line-curl/”,然后运行“curl ifconfig.me”能够让我我的公共IP。谢谢! –

回答

3

有此VM元数据东西都AWS和Azure的:
https://azure.microsoft.com/en-us/blog/what-just-happened-to-my-vm-in-vm-metadata-service/

,但目前有由源返回的信息非常少:

UbuntuInJapan:~$ curl -s http://169.254.169.254/metadata/latest/InstanceInfo 

{"ID":"_UbuntuInJapan","UD":"0","FD":"0"} 

...只是升级和故障域,这对你的努力毫无用处。

对于公网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 
+0

'curl ifconfig.co'如果你还需要一个:D。 –