11

I am using Mac OSx to access my Pi via SSH. To access it, I know that I need the Pi's IP address. So, I decided to use a few Python commands which have always succeeded in giving me the local IP.

import socket
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
print host_ip

Oddly enough, I have only gotten the local host IP (127.0.1.1).

Can someone please tell me how to get the proper local / global (whichever is better, this only needs to work over LAN for now) IP for the PI?

11

If Python is a must, you can check this article: http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-network-inter/

They rightly point out that a computer has as many IP addresses as network interfaces.

Here is their code to get the IP address of a network adapter, I've tested the code and it works:

import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

print get_ip_address('lo')
print get_ip_address('eth0')

If your RPi is connected via WiFi the adapter name should be wlan0

Otherwise you can get it with the ip command:

ip addr show eth0 | grep inet

You should get something like:

inet 192.168.0.140/24 brd ....

That's your IP address.

Of course none of this will work if you just boot up your RPi and could not connect to it because you don't know the IP address! If' that's the case your DHCP server will surely be able to tell you which IP address he just gave to the RPi (or you just try them all :) )

That said, you might benefit of referencing you RPi by name and, since you're using a Mac, probably the best solution would be to install avahi-daemon:

sudo apt-get install avahi-daemon

which implements the Apple Zeroconf specification (like Bonjour). You can then ssh to your RPi by name:

ssh pi@raspberry.local

I used a different approach for my two Raspberrys (named ygdrasill and dvalinn): I've opted for a fixed IP (192.168.0.140 and 192.168.0.142) so I can also use etc/hosts file on some of my other machines.

I could have set up an internal DNS or rely on Wins, but current solution works for me.

  • Hi Remo, Thank for explaining this in detail. I tried you code on python 3.5 running on rasp pi 3. It doesn't work and result in a runtime error. "struct.error: argument for 's' must be a bytes object" – Amit Rai Sharma Dec 8 '16 at 12:45
  • I believe this should be ssh pi@raspberrypi.local given the default hostname for a RPi is raspberrypi. Why avahi-daemon is not universally installed in the Debian world is beyond me. Ad-hoc networks are a dream when it works out of the box. – Heath Raftery May 14 at 22:38
  • In contrast to my previous comment, in the lastest Raspbian (Apr19) as of now (May19), both raspberrypi.local resolution and 169.254.x.x auto-assignment work without configuration out of the box! – Heath Raftery May 16 at 5:21
1

This code :

import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

print get_ip_address('lo')
print get_ip_address('eth0')

only works under python 2.7, it doesn't work with python 3, you'll get the error that the argument for 's' must be a byte object...

  • What does the answer help for the question? Do you have a version that works under python3? – Ingo May 5 at 18:57
0

socket may return the localhost, depending on how the OS is set up. So it isn't portable.

This stackoverflow answer about getting IP via python is helpful- it explains the problem with /etc/hosts and gives a decent workaround. It isn't perfect (ipv6), and if you have several bound IPs it may not find the routable version, but for your needs it's a good oneliner.

From the commandline, ifconfig is your friend. It'll typically list all of your bound interfaces and such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.