+
+
+def interface_up(iface):
+ """
+ This function brings given iface up.
+ """
+ ret, _out, _err = start_process(["ifconfig", iface, "up"])
+ return ret
+
+
+def interface_assign_ip(iface, ip_addr, mask):
+ """
+ This function allows to assign IP address to an interface. If mask is an
+ empty string then ifconfig will decide what kind of mask to use. The
+ caller can also specify the mask by using CIDR notation in ip argument by
+ leaving the mask argument as an empty string. In case of success this
+ function returns 0.
+ """
+ args = ["ifconfig", iface, ip_addr]
+ if mask is not None:
+ args.append("netmask")
+ args.append(mask)
+ ret, _out, _err = start_process(args)
+ return ret
+
+
+def interface_get_ip(iface):
+ """
+ This function returns tuple - ip and mask that was assigned to the
+ interface.
+ """
+ args = ["ifconfig", iface]
+ ret, out, _err = start_process(args)
+
+ if ret == 0:
+ ip = re.search(r'inet addr:(\S+)', out)
+ mask = re.search(r'Mask:(\S+)', out)
+ if ip is not None and mask is not None:
+ return (ip.group(1), mask.group(1))
+ else:
+ return ret
+
+
+def move_routes(iface1, iface2):
+ """
+ This function moves routes from iface1 to iface2.
+ """
+ args = ["ip", "route", "show", "dev", iface1]
+ ret, out, _err = start_process(args)
+ if ret == 0:
+ for route in out.splitlines():
+ args = ["ip", "route", "replace", "dev", iface2] + route.split()
+ start_process(args)