Advertisement
linux

ip2hostname.go

Jul 16th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.68 KB | None | 0 0
  1. /*
  2.     >> Sinden's HTTP IP to Hostname Filter
  3.     Credits to Google and the mind of Sinden.
  4.     Info: Reads a list of ips and resolves them and
  5.     checks the status to see if there online and outputs the online results.
  6. */
  7.  
  8. package main
  9.  
  10. import (
  11.     "bufio"
  12.     "fmt"
  13.     "log"
  14.     "net"
  15.     "net/http"
  16.     "os"
  17.     "strings"
  18.     "time"
  19. )
  20.  
  21. func dnshure_online(host string) bool {
  22.     timeout := time.Duration(3 * time.Second)
  23.     client := http.Client{
  24.         Timeout: timeout,
  25.     }
  26.  
  27.     rs, err := client.Get("http://" + host)
  28.     if err != nil {
  29.         return false
  30.     }
  31.  
  32.     defer rs.Body.Close()
  33.  
  34.     return rs.StatusCode == 200
  35. }
  36.  
  37. func dnshure_handle_host(current_host string, file *os.File) {
  38.     rev_dns, err := net.LookupAddr(current_host)
  39.     if err != nil {
  40.         return
  41.     }
  42.  
  43.     if !dnshure_online(rev_dns[0]) {
  44.         fmt.Printf("[OFFLINE] -> %s | %s\r\n", current_host, rev_dns[0])
  45.         return
  46.     }
  47.  
  48.     fmt.Printf("[ONLINE] -> %s | %s\r\n", current_host, rev_dns[0])
  49.  
  50.     if len(rev_dns) > 0 && rev_dns[0] != current_host {
  51.         hostname := strings.TrimSuffix(rev_dns[0], ".")
  52.         fmt.Fprintf(file, hostname+"\r\n")
  53.     } else {
  54.         fmt.Fprintf(file, current_host+"\r\n")
  55.     }
  56. }
  57.  
  58. func main() {
  59.     if len(os.Args) != 3 {
  60.         log.Fatal("Arguments: <ip_list> <out>")
  61.     }
  62.  
  63.     in_file, err := os.Open(os.Args[1])
  64.     if err != nil {
  65.         log.Fatal(err)
  66.     }
  67.     defer in_file.Close()
  68.  
  69.     file, err := os.Create(os.Args[2])
  70.     if err != nil {
  71.         log.Fatal(err)
  72.     }
  73.     defer file.Close()
  74.  
  75.     scanner := bufio.NewScanner(in_file)
  76.     for scanner.Scan() {
  77.         current_host := scanner.Text()
  78.  
  79.         go dnshure_handle_host(current_host, file)
  80.     }
  81.  
  82.     input, _ := bufio.NewReader(os.Stdin).ReadString('\n')
  83.     fmt.Printf("You just entered this..%s - terminating!\n", input)
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement