Selaa lähdekoodia

begin macvlan

macvlan
THONI Robin 9 vuotta sitten
vanhempi
commit
6532a4d133
4 muutettua tiedostoa jossa 38 lisäystä ja 2 poistoa
  1. 2
    0
      .gitignore
  2. 1
    1
      dhcp_client.py
  3. 1
    1
      test.py
  4. 34
    0
      vmac.py

+ 2
- 0
.gitignore Näytä tiedosto

@@ -1,3 +1,5 @@
1
+*.swp
2
+
1 3
 # Byte-compiled / optimized / DLL files
2 4
 __pycache__/
3 5
 *.py[cod]

+ 1
- 1
dhcp_client.py Näytä tiedosto

@@ -28,7 +28,7 @@ class DhcpClient:
28 28
 
29 29
 
30 30
   def get_offer(self, mac, iface, timeout = 10):
31
-    pc = pcap.pcap(name=iface)
31
+    pc = pcap.pcap(name=iface, promisc=True)
32 32
     pc.setfilter('udp and udp src port 67 and udp dst port 68')
33 33
     xid = [random.randrange(255), random.randrange(255),
34 34
         random.randrange(255), random.randrange(255)]

+ 1
- 1
test.py Näytä tiedosto

@@ -3,7 +3,7 @@
3 3
 import dhcp_client
4 4
 
5 5
 dh = dhcp_client.DhcpClient()
6
-offer = dh.get_offer('00:15:00:ba:60:da', 'eth0')
6
+offer = dh.get_offer('6c:3b:e5:1f:96:46', 'vmac')
7 7
 #offer = dh.get_offer('08:00:27:45:60:dc', 'wlan0')
8 8
 if offer == None:
9 9
     print("Timeout")

+ 34
- 0
vmac.py Näytä tiedosto

@@ -0,0 +1,34 @@
1
+#! /usr/bin/env python
2
+import subprocess
3
+import dhcp_client
4
+
5
+def create_viface(iface, viface, mac):
6
+    if subprocess.call(['ip', 'link', 'add', 'link', iface, viface,
7
+        'address', mac, 'type', 'macvlan']) != 0:
8
+        return False
9
+    if subprocess.call(['ip', 'link', 'dev', 'set', viface, 'up']) != 0:
10
+        return False
11
+    return True
12
+
13
+def remove_viface(viface):
14
+    return subprocess.call(['ip', 'link', 'del', viface]) == 0
15
+
16
+iface = 'em1'
17
+viface = 'vmac'
18
+mac = '6c:3b:e5:1f:96:46'
19
+
20
+if not create_viface(iface, viface, mac):
21
+    print('Failed to create virtual interface')
22
+    quit(1)
23
+
24
+dh = dhcp_client.DhcpClient()
25
+offer = dh.get_offer(mac, viface)
26
+
27
+if offer == None:
28
+    print('DHCP client timeout')
29
+else:
30
+    print(offer.str())
31
+
32
+if not remove_viface(viface):
33
+    printf('Failed to remove virtual interface')
34
+    quit(4)

Loading…
Peruuta
Tallenna