Home > other >  Trying to get Tkinter entry results into another python
Trying to get Tkinter entry results into another python

Time:06-27

I have been having trouble getting the input items from my Tkinter GUI into the main program and need help solving the issue. The two codes are as follows below. First the GUI code so far.

# GUI Code

import os
import platform
import tkinter as tk
import threading


def run():
    if platform.system() == "Windows":
        os.system('cmd /c PortScanFinal.py')
    if platform.system() == "Linux":
        os.system("python3 ./PortScanFinal.py")


def open_firewall():
    if platform.system() == 'Windows':
        os.system("cmd /c control")
    if platform.system() == 'Linux':
        os.system("sudo iptables -L -v -n | more")


root = tk.Tk()
root.title("Simple Port Scanner")
root.geometry("450x300")

val1 = tk.StringVar()
val2 = tk.StringVar()

lbl = tk.Label(root, text="Enter the IP address/web address: ")
txt = tk.Entry(root, textvariable=val1, width=15)
txt.focus()
lbl2 = tk.Label(root, text="Please enter file path for Port Scan results: ")
txt2 = tk.Entry(root, textvariable=val2, width=20)
txt2.focus()
txt2_run = tk.Button(root, text="Run Port Scan", command=threading.Thread(target=run).start)
btn1 = tk.Button(root, text="Start Firewall Editing", command=open_firewall)
btn2 = tk.Button(root, text="Quit", command=root.quit)


lbl.pack()
txt.pack()
lbl2.pack()
txt2.pack()
txt2_run.pack()
btn1.pack()
btn2.pack()

if __name__ == "__main__":
    root.mainloop()

next the main python code

#Port Scanner Code

import sys
import time
from socket import *
import PortScannerGUI as Gui

startTime = time.time()

portlist = [7, 19, 20, 21, 22, 23, 25, 37, 53, 69, 79, 80, 110, 111, 135, 137, 138, 139, 161, 443, 445, 512, 513, 514,
            1433, 1434, 1723, 3389, 8080]
NecessaryPorts = [20, 21, 25, 53, 80, 123, 135, 139, 143, 161, 443, 445]
OpenPorts = []


# target = input("Enter a remote host address:")

try:
    ipaddr = Gui.txt.get()
    target = ipaddr  # input("Enter remote IP/Web address here: ")
    IPget = gethostbyname(target)
    print('Starting scan on host:', IPget)
    for i in portlist:
        s = socket(AF_INET, SOCK_STREAM)
        conn = s.connect_ex((IPget, i))
        if conn == 0:
            OpenPorts.append(i)
            print('Port %d: OPEN' % (i,))
            s.close()
        else:
            # ClosedPorts.append(i)
            print('Port %d: CLOSED' % (i,))
            s.close()
    print('Time tasken:', time.time() - startTime)
    print('Results being printed to file within program execution folder')

    # Sets output path for results of scan, Please replace file path when using for own purposes.
    out_path = Gui.txt2.get() 
    sys.stdout = open(out_path, 'w ')  # Opens file in path with write capabilities
    print("Please note that the following ports are generally necessary for computer function, apply firewall rules \n"
          "that do not fully restrict the access to these ports:", NecessaryPorts)
    print("Ports that are open:", OpenPorts)
    print("Use the Open Command Center button on the Port Scanner GUI to begin navigation to the Windows Firewall Settings")
    sys.stdout.close()
except gaierror as e:
    print("Invalid IP address or web address used, please enter a proper address. \nEx: 127.0.0.1 or Google.com \nPlease try again.")

Can someone please help me figure out what is going wrong? When I test the GUI and main code, it will only capture the blank entry lines before the user input is placed.

*Edit------ When removing the get function from the GUI and calling it in the Port Scanner, I still get the following results.

Starting scan on host: 0.0.0.0
Port 7: CLOSED
Port 19: CLOSED
Port 20: CLOSED
Port 21: CLOSED
Port 22: CLOSED
Port 23: CLOSED
Port 25: CLOSED
Port 37: CLOSED
Port 53: CLOSED
Port 69: CLOSED
Port 79: CLOSED
Port 80: CLOSED
Port 110: CLOSED
Port 111: CLOSED
Port 135: CLOSED
Port 137: CLOSED
Port 138: CLOSED
Port 139: CLOSED
Port 161: CLOSED
Port 443: CLOSED
Port 445: CLOSED
Port 512: CLOSED
Port 513: CLOSED
Port 514: CLOSED
Port 1433: CLOSED
Port 1434: CLOSED
Port 1723: CLOSED
Port 3389: CLOSED
Port 8080: CLOSED
Time tasken: 0.006447315216064453
Results being printed to file within program execution folder

