Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nownot

macrumors regular
Original poster
Feb 25, 2007
245
1
tx
i wanted to learn pyton so i made this. it is a gui to edit/add/delete network routes. if anyone has any criticism or question please don't hesitate to post. thanks.

save, open up terminal, change directory to where routes.py is, and run (probably have to use sudo, i tested with sudo) python sudo routes.py

on the irc channels people gave me a hard time with sudo but pls scan the code it does nothing malicious and works perfect on my machine.

http://www.megaupload.com/?d=A1I7JN18
 

nownot

macrumors regular
Original poster
Feb 25, 2007
245
1
tx
here is the code for people to inspect.

#! /usr/bin/env python
from Tkinter import *
import tkSimpleDialog
import tkMessageBox
import os

class NewItemDialog(tkSimpleDialog.Dialog):

def __init__(self, parent, guiFrame, title=None, dest="", gw="", flags="", refs="", use="", netif="", exp=""):
"""Init, override default for default params"""
self.org_dest = dest
self.dest = dest
self.gw = gw
self.flags = flags
self.refs = refs
self.use = use
self.netif = netif
self.exp = exp
self.Result = False #Default = Cancel
tkSimpleDialog.Dialog.__init__(self, parent,title)

def body(self, master):
"""This is where you create the Body"""

lbText = Label(master, text="Destination ( be sure its all 4 sets x.x.x.x)")
lbText.grid(row=0, padx=5,sticky=W)
lbText = Label(master, text="GateWay")
lbText.grid(row=1, padx=5,sticky=W)
lbText = Label(master, text="Flags")
lbText.grid(row=2, padx=5,sticky=W)
lbText = Label(master, text="Refs")
lbText.grid(row=3, padx=5,sticky=W)
lbText = Label(master, text="Use")
lbText.grid(row=4, padx=5,sticky=W)
lbText = Label(master, text="NetIf")
lbText.grid(row=5, padx=5,sticky=W)
lbText = Label(master, text="Expire")
lbText.grid(row=6, padx=5,sticky=W)

self.n_dest = Entry(master)
self.n_dest.insert(0,self.dest)
self.n_gw = Entry(master)
self.n_gw.insert(0,self.gw)
self.n_flags = Entry(master)
self.n_flags.insert(0,self.flags)
self.n_refs = Entry(master)
self.n_refs.insert(0,self.refs)
self.n_use = Entry(master)
self.n_use.insert(0,self.use)
self.n_netif = Entry(master)
self.n_netif.insert(0,self.netif)
self.n_exp = Entry(master)
self.n_exp.insert(0,self.exp)

self.n_dest.grid(row=0, column=1)
self.n_gw.grid(row=1, column=1)
self.n_flags.grid(row=2, column=1)
self.n_refs.grid(row=3, column=1)
self.n_use.grid(row=4, column=1)
self.n_netif.grid(row=5, column=1)
self.n_exp.grid(row=6, column=1)

"""Return the widget that will get focuds"""
# return self.dest

def apply(self):
"""They have selected OK"""
self.nn_org_dest = (self.org_dest)
self.nn_dest = (self.n_dest.get())
self.nn_gw = (self.n_gw.get())
self.nn_flags = (self.n_flags.get())
self.nn_refs = (self.n_refs.get())
self.nn_use = (self.n_use.get())
self.nn_netif = (self.n_netif.get())
self.nn_exp = (self.n_exp.get())

if self.nn_org_dest == self.nn_dest:
num = self.nn_dest.count('.')

if num == 0:
self.nn_dest = self.nn_dest + ".0.0.0"
elif num == 1:
self.nn_dest = self.nn_dest + ".0.0"
elif num == 2:
self.nn_dest = self.nn_dest + ".0"

if self.nn_org_dest != "":

num = self.nn_org_dest.count('.')

