2011-09-11 61 views
1

我已經通過設置一些虛擬IP工作:蟒蛇socket.connect似乎並不在一個虛擬的IP

~# ip link add link eth0 name eth0.1 address 11:22:33:44:55:66 type macvlan 
~# ifconfig eth0.1 10.10.0.0/24 

我使用下面的代碼從它連接:

sTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
sTCP.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, IFACE) 
print "PORT s_TCP:" + str(HOST) +":" +str(TCP_PORT) 
sTCP.connect((HOST, TCP_PORT)) 
print "Connected" 

如果IFACE是eth0,這可以正常工作,但它不會從eth0.1通過sTCP.connect,並且在eth0.2上無法通過bindtodevice(與預期的一樣)。

爲什麼eth0.1不起作用?這是一個python問題,還是在linux網絡實現中的東西?

回答

1

我剛剛在我的Fedora 13系統上試過這個,它工作。我必須做一些修改才能使其在我的系統上運行,希望這會給你提供線索。使用的代碼:

### in shell 
# Used 00 for first MAC octet to avoid issues with multicast addressing 
ip link add link eth0 name eth0.1 address 00:22:33:44:55:66 type macvlan 
ifconfig eth0.1 10.1.23.6/25 

# python 
import socket 
HOST = "10.1.23.30" 
TCP_PORT = 80 
IFACE = "eth0.1" 
sTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
# switched to socket.SO_BINDTODEVICE since I'm not sure what "IN" referred to 
# EDIT: figured out there's another module called IN, but the value is the same (25) 
sTCP.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, IFACE) 
print "PORT s_TCP:" + str(HOST) +":" +str(TCP_PORT) 
sTCP.connect((HOST, TCP_PORT)) 
print "Connected" 

我用tcpdump來證明數據包來自eth0.1。也許你遇到了VLAN問題?在客戶端和服務器上運行數據包捕獲以查看線路上實際發生的情況。