Tuesday, December 4, 2018

golang examples - Hostname and IP address programs code

Golang hostname

The hostname is a label assigned to a machine which is connected in Network of machines. Golang standard package net provides functions related to TCP/IP, UDP and DNS related networking related functionalities

The following are various examples related to the hostname and Ip addresses in Go Language.

Get Hostname of the System environment

It is always good to know the hostname of the environment where the go program is running.package standard inbuilt os provides hostname function which gives hostname of the environment. You can check here  for more information
 here is the syntax of hostname function
func Hostname() (name string, error error)
It returns name - hostname and error return nil if no error exists For example if you are running on a System named 'system1', os.Hostname() function returns 'system1' as an output.
Following is an example of finding hostname of the System
package main

import (
 "fmt"
 "os"
)

func main() {
 hostname, error := os.Hostname()
 if error != nil {
  panic(error)
 }
 fmt.Println("hostname returned from Environment : ", hostname)
 fmt.Println("error : ", error)

}
Output is
hostname returned from Environment :  DESKTOP-47884kl
error :  <nil>

Check a string contains IP address or Hostname or domain name

This program is to check a given string is IP address or hostname. net package provides ParseIP() function for checking IP address with the format of IPV4 (192.134.1.12) and IPV6 (1111:ca7::57) Here is a syntax of ParseIP() function
func ParseIP(s string) IP
if given string is not valid IP address representation, It returns nil. Here is a program for checking hostname or IP address of a given string
package main

import (
 "fmt"
 "net"
)

func checkStringForIpOrHostname(host string) {
 addr := net.ParseIP(host)
 if addr == nil {
  fmt.Println("Given String is a Domain Name")

 } else {
  fmt.Println("Given String is a Ip Address")
 }
}
func main() {
 checkStringForIpOrHostname("google.com")
 checkStringForIpOrHostname("192.168.1.0")

}
Output is
Given String is a Domain Name
Given String is a Ip Address

How to get Domain name from a given URL

Often, we need to check Domain name from the given URL. For example, if Url is like mydomain.com/category/mypage.html, Domain name should return mydomain.com Package URL provides a function parse() checks URL and returns URL instance. Hostname with URL instance return hostname.
Here is parse function syntax
func Parse(rawurl string) (*URL, error) 
It takes rawurl as input, returns URL instance an error object
package main

import (
 "fmt"
 "net/url"
)

func main() {
 urlInstance, error := url.Parse("https://mydomain.com/category/mypage.html")
 if error != nil {
  fmt.Println(error)
 }
 fmt.Println(urlInstance.Hostname())
}
Output is

mydomain.com

How to get Fully Qualified Domain Name - FQDN of a local machine 

There is no direct library to get FQDN of a machine. There is a custom wrapper library https://github.com/Showmax/go-fqdn based on OS and net package First install this library using below command go get github.com/Showmax/go-fqdn Following program returns fully qualified domain name if fqdn exists, else return unknown
package main

import (
 "fmt"

 fqdn "github.com/Showmax/go-fqdn"
)

func main() {
 fmt.Println(fqdn.Get())
}
Output is
DESKTOP-968A6TP

How to get the local IP address of a system 

the net package provides Dial function which connects to the target address of named network. UDP is a protocol which doesn't require a physical connection like TCP and target will not connect it
package main

import (
 "fmt"
 "net"
)

func main() {
 conn, error := net.Dial("udp", "8.8.8.8:80")
 if error != nil {
  fmt.Println(error)

 }

 defer conn.Close()
 ipAddress := conn.LocalAddr().(*net.UDPAddr)
 fmt.Println(ipAddress)
}
Output is
192.168.1.102:49703

Related article


EmoticonEmoticon