How to get all ips associated with a Python domain?

Asked

Viewed 338 times

0

I need to create an application that gets all the ips associated with a domain. I tried using the following Python code:

import socket

print(socket.gethostbyname('facebook.com.br'))

However, just return me an IP. Would have some way to return the facebook range, for example?

2 answers

0

Good afternoon Gurion, I think the code below may help you:

import socket

lista_de_ip = []

site = socket.getaddrinfo("www.facebook.com.br" ,0)

for i in site:
  lista_de_ip.append(i[-1][0])
lista_de_ip = list(set(lista_de_ip))

print(lista_de_ip)

inserir a descrição da imagem aqui

  • Have you tested this code? Does it really work? For me here it just generated a list with an IP and a MAC address...

  • Yes Luiz, I tried it and it really works! Did you make an error? For me it returns an IPV4 from facebook and another is an IPV6 (not MAC address) I put the output from my terminal, in the answer above. In my case presents these due to facebook caches in my city, in yours may be different.

  • You’re right, it’s an IPV6 and not a MAC address. Sorry for the misunderstanding.

  • right! And you need the IP’s in another format?

0


If you prefer an option that does not use Python, you can use Tracert on Windows, or Traceroute on Linux.

Code output in Tracert

inserir a descrição da imagem aqui

So you could use Python only to run the external command with the help of the library subprocess, instead of trying to use the resources of the language itself.

The code below executes the command and stores it in a file . txt

# coding: UTF-8


import os
import subprocess


# Se for Windows, apenas troque o traceroute por tracert
command = ['traceroute', 'www.facebook.com']

with open('log.txt', 'w') as arq:
    output_command = subprocess.call(command, stdout=arq)

print('[+] Finished')
  • It’s a good tip. But, how about producing an example code that does this? It would greatly improve your response (and earn my +1)

  • You’re right, I edited my answer. Thank you for the suggestion, because it made me think differently than I was thinking...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.