[2f45140] | 1 | #!/usr/bin/python |
---|
| 2 | import subprocess |
---|
| 3 | import re |
---|
| 4 | import os |
---|
| 5 | import sys, getopt |
---|
| 6 | |
---|
| 7 | class Peer: |
---|
| 8 | @staticmethod |
---|
| 9 | def getIP(interface): |
---|
| 10 | proc = subprocess.Popen(['ifconfig',interface],stdout=subprocess.PIPE) |
---|
| 11 | exit_code = os.waitpid(proc.pid, 0) |
---|
| 12 | result = proc.communicate()[0] |
---|
| 13 | m = re.search('inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})',result) |
---|
| 14 | if not m: |
---|
| 15 | raise LookupError('Interface does not exist or IP is not assigned!') |
---|
| 16 | else: |
---|
| 17 | return m.group(1) |
---|
| 18 | @staticmethod |
---|
| 19 | def setIP(interface,ip): |
---|
| 20 | proc = subprocess.Popen(['ifconfig',interface,ip,"up"],stdout=subprocess.PIPE) |
---|
| 21 | exit_code = os.waitpid(proc.pid, 0) |
---|
| 22 | @staticmethod |
---|
| 23 | def setBridge(bridgename): |
---|
| 24 | proc = subprocess.Popen(['brctl', 'addbr',bridgename],stdout=subprocess.PIPE) |
---|
| 25 | exit_code = os.waitpid(proc.pid, 0) |
---|
| 26 | @staticmethod |
---|
| 27 | def addIfaceToBridge(iface,bridge): |
---|
| 28 | proc = subprocess.Popen(['brctl','addif',bridge,iface],stdout=subprocess.PIPE) |
---|
| 29 | exit_code = os.waitpid(proc.pid, 0) |
---|
| 30 | @staticmethod |
---|
| 31 | def delBridge(bridgename): |
---|
| 32 | proc = subprocess.Popen(['brctl', 'delbr',bridgename],stdout=subprocess.PIPE) |
---|
| 33 | exit_code = os.waitpid(proc.pid, 0) |
---|
| 34 | @staticmethod |
---|
| 35 | def ifUp(interface): |
---|
| 36 | proc = subprocess.Popen(['ifconfig',interface,"up"],stdout=subprocess.PIPE) |
---|
| 37 | exit_code = os.waitpid(proc.pid, 0) |
---|
| 38 | #add the port and make sure you increase the tunnel number! |
---|
| 39 | @staticmethod |
---|
| 40 | def tunnelConnect(dest,key,bridge,iface): |
---|
| 41 | print ['ssh','-o','Tunnel=ethernet','-f','-w','0:0','-p','22','-i',key,dest,'true'] |
---|
| 42 | proc = subprocess.call(['ssh','-o','Tunnel=ethernet','-f','-w','0:0','-p','22','-i',key,dest,'true']) |
---|
| 43 | #Need to be fixed |
---|
| 44 | Peer.addIfaceToBridge('tap0',bridge) |
---|
| 45 | Peer.ifUp(iface) |
---|
| 46 | Peer.ifUp('tap0') |
---|
| 47 | Peer.ifUp(bridge) |
---|
| 48 | try: |
---|
| 49 | opts, args = getopt.getopt(sys.argv[1:],"d:k:b:i:") |
---|
| 50 | dest = None |
---|
| 51 | key = None |
---|
| 52 | bridge = None |
---|
| 53 | interface = None |
---|
| 54 | for opt, arg in opts: |
---|
| 55 | if opt == '-d': |
---|
| 56 | dest = arg |
---|
| 57 | elif opt == '-k': |
---|
| 58 | key = arg |
---|
| 59 | elif opt == '-b': |
---|
| 60 | bridge = arg |
---|
| 61 | elif opt == '-i': |
---|
| 62 | interface = arg |
---|
| 63 | if not dest: |
---|
| 64 | print "Missing Arguemnts! destionation" |
---|
| 65 | sys.exit(2) |
---|
| 66 | if not key: |
---|
| 67 | print "Missing Arguemnts! key" |
---|
| 68 | sys.exit(2) |
---|
| 69 | if not bridge: |
---|
| 70 | print "Missing Arguemnts! bridge" |
---|
| 71 | sys.exit(2) |
---|
| 72 | if not interface: |
---|
| 73 | print "Missing Arguemnts! interface" |
---|
| 74 | sys.exit(2) |
---|
| 75 | |
---|
| 76 | Peer.tunnelConnect(dest,key,bridge,interface) |
---|
| 77 | except getopt.GetoptError: |
---|
| 78 | #print "peer_setup.py -d=destination -k=key -b=bridge -i=interface" |
---|
| 79 | #sys.exit(2) |
---|
| 80 | pass |
---|
| 81 | |
---|