Go to check EC2 environment? Or DNS domain name?

user2671513

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

fabrizioM

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Applying Domain Name to EC2 Instance

From Dev

Error in retrieving public dns name of ec2 instance

From Dev

How to access a site on AWS EC2 without a domain name

From Dev

Linking Amazon Route 53 Domain Name to EC2 instance

From Dev

Amazon EC2 link to Bigrock domain name

From Dev

Apache james email server on EC2 (domain name mapping)

From Dev

How to point domain name to Amazon EC2 subdirectory

From Dev

Amazon EC2 link to Bigrock domain name

From Dev

Domain name not showing up in DNS

From Dev

Duplicate domain name in DNS query

From Dev

Domain name not showing up in DNS

From Dev

Changing dns domain name in Kali

From Dev

Route53 DNS name from private VPC EC2 DNS name

From Dev

TclError: no display name and no $DISPLAY environment variable on EC2

From Dev

Check the validity of a domain name

From Dev

Difference between domain Forest name and DNS name?

From Dev

Update DNS for domain name in AJAX request

From Dev

Azure custom domain name with NetworkSolutions DNS

From Dev

reverse DNS lookup returns different Domain name

From Dev

Azure custom domain name with NetworkSolutions DNS

From Dev

Can a DNS domain name point to a folder on a server?

From Dev

What is the process of adding a domain name to the DNS system?

From Dev

How the whole domain name and DNS works?

From Dev

Local domain name w/o DNS

From Dev

How to figure out the 'Public DNS Name' from within an Amazon EC2 instance?

From Dev

Authentication failed. Please check your username/password. [Server response: DNS Error: Domain name not found code(0) ]

From Dev

Authentication failed. Please check your username/password. [Server response: DNS Error: Domain name not found code(0) ]

From Dev

Apache server config redirect from IP to domain name EC2

From Dev

Node server on EC2 only works through IP address and not domain name

Related Related

  1. 1

    Applying Domain Name to EC2 Instance

  2. 2

    Error in retrieving public dns name of ec2 instance

  3. 3

    How to access a site on AWS EC2 without a domain name

  4. 4

    Linking Amazon Route 53 Domain Name to EC2 instance

  5. 5

    Amazon EC2 link to Bigrock domain name

  6. 6

    Apache james email server on EC2 (domain name mapping)

  7. 7

    How to point domain name to Amazon EC2 subdirectory

  8. 8

    Amazon EC2 link to Bigrock domain name

  9. 9

    Domain name not showing up in DNS

  10. 10

    Duplicate domain name in DNS query

  11. 11

    Domain name not showing up in DNS

  12. 12

    Changing dns domain name in Kali

  13. 13

    Route53 DNS name from private VPC EC2 DNS name

  14. 14

    TclError: no display name and no $DISPLAY environment variable on EC2

  15. 15

    Check the validity of a domain name

  16. 16

    Difference between domain Forest name and DNS name?

  17. 17

    Update DNS for domain name in AJAX request

  18. 18

    Azure custom domain name with NetworkSolutions DNS

  19. 19

    reverse DNS lookup returns different Domain name

  20. 20

    Azure custom domain name with NetworkSolutions DNS

  21. 21

    Can a DNS domain name point to a folder on a server?

  22. 22

    What is the process of adding a domain name to the DNS system?

  23. 23

    How the whole domain name and DNS works?

  24. 24

    Local domain name w/o DNS

  25. 25

    How to figure out the 'Public DNS Name' from within an Amazon EC2 instance?

  26. 26

    Authentication failed. Please check your username/password. [Server response: DNS Error: Domain name not found code(0) ]

  27. 27

    Authentication failed. Please check your username/password. [Server response: DNS Error: Domain name not found code(0) ]

  28. 28

    Apache server config redirect from IP to domain name EC2

  29. 29

    Node server on EC2 only works through IP address and not domain name

HotTag

Archive