m.fourb
Posts: 5
Joined: Fri May 22, 2015 4:10 pm

[Raspbian] Dynamically changing IP address for the Raspberry

Fri May 22, 2015 4:37 pm

Hello,

I am writing my Thesis at the University at the moment and i have to build and programm a little system to demonstrate my concept.

The concept is built in Python and i was able to assemble everything but one thing:
The Raspberry is being connected to my PC via Ethernet (no internet, only this local connection!).
As soon as it gets connected it receives an IP address from my PC under which it should register itself then.
Like this I can allocate a new IP adress to every new member of the network and also register all these numbers on my PC.

So far i have tried these things without success:

Editing the IP address /etc/network/interfaces and using the networking restart command afterwards (I tried ifup and down too). I don't know why but the Raspberry always stays in the Network with his old 169.254 address.

Is there a little quirk of these local networks that i missed?

Thanks in advance and greetings.


m.fourb

ghellquist
Posts: 68
Joined: Thu Aug 02, 2012 8:47 am
Location: Stockholm Sweden

Re: [Raspbian] Dynamically changing IP address for the Raspb

Fri May 22, 2015 7:09 pm

Check out DHCP

One computer on the net as master, possibly the PC.
//Gunnar

m.fourb
Posts: 5
Joined: Fri May 22, 2015 4:10 pm

Re: [Raspbian] Dynamically changing IP address for the Raspb

Fri May 22, 2015 7:36 pm

Thank you for the very quick reply!

I considered the DHCP Server myself before, but i can't really justify it in my thesis because there has to be a way without implementing another application.

But i just stumbled across the "sudo ifconfig eth0 X.X.X.X" command, it seems to work and it can be run out of a python script with the import os module. But like that the raspberry assigns it's IP to itself. That brings me back to another problem which i wasn't able to solve last week when i tried:

I would have to register one raspberry pi as a xml-rpc server and at the same time (or right afterwards) as a client.

Is there any way to achieve this transition from server to client or can I register it as both? (I tried the latter and it then always failed to receive RPCs.)
I have added tweo examples after my post, one is for the Server and one for the client.

Thanks in advance and greetings.

m. fourb

Client:

Code: Select all

import xmlrpc.client
    name='hello'
    clientAdressTemp='8000'
    clientIPtemp='169.254......'

    s = xmlrpc.client.ServerProxy("http://{ip}:{adress}"
                               .format(adress=clientAdressTemp, ip=clientIPtemp))

s.example1()             #Aufruf der 'example1' Funktion ohne Parameter
Server:

Code: Select all

from xmlrpc.server import SimpleXMLRPCServer

def hello(name='World'):
     print (name)
     return '%s!' % name

server = SimpleXMLRPCServer(("169.254......", 8000))

server.register_function(hello)

server.serve_forever(print ("server idle"))

User avatar
bobstro
Posts: 193
Joined: Wed Feb 05, 2014 6:48 am
Location: Central Massachusetts, US
Contact: Website

Re: [Raspbian] Dynamically changing IP address for the Raspb

Fri May 22, 2015 8:43 pm

Do you really need a TCP/IP stack for what you're doing? It sounds like you just want the RPis to find each other, no Internet or elaborate network required. If that's so, you could communicate at layer 2 using raw Ethernet frames. Of course, using TCP/IP is a lot easier.

m.fourb
Posts: 5
Joined: Fri May 22, 2015 4:10 pm

Re: [Raspbian] Dynamically changing IP address for the Raspb

Fri May 22, 2015 8:47 pm

You are right, for this demonstration i don't really need the TCP/IP stack, but later when it comes to the execution of the whole concept it will be needed. Sp the demonstration code should be able to work with it too ;)

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: [Raspbian] Dynamically changing IP address for the Raspb

Fri May 22, 2015 11:59 pm

m.fourb wrote:The Raspberry is being connected to my PC via Ethernet (no internet, only this local connection!).
As soon as it gets connected it receives an IP address from my PC under which it should register itself then.
Are you sure that your PC is allocating an IP address to the Pi.
but the Raspberry always stays in the Network with his old 169.254 address
All network addresses in the range 169.254.0.0 through 169.254.255.255 are part of the APIPA - Automatic Private IP Addressing range, which means that the Pi is assigning it's own address because
a) it does not have a static IP address defined by, for example the /etc/network/interfaces file, or
b) it is not receiving an ip address from a dhcp server.

