-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowvnet.py
40 lines (28 loc) · 1.1 KB
/
showvnet.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
import os
def get_mac_vnet_map():
buff = os.popen("ifconfig | grep -E 'vnet|ether' | gawk '{printf (\"%s\\n%s\\n\", $1, $2)}'").readlines()
mac_vnet = {} # mac to vnet dict
for i in range(0, len(buff)):
if buff[i][0] == 'v':
mac_vnet[buff[i + 3][11:(len(buff[i + 3]) - 1)]] = buff[i][0:(len(buff[i]) - 2)]
return mac_vnet
def get_mac_port_map(mac_vnet):
mac_port = {} # mac to port dict
for mac in mac_vnet:
cmd = "sudo ovs-vsctl get interface " + mac_vnet[mac] + " ofport"
tmpint = os.popen(cmd).readlines()
if len(tmpint): # jundge length of list
mac_port[mac] = tmpint[0][0:len(tmpint[0]) - 1]
return mac_port
def get_vnet_port_map(mac_port, mac_vnet): # maybe no need
vnet_port = {} # vnet to port dict
for mac in mac_port:
vnet_port[mac_vnet[mac]] = mac_port[mac]
return vnet_port
if __name__ == "__main__":
mac_vnet = get_mac_vnet_map()
mac_port = get_mac_port_map(mac_vnet)
vnet_port = get_vnet_port_map(mac_port, mac_vnet)
print(mac_vnet)
print(mac_port)
print(vnet_port)