5

I am trying to connect/bind to a specific Network Interface Card (NIC) or wireless network interface. For instance, when I create a socket, I want to connect using the name of the network (such as 'wlan0' or 'eth0') instead of using the IP address. In JAVA I can do this easily with the following code:

//Initializing command socket

//String networkCard = "wlan0"; //or could be "eth0", etc.

NetworkInterface nif = NetworkInterface.getByName(networkCard);

Enumeration<InetAddress> nifAddresses = nif.getInetAddresses();

// IP address of robot connected to NIC       
SocketAddress sockaddr = new InetSocketAddress("192.168.1.100", 80);

sock = new Socket();

// bind to the specific NIC card which is connected to a specific robot

sock.bind(new InetSocketAddress(nifAddresses.nextElement(), 0));

sock.connect(sockaddr,10000);

I want to translate this to Python, but I am having a hard time. Any suggestions on how to do this?

I was using sockopt and the AF_CAN, but nothing is working.

Thank you very much!!!

Pototo
  • 691
  • 1
  • 12
  • 27
  • Show us the Python code you have so far. – John Zwinck Nov 09 '13 at 03:59
  • Why not let the OS choose the right interface for you? Just create a socket connecting to the robot's IP, the OS will pick the correct interface based on the routes it knows about. – korylprince Nov 09 '13 at 04:08
  • Agreed, this is what routing tables are for. – dstromberg Nov 09 '13 at 05:18
  • I have several robots, and they all have the same IP address (each robot produces its own, unchangeable ad-hoc network) which is why I use the NIC's, so that I can connect to each one individually – Pototo Nov 10 '13 at 21:20
  • so far is this: *def connect_rover(self): *nic = 'wlan0' // or other name *self.move_socket = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW) *self.move_socket.settimeout(1000) *self.move_socket.bind((nic,)) *print (self.move_socket.getsockname()) *// all robots' have same IP address: '192.168.1.100' *self.move_socket.connect(('192.168.1.100', 80)) *self.move_socket.setblocking(1) – Pototo Nov 12 '13 at 14:29
  • each " * " (or star) means "new line" in this comment (formatting issue) – Pototo Nov 12 '13 at 14:29
  • thanks for you help guys! I appreciate it! I've been stuck on this for a while. I can't believe is so hard to do this on python than in JAVA :-( ...seems like an oxymoron haha – Pototo Nov 12 '13 at 14:31

2 Answers2

2

Actually the answer is very simple. And its similar to what Idx did in the previous answer:

def findConnectedRobot():

'''
Finds which robots are connected to the computer and returns the
addresses of the NIC they are connected to
'''
robot_address = []  # stores NIC address
import netifaces
# get the list of availble NIC's
for card in netifaces.interfaces():
    try:
        # get all NIC addresses
        temp = netifaces.ifaddresses(\
                card)[netifaces.AF_INET][0]['addr']
        temp2 = temp.split('.')
        # see if address matches common address given to NIC when
        # NIC is connected to a robot
        if temp2[0] == '192' and int(temp2[3]) < 30:
            print('appending address: ' + temp)
            robot_address.append(temp)
    except BaseException:
        pass
return robot_address

After I get the "robot addresses" then I can just bind/connected to them like a normal socket.

Thanks for the help!

Pototo
  • 691
  • 1
  • 12
  • 27
1

You need libnl and its python bindings:

#!/usr/bin/env python

import netlink.core as netlink
import netlink.route.link as link
import netlink.route.address as Address

sock = netlink.Socket()
sock.connect(netlink.NETLINK_ROUTE)

cache = link.LinkCache()
cache.refill(sock)
intf = cache['wlan0']

addr_cache = Address.AddressCache()
addr_cache.refill()

for addr in addr_cache:
    if addr.ifindex == intf.ifindex:
        print addr
ldx
  • 3,984
  • 23
  • 28
  • I get this error when I run the code: Traceback (most recent call last): File "test_net.py", line 17, in for addr in addr_cache: File "/usr/local/lib/python2.7/dist-packages/netlink/core.py", line 452, in next return self.__next__(self) TypeError: __next__() takes exactly 1 argument (2 given) Am I missing something crucial? Thanks for your help – Pototo Nov 11 '13 at 00:20
  • Oh, that's a bug in the lib. Change that line in core.py to use `self.__next__()` instead of `self.__next__(self)`. – ldx Nov 11 '13 at 21:25
  • oohhh, I see what your code does. I actually did the same thing with this: *import netifaces *netifaces.interfaces() *netifaces.ifaddresses('wlan0')[netifaces.AF_INET][0]['addr'] * each start " * " means new line in the comment. – Pototo Nov 12 '13 at 14:33
  • I failed to mention that all the devices/robots I'm connecting to have the same IP address. That is why its important to bind to the NIC rather than just connecting to an IP address. If I could just connect saying something like socket.connect(('wlan0', 80)), similar to my JAVA code, then that would be more than perfect. Sorry about the fact that I am not giving you guys all the details from the beginning. I added my code so far in the comment above at the beginning of the thread – Pototo Nov 12 '13 at 14:34