CodePudding user response:

See comments and code changes from the original code.

PortScannerGUI.py

# File: PortScannerGUI.py
# GUI Code

import os
import platform
import tkinter as tk
import threading
from PortScanFinal import port_scan  # Import the function version of


# previous code.


def run(address, file_out):  # Add address parameter to this function.
    if platform.system() == "Windows":
        port_scan(address, file_out)
    if platform.system() == "Linux":
        os.system("python3 ./PortScanFinal.py")


def open_firewall():
    if platform.system() == 'Windows':
        os.system("cmd /c control")
    if platform.system() == 'Linux':
        os.system("sudo iptables -L -v -n | more")


root = tk.Tk()
root.title("Simple Port Scanner")
root.geometry("450x300")

val1 = tk.Variable()
val2 = tk.Variable()

lbl = tk.Label(root, text="Enter the IP address/web address: ")
txt = tk.Entry(root, textvariable=val1, width=15)
txt.focus()
lbl2 = tk.Label(root, text="Please enter file path for Port Scan results: ")
txt2 = tk.Entry(root, textvariable=val2, width=20)
txt2.focus()
# Use a lambda function to call the run function with a parameter.
# txt.get()
txt2_run = tk.Button(root, text="Run Port Scan",
                     command=lambda: threading.Thread(target=run(txt.get(

                     ), txt2.get())).start)
btn1 = tk.Button(root, text="Start Firewall Editing", command=open_firewall)
btn2 = tk.Button(root, text="Quit", command=root.quit)

ipaddr = txt.get()
path = txt2.get()

lbl.pack()
txt.pack()
lbl2.pack()
txt2.pack()
txt2_run.pack()
btn1.pack()
btn2.pack()

if __name__ == "__main__":
    root.mainloop()

PortScanFinal.py

# File: PortScanFinal.py
# Import this file into PortScannerGUI
# Port Scanner Code

import sys
import time
from socket import *

# import PortScannerGUI as Gui # Remove import here.

startTime = time.time()

portlist = [7, 19, 20, 21, 22, 23, 25, 37, 53, 69, 79, 80, 110, 111, 135, 137,
            138, 139, 161, 443, 445, 512, 513, 514,
            1433, 1434, 1723, 3389, 8080]
NecessaryPorts = [20, 21, 25, 53, 80, 123, 135, 139, 143, 161, 443, 445]
OpenPorts = []


# target = input("Enter a remote host address:")
# Make the following code a function.
def port_scan(target, out_path):
    try:
        # target = Gui.txt.get()  # input("Enter remote IP/Web address here: ")
        IPget = gethostbyname(target)
        print('Starting scan on host:', IPget)
        for i in portlist:
            s = socket(AF_INET, SOCK_STREAM)
            conn = s.connect_ex((IPget, i))
            if conn == 0:
                OpenPorts.append(i)
                print('Port %d: OPEN' % (i,))
                s.close()
            else:
                # ClosedPorts.append(i)
                print('Port %d: CLOSED' % (i,))
                s.close()
        print('Time tasken:', time.time() - startTime)
        print('Results being printed to file within program execution folder')

        # Sets output path for results of scan, Please replace file path when using for own purposes.
        # out_path = Gui.txt2.get()
        sys.stdout = open(out_path, 'w ')  # Opens file in path with write
        # capabilities
        print(
            "Please note that the following ports are generally necessary for computer function, apply firewall rules \n"
            "that do not fully restrict the access to these ports:",
            NecessaryPorts)
        print("Ports that are open:", OpenPorts)
        print(
            "Use the Open Command Center button on the Port Scanner GUI to begin navigation to the Windows Firewall Settings")
        sys.stdout.close()
    except gaierror as e:
        print(
            "Invalid IP address or web address used, please enter a proper address. \nEx: 127.0.0.1 or Google.com \nPlease try again.")
  • Related