Skip to content

Instantly share code, notes, and snippets.

@GShwartz
Created February 11, 2020 19:49
Show Gist options
  • Save GShwartz/86059cb0cb56e3075ecb4ed989affd4e to your computer and use it in GitHub Desktop.
Save GShwartz/86059cb0cb56e3075ecb4ed989affd4e to your computer and use it in GitHub Desktop.
URL2IPonDesktop
################################################################################
# This program takes a URL from the user, Translates it to an IP address and #
# saves the data on the user's Desktop by the user's filename choice. #
# Coded By Gil Shwartz #
################################################################################
import socket
import sys
import os
from termcolor import colored
class url_ip():
def __init__(self, host, filename):
self.host = host
self.filename = filename
# Start with prompt for exit.
def start(self):
while True:
# Get URL
host = input("Enter URL or [0] to Exit: ")
# Validate input starts with www.
if host[0:4] == "www.":
url_ip.get_ip_addr(host)
url_ip.write_to_file(host)
continue
elif host == "0":
print(colored("Bye!", 'grey'))
sys.exit()
else:
print(colored("[-] Invalid URL! [-]", 'red'))
url_ip.start()
# Get the URL's IP.
def get_ip_addr(host):
# Establish Socket connection.
try:
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# End program if error occurs with Error code.
except socket.error as err:
print(colored(f"[-] Could not connect socket. Error: {err} [-]", 'red'))
sys.exit()
# Get the URL's IP
try:
host = socket.gethostbyname(host)
return host
# End program if error occurs with Error code.
except :
print(colored("[-] Could not connect. Check typing. [-]", 'red'))
url_ip.start()
# Write data to file name on user's Desktop.
def write_to_file(host):
# Get the file name.
filename = input("Enter File Name: ")
# Open file for appending on user's Desktop.
with open(fr'c:\Users\{os.getlogin()}\Desktop\{filename}.txt', 'a') as file:
file.write(f"HOST: {host} = = = IP: {url_ip.get_ip_addr(host)}\n")
# End of writing to file.
file.close()
print(colored(f"{host}'s IP address is: {url_ip.get_ip_addr(host)} [!] File {filename}.txt Updated [!]\n", 'green'))
url_ip.start()
url_ip.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment