tsounakis

netmanager

Sep 9th, 2020
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.28 KB | None | 0 0
  1. #version 0.2.1 ALPHA
  2. #under MIT Licence
  3.  
  4. import socket as sc
  5. import tkinter as tk
  6. import tkinter.scrolledtext as scr
  7. import pyperclip
  8. import subprocess
  9. import datetime
  10. from os.path import dirname, join
  11. from time import gmtime, strftime
  12.  
  13. global hostname, IP_address
  14.  
  15. current_dir = dirname(__file__)
  16. image_path = join(current_dir, "net.png")
  17.  
  18.  
  19. class main_win():
  20.     def __init__(self, master):
  21.         self.tm = datetime.datetime.now()
  22.  
  23.         self.master = master
  24.         self.master.option_add("*Font", 'arial 10')
  25.         self.master.title('Net Manager 0.2 Alpha')
  26.         self.master = master
  27.         self.master.configure(background = 'white')
  28.         self.master.resizable(False, False)
  29.         self.masterback = tk.Frame(height = 300, width = 500)
  30.         self.masterback.pack()
  31.         self.masterback.configure(background = 'white')
  32.          
  33.         self.Message = tk.Label(master, text='Welcome to Net Manager 0.2 Alpha Build - Exclusive Alpha Access.')
  34.         self.Message.place(relx= 0.5, rely = 0.105, anchor = 'center')
  35.         self.Message.configure(background = 'white')
  36.  
  37.         self.show_ip = tk.Button(master, text = 'Show host IP address', command = self.print_ip)
  38.         self.show_ip.place(relx= 0.62, rely = 0.575)
  39.         self.show_ip.configure(background = 'white')
  40.                        
  41.         self.ip_entry = tk.Entry(master)
  42.         self.ip_entry.place(relx = 0.1, rely = 0.6)
  43.         self.ip_entry.configure(background = 'white')
  44.        
  45.         self.show_hostname = tk.Button(master, text = 'Show hostname', command = self.print_hn)
  46.         self.show_hostname.place(relx= 0.62, rely = 0.475)
  47.         self.show_hostname.configure(background = 'white')
  48.        
  49.         self.show_wifi_password = tk.Button(master, text = 'Show Saved N and P/W', command = self.find_wifi_password)
  50.         self.show_wifi_password.place(relx= 0.62, rely = 0.375)
  51.         self.show_wifi_password.configure(background = 'white')
  52.                        
  53.         self.hostname_entry = tk.Entry(master)
  54.         self.hostname_entry.place(relx = 0.1, rely = 0.5)
  55.         self.hostname_entry.configure(background = 'white')
  56.        
  57.         img = tk.PhotoImage(file = image_path)
  58.         self.master.label= tk.Label(self.master, image = img)
  59.         self.master.label.image = img
  60.         self.master.label.place(relx = 0.08, rely= 0.175, anchor="s")
  61.         self.master.label.configure(background = 'white')
  62.  
  63.         self.log_label = tk.Label(self.master, bg = 'white', text = 'Action log:')
  64.         self.log_label.place(rely = 0.69, relx = 0.02)
  65.         self.log_frame = tk.Frame(master, bg = 'white')
  66.         self.log_frame.pack(fill = 'both', expand = 'yes')
  67.         self.log = scr.ScrolledText(
  68.             master = self.log_frame,
  69.             wrap = 'word',
  70.             width = 75,
  71.             height = 5
  72.         )
  73.         self.log.pack(padx = 10, pady = 10, fill = 'both', expand = 'True')
  74.         self.log.insert('insert', 'Your network: ' + self.find_nw_name() + '\n')
  75.  
  76.     def print_hn(self):
  77.         self.hostname_entry.delete(0, 'end')
  78.         self.hostname_entry.insert(0, hostname)
  79.        
  80.     def print_ip(self):
  81.         self.ip_entry.delete(0, 'end')
  82.         self.ip_entry.insert(0, IP_address)
  83.        
  84.         self.copy_ip()
  85.         self.print_to_log()
  86.  
  87.     def find_nw_name(self):
  88.         self.nw_str = (str(subprocess.check_output('netsh wlan show interfaces')))
  89.         self.nw_lst = self.nw_str.split()
  90.         idx = self.nw_lst.index('SSID')
  91.         idx += 2
  92.         return self.nw_lst[idx][:-4]
  93.    
  94.     def copy_ip(self):
  95.         pyperclip.copy(self.ip_entry.get())
  96.         spam = pyperclip.paste()
  97.  
  98.  
  99.     def print_to_log(self):
  100.         self.log.insert('insert', str(str(self.tm.day) +'/' + str(self.tm.month) + '/' + str(self.tm.year) + ' , ' + str(self.tm.hour) +':' + str(self.tm.minute) + ':' + str(self.tm.second) + ' // Shown ' + hostname +'\'s IP address and copied it to clipboard.' + '\n'))
  101.  
  102.     def find_wifi_password(self):
  103.         data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8', errors="backslashreplace").split('\n')
  104.         profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
  105.         self.log.insert('insert', str(self.tm.day) +'/' + str(self.tm.month) + '/' + str(self.tm.year) + ' , ' + str(self.tm.hour) +':' + str(self.tm.minute) + ':' + str(self.tm.second) + '// Your PC has these saved - recently used networks and their passwords:\n\nNAME OF NETWORK       PASSWORD\n\n')
  106.         for i in profiles:
  107.             try:
  108.                 results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8', errors="backslashreplace").split('\n')
  109.                 results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
  110.                 try:
  111.                     self.log.insert('insert', str(i) + ' : ' + str(results[0]) + '\n')
  112.                 except IndexError:
  113.                     self.log.insert('insert', str(i) + ": OpenNetwork\n")
  114.             except subprocess.CalledProcessError:
  115.                 self.log.insert('insert', (i, "ENCODING ERROR"))
  116.         self.log.insert('insert', '\n')
  117.        
  118.  
  119. hostname = sc.gethostname()
  120. IP_address = sc.gethostbyname(hostname)
  121.  
  122. root = tk.Tk()
  123.  
  124. window = main_win(root)
  125. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment