Site icon Virtual Maestro

Changing MAC using python code – GUI interface

Advertisements
 
import sys
import subprocess
import tkinter as tk
from tkinter import tsk


gui = tk.Tk()
gui.title('MAC-Changer')

interface_name = ttk.Label(gui, text="Enter the interface name: ")
interface_name.grid(row=0, column=0, sticky=tk.W)

intface_name = tk.StringVar()
interface_name_entry = ttk.Entry(gui, width = 20, textvariable=intface_name)
interface_name_entry.grid(row=0, column=1)


mac = ttk.Label(gui, text="Enter new MAC address: ")
mac.grid(row=1, column=0, sticky=tk.W)

mac = tk.StringVar()
mac_addr = ttk.Entry(gui, width=20, textvariable=mac)
mac_addr.grid(row=1, column=1)

def changemac():
    iface = intface_name.get()
    nmac = mac.get()
    if not iface:
        sys.exit("Invalid input.")
    elif not nmac:
        sys.exit("Invalid input.")
    else:
        print("Changing the MAC Address as per the input")
        subprocess.call(["ifconfig", iface, "down"])
        subprocess.call(["ifconfig", iface, "hw", "ether", nmac])
        subprocess.call(["ifconfig", iface, "up"])
        sys.exit("MAC Address successfully changed to: ")

mac_button = ttk.Button(gui, text="Change MAC", command=changemac)
mac_button.grid(row=4, column=1)
gui.mainloop()
Exit mobile version