Last active
August 21, 2016 17:32
-
-
Save vik-y/f0c853032368d93f9d90e5575a050100 to your computer and use it in GitHub Desktop.
Script to help in operating ovs on ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Author : Vikas Yadav | |
DISCLAIMER: | |
Running this script requires root privileges | |
You can loose internet connectivity after using this script. So proceed with | |
caution. | |
This should not be used on systems which have two physical interfaces connected | |
to internet. | |
This should not be used on system which already has a ovs bridge up and running. | |
''' | |
import os | |
import sys | |
def gatewayIP(): | |
cmd = """route -n | grep UG | awk '{split($0,a," "); print a[2]}'""" | |
result = os.popen(cmd).read() | |
return result[:-1] | |
def gatewayInterface(): | |
cmd = """route -n | grep UG | awk '{split($0,a," "); print a[8]}'""" | |
result = os.popen(cmd).read() | |
return result[:-1] | |
''' | |
Please make sure that "interface" passed in this argument is connected to the | |
"bridge" otherwise this will make no difference. | |
''' | |
def reset(bridge, interface): | |
cmd = "ovs-vsctl del-port %s %s" % (bridge, interface) | |
os.system(cmd) | |
os.system("ifconfig %s down" % (bridge)) | |
''' | |
Please make sure that "interface" passed in this argument is connected to Internet | |
''' | |
def connect(bridge, interface): | |
# Connect interface to bridge | |
cmd = "ovs-vsctl add-port %s %s" % (bridge, interface) | |
os.system(cmd) | |
# Change the default gateway | |
cmd = """ifconfig %s | grep "inet addr" """ % (interface) | |
try: | |
current_ip = os.popen(cmd).read().split(":")[1].split(" ")[0] | |
print current_ip | |
netmask = os.popen(cmd).read().split(":")[-1][:-1] | |
print netmask | |
gateway = gatewayIP() | |
except: | |
print "Error occured. Cannot proceed further" | |
return 0 | |
# Bring up the bridge interface in case it is down | |
os.system("ifconfig %s up" % (bridge)) | |
# Set the ip of current interface as 0 | |
os.system("ifconfig %s 0" %(interface)) | |
# change the ip of bridged interface | |
os.system("ifconfig %s %s && ifconfig %s netmask %s" % (bridge, current_ip, bridge, netmask)) | |
os.system("route add default gw %s %s" % (gateway, bridge)) | |
print "OVS Switch Enabled. Now create a tuntap interface and enjoy" | |
''' | |
from ovs-connect import * | |
''' | |
# Enter name of the Interface which is connected to Internet or the interface | |
# which you want to bridge to the Internet. | |
INTERFACE = "wlp3s0" | |
# Enter the name of OVS Bridge which you created | |
BRIDGE = "br0" | |
if sys.argv[1] == "reset": | |
reset(BRIDGE, INTERFACE) | |
if sys.argv[1] == "connect": | |
connect(BRIDGE, INTERFACE) | |
#print gatewayIP() | |
#print gatewayInterface() | |
#refresh("br0", "wlp3s0") | |
#connect("br0", gatewayInterface()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment