myjess
Posts: 9
Joined: Sat Sep 29, 2012 7:15 pm

Playing with network settings.

Wed Oct 03, 2012 10:29 pm

Self healing network config?

I want to setup static IP in wheezy on wlan0.

But, if my playing about with the network settings screws up, and i am working remotely, i want a daemon to constantly ping my router and if it doesn't get a reply after 5 minutes, i want the new settings copied to new files, and the old good files put back in their place. I then want the pi to either reboot or restart networking including wlan, so I can get back into it remotely again.
The copying of files has to be aware of open file locks, kill the process that has these files open (say in nano) so the files can be overwritten with the old good versions.

Anyone do this yet?

Thanks and please send script if you have.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Playing with network settings.

Wed Oct 03, 2012 10:59 pm

Maybe I'm wrong but I don't believe it's possible to assign a static IP address (non-DHCP) to a wireless network it's certain not a feature supported by my LINKSYS router.

If you're playing around with network settings with your router why the need to reboot the PI any event of lost wifi connectivity should be restored when the signal returns.

Richard S.

ski522
Posts: 394
Joined: Sun Sep 30, 2012 2:22 pm

Re: Playing with network settings.

Wed Oct 03, 2012 11:43 pm

Not really clear what you're trying to do, but you can set static IP's for wlan0. Best to probably just write a shell script that runs via a cronjob every 5 minutes to check your IP.

Setting static IP from a shell prompt would be done: ifconfig wlan0 [ip address/cidr] up
Example:
ifconfig wlan0 192.168.1.5/24 up

myjess
Posts: 9
Joined: Sat Sep 29, 2012 7:15 pm

Re: Playing with network settings.

Thu Oct 04, 2012 7:21 am

i want a fallback mode that falls back itself.
I know it's going to be a script, I just don't want to have to write it myself :)
Thanks.

User avatar
penguintutor
Posts: 376
Joined: Tue May 08, 2012 9:11 am
Location: UK
Contact: Website

Re: Playing with network settings.

Thu Oct 04, 2012 10:01 am

Here is a different approach that can be used to restore access if things go wrong.
This is something I've done in the past when configuring firewall rules on remote firewalls (up to 3 hours drive away from where I was doing the configuration).

You can use the at command to schedule a job in the future. You will need to install it first using

Code: Select all

sudo apt-get install at 

If you set that job to undo your changes at a future time, then if everything goes OK you can cancel the job - if not then it will restore your access.

Code: Select all

echo "cp /etc/network/interfaces.old /etc/network/interfaces; reboot" | at 10:35
Note that you need to run this using a root shell. So use sudo -s before entering that command rather than on the same command line.

Code: Select all

$ sudo -s
# echo "cp /etc/network/interfaces.old /etc/network/interfaces; reboot" | at 10:35
* assuming you have already created the interfaces.old file.

You can enter the time as a relative time as well eg. now +1hour (man at - for more details)

If your changes are successful you can use
at -l to view at commands and get the job number
and add -r <job number> to cancel it assuming you haven't kicked yourself out.

If you break your config - just wait until the set time and your machine will restore the old config and reboot.

This will work OK from the command line if making dynamic changes, but if you are making changes that need you to reboot to test your configuration you should enter the command into /etc/rc.local using a relative time

Although be careful if you do add it into /etc/rc.local as you could end up with a constant reboot cycle.

User avatar
penguintutor
Posts: 376
Joined: Tue May 08, 2012 9:11 am
Location: UK
Contact: Website

Re: Playing with network settings.

Thu Oct 04, 2012 10:18 am

Maybe I'm wrong but I don't believe it's possible to assign a static IP address (non-DHCP) to a wireless network it's certain not a feature supported by my LINKSYS router.
You can use a static IP address by coding in on the Pi, which is what myjess is trying to do. You don't need your router to support anything - other than to change the range of the dhcp leases so it doesn't try and allocate the static IP address to a DHCP client (or just use a high IP address for the static as it's unlikely the dhcp server will get above 100.
The copying of files has to be aware of open file locks, kill the process that has these files open (say in nano) so the files can be overwritten with the old good versions.
Nano doesn't lock any files - if you try to overwrite a file whilst nano is editing then you can do. If nano is killed cleanly then it will create a temporary .save file which can be used for recovering the file - but if issued a force kill it will not. The shutdown command (run by reboot) will issue the SIGHUP to nano for it to create it's temporary .save file.

myjess
Posts: 9
Joined: Sat Sep 29, 2012 7:15 pm

Re: Playing with network settings.

Thu Oct 04, 2012 11:37 am

Thanks for all your help.

It's defo nice knowing about nano.

Thanks you also for the code snippets. I find these really useful and clear to understand.

broo0ose
Posts: 317
Joined: Wed Dec 14, 2011 3:59 pm
Location: Wirral, UK

Re: Playing with network settings.

Thu Oct 04, 2012 8:37 pm

Can't you just assign a static DHCP address on your router, that's what I have done for my Wired and WiFi cards. You just need to find the MAC address using "ifconfig -a" and then copy that into the DHCP settings in your router. Then whenever your device asks for an IP address it gets the same one.

User avatar
penguintutor
Posts: 376
Joined: Tue May 08, 2012 9:11 am
Location: UK
Contact: Website

Re: Playing with network settings.

Thu Oct 04, 2012 9:18 pm

assign a static DHCP address on your router
That's another way of doing it. On my router it's called DHCP Reservation (you can even see the list of connected devices so you don't have to copy the MAC addresses over), but I don't use it much.

There are pros and cons about whether you configure it on the router or on the computer, although not much in it.

Maybe it's because I'm a server guy*, but it feels more satisfying manually configuring the Pi rather than "cheating" by editing the router, but the technique I showed can be useful for other situations where you think there is a risk that you may prevent you accessing the computer remotely when making a change; in my case it was more about getting firewall rules wrong and hence killing my own access to the firewall (these were manual text based firewall rules I had to create).

* the irony being that I used to be a server sysadmin working for a networking company :-)

Return to “Beginners”