-
Notifications
You must be signed in to change notification settings - Fork 0
/
config
executable file
·37 lines (26 loc) · 940 Bytes
/
config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python
import argparse
import yaml
import iproute
parser = argparse.ArgumentParser()
parser.set_defaults(config=None, action=None)
parser.add_argument("profile")
parser.add_argument("action")
options = parser.parse_args()
with open("config.yaml") as stream:
configuration = yaml.load(stream)
profile_name = options.profile
profile = configuration["profiles"][profile_name]
action = options.action
interface = profile["interface"]
assert action in ["up", "down"]
add_delete = {"up": "add", "down": "delete"}[action]
def run(commands):
for command in commands:
print(' '.join(command))
if action == "up":
run(iproute.ip_link_set_up(interface))
for address in profile.get("addresses", []):
run(iproute.ip_address(add_delete, address, interface, profile.get("alias")))
for route in profile.get("routes", []):
run(iproute.ip_route(add_delete, route["network"], route.get("gateway"), interface))