vrijdag 27 september 2013

A soft reset button for a raspberri pi

scenario

i have a raspberry pi that i want to be able to reboot cleanly without ssh-ing into it


sollution

we're going to use a GPIO pin with a button wired up like this

picture from adafruit's website
3.3 volt is pin 1 (the pin nearest to the port where you plug in the power addaptorGND  is pin 6 (2 down and one over from pin 1)
and i'll be using gpi 23 .. that's pin 16 (nr 8 on the same line as pin 6)

My buton is actually a different setup from the on in this picture (mine breaks contacet when pressed



next we make a script
$ sudo nano /etc/softresetbutton.py




#!/usr/bin/env python
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
while True:
        if ( GPIO.input(23)== True ):
             # tue or false here depends on your button type
             # when in doubt replace 'reboot &' in the following line with 'echo "pushed" &'
                os.system('reboot &')
        sleep(0.1);



next we make this script executable
$sudo chmod +x /etc/softresetbutton.py

and test-run it
$sudo /etc/softresetbutton.py

push the button to test it
works ok ??
now we need to make it run at startup

$sudo nano /etc/rc.local

and add the line
/etc/softresetbutton.py &
just above the " exit 0 "


reboot and you'll have a soft-rest button

if you would prefer a "clean halt" button .. replace the reboot command in the script with  halt

woensdag 25 september 2013

Stupid but sometimes usefull IP-tables trick

OK so you want to secure your off-site server using IP tables .. this is always a good thing to do


on of the things that i highly recommend is to restrict the incomming IP's to  whatever port you are using for management so that you can only access it from your networks

but if you start messing with the iptables for port 22 (for example) and you make a tiny tiny slip-up you might lose connection yourself. if the machine is sitting next to you (under your desk or in your on-site server-rack, yu can walk over hook up a screen and keyboard and fix it .... but if it's in a datacenter somewhere... yu've got a problem. (with a physical machine all you can do is get an engineer to help you out (expensive) or hard reboot the machine (not nice))

the trick is .. let's say we're starting at 2 in the afternoon, and we think we might have 15 minutes of work.
so we'll shedule a restore f the current situation in 20 minutes
as root (obviously) do

$iptables-save > /home/user/iptables.conf
$at 14:20
at>iptables-restore < /home/user/iptables.conf
   ctrl-D to save the job and quit

now check your job is scheduled
$atq
1       Wed Sep 25 14:20:00 2013 a root


start messing with your iptables
if something goes wrong .. wait for the scheduled restore

if nothing goes wrong (congrats)
you can remve the job with
$atrm 1