import socket, os
list_dom = ['google.com', 'yahoo.com', 'bing.com']
file = open('host-to-ip.txt', 'a')
for x in list_dom:
#print('IP of domain: ' + x + socket.gethostbyname(x))
file.write('IP of domain: ' + x + ' -> ' + socket.gethostbyname(x) + '\n')
file.close()
- Import the necessary modules
socket
andos
. - Create a list of domains to resolve their IP addresses and store it in the variable
list_dom
. - Open a new file
host-to-ip.txt
inappend
mode and assign it to the variablefile
. - Iterate over each domain in the
list_dom
using a for loop: a. Usesocket.gethostbyname(x)
to resolve the IP address of the current domainx
. b. Write the domain name and its IP address to thehost-to-ip.txt
file. - Close the file.
Explanation of the code: The purpose of this code is to resolve the IP addresses of a list of domains and store them in a file.
The first line of code imports the necessary modules socket
and os
.
In the next line, a list of domains to resolve their IP addresses is created and stored in the variable list_dom
.
The next line opens a new file host-to-ip.txt
in append
mode and assigns it to the variable file
.
The for loop iterates over each domain in the list_dom
using the variable x
. Inside the for loop, socket.gethostbyname(x)
is used to resolve the IP address of the current domain x
. The resolved IP address is then written to the file host-to-ip.txt
using the write()
method of the file object. The string written to the file includes the domain name and its IP address separated by '->'.
Finally, the file is closed using the close()
method of the file object.
No comments:
Post a Comment