我的主机上安装了四个以太网卡,我想在python中发送用户定义的数据包(在以太网等之上)。它有什么解决方案吗?
我已设法在C中执行以下代码:
if ((sk = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_USER))) < 0) {
perror("create socket error\n");
return -1;
}
// bind the interface like "eth0"
memset(&nsa_ifr, 0, sizeof(nsa_ifr));
strncpy(nsa_ifr.ifr_name, dev_name, sizeof(nsa_ifr.ifr_name));
if (ioctl(sk, SIOCGIFINDEX, &nsa_ifr) < 0) {
perror("SIOCGIFINDEX error\n");
return -1;
}
memset(&nsa_dst, 0, sizeof(nsa_dst));
nsa_dst.sll_family = AF_PACKET;
nsa_dst.sll_ifindex = nsa_ifr.ifr_ifindex;
nsa_dst.sll_halen = ETH_ALEN;
nsa_dst.sll_protocol = htons(ETH_P_USER);
// Then I can send a user defined packet
n_send = sendto(sk, buf, size, 0, (struct sockaddr *) &nsa_dst, sizeof(nsa_dst));
我可以在python
中创建相同的套接字 sk = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0801))
但我不知道如何将套接字绑定到python中的命名网络接口, 任何想法?