cryptonas-branches/pythonrewrite/plugins/network/network.py

67 lines
1.7 KiB
Python

from CryptoBoxExceptions import CBPluginActionError
import re
import subprocess
import imp
import os
def prepareForm(hdf, cbox):
(oc1, oc2, oc3, oc4) = __getCurrentIP(cbox)
hdf["Data.Modules.network.ip.oc1"] = oc1
hdf["Data.Modules.network.ip.oc2"] = oc2
hdf["Data.Modules.network.ip.oc3"] = oc3
hdf["Data.Modules.network.ip.oc4"] = oc4
def doAction(cbox, store=None, ip1="", ip2="", ip3="", ip4=""):
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:
raise CBPluginActionError, "InvalidIP"
if __setIP(cbox, ip):
return "form_system"
else:
raise CBPluginActionError, "InvalidIP"
else:
return "form_network"
def getStatus(cbox):
return "%d.%d.%d.%d" % __getCurrentIP(cbox)
def __getCurrentIP(cbox):
root_action_mod = 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_mod.IFCONFIG_BIN,
root_action_mod.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