For the Pi to be allocated an ip address from your PC, your PC must be running a dhcp server.
Is your PC running a DHCP server

The dhcp server must also hand out ip addresses in the same range as used by your PC so that they can communicate directly.

Take a look at dnsmasq, which can act as a dhcp server (also a lightweight dns server). Once you have dnsmasq running it can hand out ip addresses and it can hand out the same addresses each time to the same machines based on their network interface's MAC ID (if required). It also creates a file containing the ip addresses it hands out.

Each Pi should have a unique hostname.

Regards

anita2R

ghellquist
Posts: 68
Joined: Thu Aug 02, 2012 8:47 am
Location: Stockholm Sweden

Re: [Raspbian] Dynamically changing IP address for the Raspb

Sat May 23, 2015 8:13 am

Is there any way to achieve this transition from server to client or can I register it as both?

Both at the same time. Run the client as one process, the server as a different process.
The small character & in shell starts a command in the background.
// Gunnar

m.fourb
Posts: 5
Joined: Fri May 22, 2015 4:10 pm

Re: [Raspbian] Dynamically changing IP address for the Raspb

Sat May 23, 2015 10:21 am

First I want to thank you all for your numerous replies, this is one of the few forums in which one can really find help :)

anita2R:
Is your PC running a DHCP server
No it isn't, because i cannot really justifify the usage of such a server in my thesis, because the concept should work independently with any kind of IP assigment. But I will have a look into dnsmasq because I could use it as a server example then. Thank you for that!

Gunnar:
Both at the same time. Run the client as one process, the server as a different process.
The small character & in shell starts a command in the background.
That was my thought too. Yet I wanted to try threading the two then (one thread client and one thread server) because like that i could use the same variables easier for both threads, or do you see any advantages using two scripts?

Greetings

m.fourb

ghellquist
Posts: 68
Joined: Thu Aug 02, 2012 8:47 am
Location: Stockholm Sweden

Re: [Raspbian] Dynamically changing IP address for the Raspb

Sat May 23, 2015 1:07 pm

do you see any advantages using two scripts?
Yes. It is about robustness in a networked system. Remember that in a network, any computer could stop running or disappear from the net at any time. Each system then has to be able to handle that situation.

I would definitely run at least two processes. The client would then be synchronous: send a request, wait for the response. The answer might take several seconds before it arrives (especially if you get a time-out due to network problems) .

It can be done with threads of course, but sharing variables between threads is not quite as naively easy to program with as it might look from the start. In my experience it is better to run separate processes, not sharing variables directly. This will allow the processes to run on different CPU kernels (the Raspberry 2 has four kernels, most modern PC-s has at least 2).

If you run both functions in same thread, you cannot let the client do a simple wait, instead it needs to check for new incoming requests to the master function while waiting for the response to the client question. This means going for an asynchronous client, which is quite a bit more complicated, might not be supported in the libraries you have chosen to use.

Additionally, I would probably let the master process handle each request in a separate child process. Then, if for some reason, the request aborts the process, the master will still run.

If you want to share variables, place them in a controlled store outside of both processes. I would suggest sqlite as one of many possible solutions or why not go for a full database such as mysql. These functions have solutions to sharing variables between threads or processes.

A totally unrelated spoiler here: the computer language Erlang + the OTP libraries has very good solutions to how to handle a distributed system with all its inherent fault situations. It is a quite different "world" though.

// Gunnar

m.fourb
Posts: 5
Joined: Fri May 22, 2015 4:10 pm

Re: [Raspbian] Dynamically changing IP address for the Raspb

Sat May 23, 2015 9:22 pm

@ Gunnar:
Thank you very much for your detailed reply.
I guess i forgot the 7 fallacies of distributed computing myself there ;)

I will try to write the code then seperately and let the server run in the background.
Also i am going to try out the mysql though i never tried such databases myself...
Is there a good tutorial or anything you can recommend me, or is it so easy that any explanation i can find on google should do it?

Thank you and greetings.

m.fourb

Return to “Networking and servers”