gui-host-lookup/tkinter-enter.py

106 lines
3.6 KiB
Python
Raw Normal View History

# From: https://www.geeksforgeeks.org/how-to-bind-the-enter-key-to-a-t//kinter-window/
# importing tkinter
import tkinter as tk
2024-08-17 12:00:20 +02:00
from generic import (domain_lookup, ip_lookup, sanitize, spf_dmarc,
type_hosts_listed, type_hosts_organized)
2024-08-15 21:22:21 +02:00
# function to run on pressing
# 'ENTER' key from keyboard.
def key_handler_function(event):
inputs = entry.get()
print(type(inputs))
sanitized = sanitize.strip_and_list(inputs)
typed = type_hosts_listed.type_hosts(sanitized)
2024-08-15 21:22:21 +02:00
textbox.insert("end", typed)
listed = type_hosts_organized.type_hosts(sanitized)
# IPs = listed['IPs']
# below works for updating a global variable (here)...
2024-08-15 21:22:21 +02:00
var.set(listed["IPs"])
ip_listbox.delete(0, "end")
domain_listbox.delete(0, "end")
url_listbox.delete(0, "end")
email_listbox.delete(0, "end")
2024-08-15 21:22:21 +02:00
for IP in listed["IPs"]:
hostname = ip_lookup.get_hostname(IP)
output = "{} => {}".format(IP, hostname[0])
2024-08-15 21:22:21 +02:00
ip_listbox.insert("end", output)
for domain in listed["domains"]:
ip = domain_lookup.get_ip_address(domain)
output = "{} => {}".format(domain, ip)
2024-08-15 21:22:21 +02:00
domain_listbox.insert("end", output)
for URL in listed["URLs"]:
url_listbox.insert("end", URL)
for email in listed["emails"]:
dmarc, spf = spf_dmarc.lookup(email)
output_dmarc = "{} => {}".format(email, dmarc)
output_spf = "{} => {}".format(email, spf)
2024-08-15 21:22:21 +02:00
email_listbox.insert("end", output_dmarc, output_spf)
def test_button_function():
# testing quoting and comma separating hosts, IPs for now, from global StringVar var
2024-08-15 21:22:21 +02:00
textbox.delete(1.0, "end")
IPs = ", ".join([f'"{host}"' for host in var.get()])
textbox.insert("end", IPs)
print("var is : {}".format(var.get()))
# ... and printing an updated global variable here!
2024-08-15 21:22:21 +02:00
# main application window
root = tk.Tk()
root.title("Enter IP addresses, URLs, domains, email addresses")
root.geometry("900x600")
2024-08-15 21:22:21 +02:00
root.resizable(0, 0)
# configure the grid
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.columnconfigure(2, weight=1)
root.columnconfigure(3, weight=1)
2024-08-15 21:22:21 +02:00
# Listing stuff
# creating widgets
entry = tk.Entry(root, text="Put in IP addresses, URLs, domains, email addresses")
textbox = tk.Text(root, height=8)
# textbox.insert('1.0', test)
entrylabel = tk.Label(root, text="Copy-paste or type and Enter :D")
ip_label = tk.Label(root, text="IP and hostname")
ip_listbox = tk.Listbox(root)
domain_label = tk.Label(root, text="domain => IP")
domain_listbox = tk.Listbox(root)
url_label = tk.Label(root, text="URL => IP")
url_listbox = tk.Listbox(root)
2024-08-15 21:22:21 +02:00
email_label = tk.Label(root, text="email address => DMARC/SPF")
email_listbox = tk.Listbox(root)
2024-08-15 21:22:21 +02:00
var = tk.Variable()
entrylabel.grid(column=1, row=0, columnspan=2, sticky="nsew")
entry.grid(column=0, row=1, columnspan=4, sticky="nsew")
ip_label.grid(column=0, row=2, columnspan=2, sticky="nsew")
ip_listbox.grid(column=0, row=3, columnspan=2, sticky="nsew")
domain_label.grid(column=2, row=2, columnspan=2, sticky="nsew")
domain_listbox.grid(column=2, row=3, columnspan=2, sticky="nsew")
url_label.grid(column=0, row=4, columnspan=2, sticky="nsew")
url_listbox.grid(column=0, row=5, columnspan=2, sticky="nsew")
email_label.grid(column=2, row=4, columnspan=2, sticky="nsew")
email_listbox.grid(column=2, row=5, columnspan=2, sticky="nsew")
# label.grid(column=1, row=2)
# textbox.grid(column=1, row=3)
2024-08-15 21:22:21 +02:00
# Trying out buttons
test_button = tk.Button(
root, text="comma-separate and add quotes", command=test_button_function
)
# Binding ENTER key to function
entry.bind("<Return>", key_handler_function)
# run the application
root.mainloop()