if num == 0:
self.nn_org_dest = self.nn_org_dest + ".0.0.0"
elif num == 1:
self.nn_org_dest = self.nn_org_dest + ".0.0"
elif num == 2:
self.nn_org_dest = self.nn_org_dest + ".0"

cmd = 'sudo route delete ' + self.nn_org_dest + '/24'
os.popen(cmd)
cmd = 'sudo route add -net ' + self.nn_dest +'/24 '+ self.nn_gw
os.popen(cmd)
guiFrame.Show()

class GUIFramework(Frame):
"""This is the GUI"""

def __init__(self,master=None):
"""Initialize yourself"""

"""Initialise the base class"""
Frame.__init__(self,master)

"""Set the Window Title"""
self.master.title("advanced settings")
top=self.winfo_toplevel()
"""Display the main window"
with a little bit of padding"""
self.grid(padx=1, pady=1,sticky=N+S+E+W)
self.InitResizing()
self.CreateWidgets()


def InitResizing(self):
"""Initialize the Resizing of the Window"""
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
self.columnconfigure(6, weight=1)

def CreateWidgets(self):
"""Create all the widgests that we need"""
"""Create the Text"""
self.lbRSSSiteText = Label(self, text="All of your Routes :: Double click to edit route")
self.lbRSSSiteText.grid(row=0, column=0, sticky=W)

"""Create the First ListBox"""
scrollbarV = Scrollbar(self, orient=VERTICAL)
scrollbarH = Scrollbar(self, orient=HORIZONTAL)

self.lbSites = Listbox(self, selectmode=BROWSE
, yscrollcommand=scrollbarV.set
, xscrollcommand=scrollbarH.set
, relief=SUNKEN)
self.lbSites.grid(row=1, column=0, columnspan=4, sticky=N+W+S+E)
"""Show the scrollbars and attatch them"""
scrollbarV.grid(row=1, column=5, sticky=N+S)
scrollbarV.config(command=self.lbSites.yview)
scrollbarH.grid(row=2, column=0, columnspan=5, sticky=E+W)
scrollbarH.config(command=self.lbSites.xview)

self.btnView = Button(self, text="View", command=self.Show)
self.btnView.grid(column=1, row=3, stick=E, pady=5)
self.btnView = Button(self, text="Add New", command=self.Add)
self.btnView.grid(column=2, row=3, stick=E, pady=5)
self.btnView = Button(self, text="Delete", command=self.Delete)
self.btnView.grid(column=3, row=3, stick=E, pady=5)

def Show(self):
cmd = "netstat -nr"
self.lbSites.delete(0, END)
for line in os.popen(cmd).readlines():
line = line[:-1]
self.lbSites.insert(END, line)

self.lbSites.bind('<Double-1>', self.Edit)

def Edit(self, event, main="NULL"):
"""Add an RSSItem"""
lstCurrSel = self.lbSites.curselection();
main = "";
main = self.lbSites.get(lstCurrSel)
parts = main.split()
send = ['""', '""', '""', '""', '""', '""', '']
for i in range(len(parts)):
send=parts
# print send
new_new = NewItemDialog(self.master,"Route Management",dest=send[0], gw=send[1], flags=send[2],refs=send[3],use=send[4],netif=send[5],exp=send[6])

def Add(self):
"""docstring for Add"""
new_new = NewItemDialog(self.master,"Route Management")

def Delete(self):
"""docstring for Delete"""
lstCurrSel = self.lbSites.curselection();
main = self.lbSites.get(lstCurrSel)
parts = main.split()
# print parts[0]
num = parts[0].count('.')

if num == 0:
parts[0] = parts[0] + ".0.0.0"
elif num == 1:
parts[0] = parts[0] + ".0.0"
elif num == 2:
parts[0] = parts[0] + ".0"


cmd = 'sudo route delete ' + parts[0] + '/24'
os.popen(cmd)
self.Show()

if __name__ == "__main__":
guiFrame = GUIFramework()
guiFrame.mainloop()
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.