Added
Link Here
|
1 |
--- google_compute_engine/ip_forwarding/ip_forwarding_utils.py.orig 2017-06-10 00:18:58 UTC |
2 |
+++ google_compute_engine/ip_forwarding/ip_forwarding_utils.py |
3 |
@@ -17,6 +17,8 @@ |
4 |
|
5 |
import re |
6 |
import subprocess |
7 |
+import netifaces |
8 |
+import netaddr |
9 |
|
10 |
IP_REGEX = re.compile(r'\A(\d{1,3}\.){3}\d{1,3}\Z') |
11 |
IP_ALIAS_REGEX = re.compile(r'\A(\d{1,3}\.){3}\d{1,3}/\d{1,2}\Z') |
12 |
@@ -51,8 +53,8 @@ class IpForwardingUtils(object): |
13 |
options.update(kwargs) |
14 |
return options |
15 |
|
16 |
- def _RunIpRoute(self, args=None, options=None): |
17 |
- """Run a command with ip route and return the response. |
18 |
+ def _RunIfconfig(self, args=None, options=None): |
19 |
+ """Run a command with ifconfig and return the response. |
20 |
|
21 |
Args: |
22 |
args: list, the string ip route command args to execute. |
23 |
@@ -63,7 +65,7 @@ class IpForwardingUtils(object): |
24 |
""" |
25 |
args = args or [] |
26 |
options = options or {} |
27 |
- command = ['ip', 'route'] |
28 |
+ command = ['ifconfig'] |
29 |
command.extend(args) |
30 |
for item in options.items(): |
31 |
command.extend(item) |
32 |
@@ -108,10 +110,15 @@ class IpForwardingUtils(object): |
33 |
Returns: |
34 |
list, the IP address strings. |
35 |
""" |
36 |
- args = ['ls', 'table', 'local', 'type', 'local'] |
37 |
- options = self._CreateRouteOptions(dev=interface) |
38 |
- result = self._RunIpRoute(args=args, options=options) |
39 |
- return self.ParseForwardedIps(result.split()) |
40 |
+ try: |
41 |
+ ips = netifaces.ifaddresses('lo' + self.proto_id) |
42 |
+ ips = ips[netifaces.AF_INET] |
43 |
+ except (ValueError, KeyError) as e: |
44 |
+ return [] |
45 |
+ forwarded_ips = [] |
46 |
+ for ip in ips: |
47 |
+ forwarded_ips.append(ip['addr'] + '/' + str(netaddr.IPAddress(ip['netmask']).netmask_bits())) |
48 |
+ return self.ParseForwardedIps(forwarded_ips) |
49 |
|
50 |
def AddForwardedIp(self, address, interface): |
51 |
"""Configure a new IP address on the network interface. |
52 |
@@ -121,9 +128,12 @@ class IpForwardingUtils(object): |
53 |
interface: string, the output device to use. |
54 |
""" |
55 |
address = address if IP_ALIAS_REGEX.match(address) else '%s/32' % address |
56 |
- args = ['add', 'to', 'local', address] |
57 |
- options = self._CreateRouteOptions(dev=interface) |
58 |
- self._RunIpRoute(args=args, options=options) |
59 |
+ cmd = 'alias' |
60 |
+ try: |
61 |
+ forwarded_ips = netifaces.ifaddresses(interface) |
62 |
+ except (ValueError, KeyError) as e: |
63 |
+ cmd = 'create' |
64 |
+ self._RunIfconfig(args=[interface, cmd, address]) |
65 |
|
66 |
def RemoveForwardedIp(self, address, interface): |
67 |
"""Delete an IP address on the network interface. |
68 |
@@ -132,7 +142,5 @@ class IpForwardingUtils(object): |
69 |
address: string, the IP address to configure. |
70 |
interface: string, the output device to use. |
71 |
""" |
72 |
- address = address if IP_ALIAS_REGEX.match(address) else '%s/32' % address |
73 |
- args = ['delete', 'to', 'local', address] |
74 |
- options = self._CreateRouteOptions(dev=interface) |
75 |
- self._RunIpRoute(args=args, options=options) |
76 |
+ address = address if IP_REGEX.match(address) else address[:-3] |
77 |
+ self._RunIfconfig(args=[interface, '-alias', address]) |