英文:
Go to check EC2 environment? Or DNS domain name?
问题
我想查看程序是否在 EC2 上运行。
一种方法是在 EC2 上运行 hostname -d
命令来获取 DNS 域名
。
在 Go 语言中,如何获取这个 DNS 域名?
我查看了 net
包,使用了 http://golang.org/pkg/net/#LookupNS
但是我需要传递一个参数。
如何检查它是否在 EC2 上运行?
谢谢。
英文:
I want to see if the program is being run in EC2 or not.
One way is to run hostname -d
in EC2 to get the DNS domain name
.
How do I get this DNS domain name in Go.
I looked at the net
package using http://golang.org/pkg/net/#LookupNS
But I need to pass an argument.
How do I check if it's in EC2 or not?
Thanks
答案1
得分: 1
在我看来,正确的方法是从机器本身尝试访问元数据API,该API位于http://169.254.169.254/latest/meta-data
。令人担忧的是,你似乎需要在代码中了解这一点。我不太确定这个用例是什么,但我觉得你应该有一种在代码之外了解这个的方式。
不过,以下是代码部分的翻译:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
/* 如果你只需要主机名 */
name, _ := os.Hostname()
fmt.Println(name)
/* 如果你必须访问EC2元数据API */
client := http.Client{
Timeout: time.Duration(2 * time.Second),
}
resp, err := client.Get("http://169.254.169.254/latest/meta-data/public-hostname")
if err != nil {
fmt.Println("可能不在EC2上")
fmt.Println(err)
return
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
希望对你有帮助!
英文:
The right way, IMO, is to try and hit the metadata API at http://169.254.169.254/latest/meta-data
from the machine itself. The worrisome part is that you feel the need to know this in code. I am not quite sure what the use case for this is but it seems to me that there ought to be a way for you to know this outside of your code.
Nevertheless:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
/* if you just need the hostname */
name, _ := os.Hostname()
fmt.Println(name)
/* if you must hit the EC2 metadata API */
client := http.Client{
Timeout: time.Duration(2 * time.Second),
}
resp, err := client.Get("http://169.254.169.254/latest/meta-data/public-hostname")
if err != nil {
fmt.Println("Probably not on EC2")
fmt.Println(err)
return
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
答案2
得分: 1
你可以使用以下函数来查看是否存在具有特定名称的接口:
package main
import (
"log"
"net"
"strings"
)
func trace(fmt string, args ...interface{}) {
log.Printf(fmt, args...)
}
func HasAddrWithName(name string) (bool, error) {
ifaces, err := net.Interfaces()
if err != nil {
return false, err
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
trace("%s", err)
continue
}
for _, addr := range addrs {
ipaddr, _, err := net.ParseCIDR(addr.String())
if err != nil {
trace("%s", err)
continue
}
hosts, err := net.LookupAddr(ipaddr.String())
if err != nil {
trace("%s", err)
continue
}
for idx, h := range hosts {
trace("%d: %s\n", idx, h)
if strings.Contains(h, name) {
return true, nil
}
}
}
}
return false, nil
}
func main() {
hasAddr, err := HasAddrWithName(".ec2.internal")
if err != nil {
log.Fatal(err)
}
if hasAddr {
log.Println("在 EC2 内部")
return
}
log.Println("不在 EC2 内部")
}
该函数将尝试查找所有接口并将 IP 解析为 DNS 名称。如果名称包含特定字符串,则返回 true。
英文:
You can see if there is an interface with a specific name with this function:
package main
import (
"log"
"net"
"strings"
)
func trace(fmt string, args ...interface{}) {
log.Printf(fmt, args...)
}
func HasAddrWithName(name string) (bool, error) {
ifaces, err := net.Interfaces()
if err != nil {
return false, err
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
trace("%s", err)
continue
}
for _, addr := range addrs {
ipaddr, _, err := net.ParseCIDR(addr.String())
if err != nil {
trace("%s", err)
continue
}
hosts, err := net.LookupAddr(ipaddr.String())
if err != nil {
trace("%s", err)
continue
}
for idx, h := range hosts {
trace("%d: %s\n", idx, h)
if strings.Contains(h, name) {
return true, nil
}
}
}
}
return false, nil
}
func main() {
hasAddr, err := HasAddrWithName(".ec2.internal")
if err != nil {
log.Fatal(err)
}
if hasAddr {
log.Println("inside ec2")
return
}
log.Println("Not inside ec2")
}
The function will try to find all the interface an resolve the ip to a dns name. if the name contains the specific string returns true.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论