我正在尝试将“securityonion”函数的stdout打印到该函数生成的窗口。我该怎么做?
我在同一主题上看过其他几篇文章,但无法解决如何实现这一目标。在这种情况下,我不会为OO代码拍摄,如果你尝试我的整个脚本,你会发现事情正在顺利发布。我只需要stdout出现在我在所述函数中创建的窗口中。
对不起,很棒的Tkinter noob。
from Tkinter import *
import Tkinter as tk
from PIL import ImageTk, Image
import os
import time
import sys
from scapy.all import *
def terminal():
window = tk.Toplevel(root)
window.title("Terminal")
termf = Frame(window, height=700, width=700)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
os.system('xterm -into %d -geometry 700x700 -sb &' % wid)
def wazuh():
pass
def securityonion():
window = tk.Toplevel(root)
window.resizable(0,0)
window.title("Security Onion Bomb")
list = open("dests.txt").readlines()
for x in list:
packet = IP(dst=x,src="10.10.10.10")/TCP(dport=999,sport=1234)/"iamhaxor"
send(packet)
print("A PACKET HAS BEEN SENT TO " + x)
time.sleep(2)
def endpoint():
pass
def help():
pass
#setup main window
root = Tk()
img = ImageTk.PhotoImage(Image.open("bluetools.png"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.title("Optometry")
menubar = Menu(root)
root.geometry("500x400")
root.resizable(0,0)
filemenu = Menu(menubar, tearoff=0)
# tools menu
filemenu.add_command(label="Launch Terminal", command=terminal)
filemenu.add_command(label="Wazuh Bomb", command=wazuh)
filemenu.add_command(label="Security Onion Bomb", command=securityonion)
filemenu.add_command(label="Endpoint Bomb", command=endpoint)
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="Tools", menu=filemenu)
# help menu
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About...", command=help)
menubar.add_cascade(label="Help", menu=helpmenu)
root.config(menu=menubar)
root.mainloop()
答案 0 :(得分:0)
我曾做过类似的事情,需要将stdout + stderr重定向到tkinter文本框。它都是面向对象的,但您应该能够提取相关的功能。 Here's正在运行的代码,这是重要的部分:
import tkinter as tk
from tkinter import ttk, DISABLED, NORMAL, END
def __init__(self):
tk.Tk.__init__(self)
self.create_widgets()
# If .buf exists, clear it.
with open('.buf', 'w'):
pass
# Set stdout to output to .buf
# This allows us to display a virtual terminal
# that intercepts print(info) statements from imported classes
sys.stdout = open(".buf", 'a')
# Check and refresh buf -- see function for details
self.read_std_out()
def create_widgets(self):
...
info_box = ttk.Frame(self, borderwidth=1)
self.info_text = tk.Text(info_box, bg="grey")
self.info_text.insert(
END, "This is printed at the beginning of the textbox!\n")
self.info_text.config(state=DISABLED)
self.info_text.grid(column=1, row=1)
...
def read_std_out(self):
"""Put stdout messages into the info_box. Called once, auto-refreshes"""
sys.stdout.flush() # Force write
with open('.buf', 'r') as buf:
read = buf.read()
if read: # Do not write if empty
self.add_info(read)
with open('.buf', 'w'):
pass # Clear file
sys.stdout = open(".buf", 'a')
self.after(1000, self.read_std_out) # Call this again soon
def add_info(self, info):
"""Add informational message to info box. Use instead of print().
arguments: (str) info
effects: add line with info printed to screen in info box"""
self.info_text.config(state=NORMAL)
info = "> " + str(info) + "\n"
self.info_text.insert(END, info)
self.info_text.see(END)
self.info_text.config(state=DISABLED)
我不能说我真的对过去发生的事情了解得太多,但希望我的一些旧代码很有用。
答案 1 :(得分:0)
只需将print("A PACKET HAS BEEN SENT TO " + x)
替换为:
Label(window, text="A PACKET HAS BEEN SENT TO " + x).pack(fill="both", expand=True)