Created
December 12, 2014 10:50
-
-
Save wtarr/657e7b1c8e9722cd6ca3 to your computer and use it in GitHub Desktop.
Quick and dirty eth0 interface toggling GUI for testing purposes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Tkinter import * | |
from subprocess import check_output | |
import ttk, os, sys | |
def disableConnection(): | |
global currentstatus | |
try: | |
os.popen('sudo ifdown eth0', 'r', 1) | |
print 'Connection disabled' | |
currentstatus.set('down') | |
except ValueError: | |
pass | |
def enableConnection(): | |
global w | |
try: | |
os.popen('sudo ifup eth0', 'r', 1) | |
print 'Connection enabled' | |
currentstatus.set('up') | |
except ValueError: | |
pass | |
def toggle(*args): | |
try: | |
status = getInterfaceStatus() | |
if 'up' in status: | |
print 'Up so disabling' | |
disableConnection() | |
else: | |
print 'Up so enabling' | |
enableConnection() | |
except ValueError: | |
pass | |
def getInterfaceStatus(): | |
f = open("/sys/class/net/eth0/operstate", "r") | |
status = f.read() | |
f.close() | |
return status | |
# main | |
master = Tk() | |
master.wm_title("Eth0 Interface Mgmt") | |
f = Frame(master, height=100, width=250, padx=10, pady=10) | |
f.pack_propagate(0) # don't shrink | |
f.pack() | |
b = Button(f, text="Toggle interface", padx=10, pady=10, command=toggle) | |
b.pack() | |
currentstatus = StringVar() | |
currentstatus.set(getInterfaceStatus()) | |
w = Label(f, textvariable=currentstatus) | |
w.pack() | |
mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment