-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchCalls.py
executable file
·86 lines (74 loc) · 2.41 KB
/
SwitchCalls.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
"""
Usage:
switch_auditor_cli.py [list_vlans PORT]
switch_auditor_cli.py [list_ports VLAN]
Examples:
SwitchCalls.py list_ports 100
SwitchCalls.py list_vlans veth-4
Arguments:
PORT currently selected port
VLAN currently selected VLAN
"""
import re
import logging
import ast
import subprocess
import shlex
import json
logger = logging.getLogger(__name__)
import json
import logging
try:
from docopt import docopt
except ImportError:
exit("Docopt needs to be installed in order to display CLI arguments.\n" +\
"Please run 'pip install docopt==0.6.2' or install from " +\
"'https://github.com/docopt/docopt/'.")
def list_vlans():
#shell_cmd = "sudo ovs-vsctl list port {port}".format(port=ARGUMENTS["PORT"])
shell_cmd = "sudo ovs-vsctl show"
args = shlex.split(shell_cmd)
try:
output = subprocess.check_output(args)
except subprocess.CalledProcessError as e:
logger.error(" %s ", e)
raise SwitchError('Ovs command failed: ')
port_found = 0
output = output.split('\n')
for x in range(0, len(output)):
if port_found > 0:
if output[x].find("tag") > 0:
print(output[x].replace("tag", "VLAN").lstrip())
if output[x].find("Port \"{port}\"".format(port=ARGUMENTS["PORT"])) > 0:
port_found = 1
if port_found > 0 and output[x].find("Port") > 0 and output[x].find("Port \"{port}\"".format(port=ARGUMENTS["PORT"])) < 0:
port_found = 0
def list_ports():
shell_cmd = "sudo ovs-vsctl show"
args = shlex.split(shell_cmd)
try:
output = subprocess.check_output(args)
except subprocess.CalledProcessError as e:
logger.error(" %s ", e)
raise SwitchError('Ovs command failed: ')
vlan_found = 0
output = output.split('\n')
for x in range(0, len(output)):
if vlan_found > 0:
print(output[x-2].lstrip().replace("Port", "Port:").replace("\"", ""))
vlan_found = 0
if output[x].find("tag: {vlan}".format(vlan=ARGUMENTS["VLAN"])) > 0:
vlan_found = 1
if vlan_found > 0 and output[x].find("Port") > 0:# and output[x].find("Port \"{port}\"".format(port=ARGUMENTS["PORT"])) < 0:
vlan_found = 0
if __name__ == '__main__':
ARGUMENTS = docopt(__doc__)
# for debugging purposes
# LOGGER.debug(ARGUMENTS)
#print ARGUMENTS
ARGUMENTS["PROJECT"] = "default"
if ARGUMENTS["list_vlans"]:
list_vlans()
elif ARGUMENTS["list_ports"]:
list_ports()