Skip to content

Instantly share code, notes, and snippets.

@random-robbie
Created January 6, 2023 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save random-robbie/c388d3acd78018195639e24301e6eead to your computer and use it in GitHub Desktop.
Save random-robbie/c388d3acd78018195639e24301e6eead to your computer and use it in GitHub Desktop.
Checking for dead mx hosts
package main
import (
"fmt"
"net"
"time"
)
func main() {
// Set the domain to check
domain := "example.com"
// Look up the MX records for the domain
mxRecords, err := net.LookupMX(domain)
if err != nil {
fmt.Println(err)
return
}
// Check each MX record to see if the host is dead
for _, mx := range mxRecords {
// Get the host associated with the MX record
mxHost := mx.Host
fmt.Printf("Checking MX record for host: %s\n", mxHost)
// Check if the host is dead
dead, err := checkDead(mxHost)
if err != nil {
fmt.Println(err)
continue
}
if dead {
fmt.Println("Host is dead")
} else {
fmt.Println("Host is alive")
}
}
}
// checkDead checks if a host is dead
func checkDead(host string) (bool, error) {
// Set a timeout for the check
timeout := time.Second * 5
// Try to establish a connection to the host
conn, err := net.DialTimeout("tcp", host+":25", timeout)
if err != nil {
// If the connection fails, return true (dead)
return true, nil
}
// If the connection succeeds, close the connection and return false (alive)
conn.Close()
return false, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment