#!/usr/bin/python import subprocess import re import os import sys, getopt class Peer: @staticmethod def getIP(interface): proc = subprocess.Popen(['ifconfig',interface],stdout=subprocess.PIPE) exit_code = os.waitpid(proc.pid, 0) result = proc.communicate()[0] m = re.search('inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})',result) if not m: raise LookupError('Interface does not exist or IP is not assigned!') else: return m.group(1) @staticmethod def setIP(interface,ip): proc = subprocess.Popen(['ifconfig',interface,ip,"up"],stdout=subprocess.PIPE) exit_code = os.waitpid(proc.pid, 0) @staticmethod def setBridge(bridgename): proc = subprocess.Popen(['brctl', 'addbr',bridgename],stdout=subprocess.PIPE) exit_code = os.waitpid(proc.pid, 0) @staticmethod def addIfaceToBridge(iface,bridge): proc = subprocess.Popen(['brctl','addif',bridge,iface],stdout=subprocess.PIPE) exit_code = os.waitpid(proc.pid, 0) @staticmethod def delBridge(bridgename): proc = subprocess.Popen(['brctl', 'delbr',bridgename],stdout=subprocess.PIPE) exit_code = os.waitpid(proc.pid, 0) @staticmethod def ifUp(interface): proc = subprocess.Popen(['ifconfig',interface,"up"],stdout=subprocess.PIPE) exit_code = os.waitpid(proc.pid, 0) #add the port and make sure you increase the tunnel number! @staticmethod def tunnelConnect(dest,key,bridge,iface): print ['ssh','-o','Tunnel=ethernet','-f','-w','0:0','-p','22','-i',key,dest,'true'] proc = subprocess.call(['ssh','-o','Tunnel=ethernet','-f','-w','0:0','-p','22','-i',key,dest,'true']) #Need to be fixed Peer.addIfaceToBridge('tap0',bridge) Peer.ifUp(iface) Peer.ifUp('tap0') Peer.ifUp(bridge) try: opts, args = getopt.getopt(sys.argv[1:],"d:k:b:i:") dest = None key = None bridge = None interface = None for opt, arg in opts: if opt == '-d': dest = arg elif opt == '-k': key = arg elif opt == '-b': bridge = arg elif opt == '-i': interface = arg if not dest: print "Missing Arguemnts! destionation" sys.exit(2) if not key: print "Missing Arguemnts! key" sys.exit(2) if not bridge: print "Missing Arguemnts! bridge" sys.exit(2) if not interface: print "Missing Arguemnts! interface" sys.exit(2) Peer.tunnelConnect(dest,key,bridge,interface) except getopt.GetoptError: #print "peer_setup.py -d=destination -k=key -b=bridge -i=interface" #sys.exit(2) pass