cryptonas-branches/pythonrewrite/plugins/network/network.py
lars de3280806f language namespace for plugins separated
plugin interface changed ("prepareForm" removed)
plugins do not raise exceptions anymore
first part of the partitioning plugin
device-specific stuff moved to CryptoBoxTools
2006-09-14 12:33:01 +00:00

71 lines
1.9 KiB
Python

import re
import subprocess
import imp
import os
def doAction(hdf, cbox, store=None, ip1="", ip2="", ip3="", ip4=""):
__prepareFormData(hdf, cbox)
if store:
try:
for ip_in in (ip1, ip2, ip3, ip4):
if (int(ip_in) < 0) or (int(ip_in) > 255):
cbox.log.debug("invalid IP supplied: %s" % str((ip1,ip2,ip3,ip4)))
raise ValueError
ip = "%d.%d.%d.%d" % (int(ip1), int(ip2), int(ip3), int(ip4))
except ValueError:
hdf["Data.Warning"] = "Plugins.network.InvalidIP"
return "form_system"
if __setIP(cbox, ip):
hdf["Data.Success"] = "Plugins.network.IPChanged"
hdf["Data.Redirect.URL"] = ""
hdf["Data.Redirect.Delay"] = 30
return "form_system"
else:
hdf["Data.Warning"] = "Plugins.network.InvalidIP"
return "form_network"
else:
return "form_network"
def getStatus(cbox):
return "%d.%d.%d.%d" % __getCurrentIP(cbox)
def __prepareFormData(hdf, cbox):
(oc1, oc2, oc3, oc4) = __getCurrentIP(cbox)
hdf["Data.Plugins.network.ip.oc1"] = oc1
hdf["Data.Plugins.network.ip.oc2"] = oc2
hdf["Data.Plugins.network.ip.oc3"] = oc3
hdf["Data.Plugins.network.ip.oc4"] = oc4
def __getCurrentIP(cbox):
root_action_plug = imp.load_source("root_action", os.path.join(os.path.dirname(__file__), "root_action.py"))
proc = subprocess.Popen(
shell = False,
stdout = subprocess.PIPE,
args = [
root_action_plug.IFCONFIG_BIN,
root_action_plug.IFACE])
(output, error) = proc.communicate()
if proc.returncode != 0: return (0,0,0,0)
match = re.search(u'inet [\w]+:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\s',output)
if match:
return tuple([int(e) for e in match.groups()])
else:
return (0,0,0,0)
def __setIP(cbox, ip):
proc = subprocess.Popen(
shell = False,
args = [
cbox.prefs["Programs"]["super"],
cbox.prefs["Programs"]["CryptoBoxRootActions"],
"plugin",
os.path.join(os.path.dirname(__file__), "root_action.py"),
ip])
proc.communicate()
return proc.returncode == 0