Jan 302013
 

I’ve gotten some coding done on the Pi’s WiFi configuration portal.

This is a quick snapshot of what it looks like so far.
Does anyone have any feedback or comments ?

WifiPortal

Share
Jan 282013
 

I’ve been doing some coding for my Raspberry Pi’s Wireless Network Configuration page, and was trying to find a good way to get just a single number out of the block that ifconfig outputs –
I wanted to get the number after RX packets: in this example.

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2383877 errors:0 dropped:8221 overruns:0 frame:0
TX packets:2492088 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:734455593 (700.4 MiB) TX bytes:3075500605 (2.8 GiB)

I ended up writing a oneliner to do it for me, and put it into a function so I could use it more.
$strWlan0 is the block of code above.

substr($strWlan0,strpos($strWlan0,"RX packets:")+11,strpos(substr($strWlan0,strpos($strWlan0,"RX packets:")+11)," "));

If anyone has a better way of doing it though, please add it in the comments.

Share
Jan 272013
 

I recently got my Raspberry Pi to act as a WiFi Router with shaping by using hostapd, tc, and iptables together to broadcast as an Access Point, apply shaping rules, and apply packet marking respectively.

This can be done on any device with 2 NICs but this may come in useful for anyone using the Pi as a router but doesn’t want people using all the bandwidth.

I will be going through the iptables and tc configuration in this post, but I won’t go through the hostapd setup as that was the same as part 3 of my Raspi WiFi Access point guide.

I started off with a blank slate of Raspbian Server Edition again, and I didn’t need to install any utilities as iptables and tc are installed by default.
**Note** All command are run as root. If you are not running as root, prepend sudo to all commands

Setting up tc

tc is the program that is in charge of setting up the shaping rules.

Step 1
Firstly, we will setup the default rule for the interface, which is wlan0 in this instance.

These 2 commands sets the default policy on wlan0 to shape everyone’s download speed to 64 kilobytes a second.

tc qdisc add dev wlan0 root handle 1:0 htb default 10
tc class add dev wlan0 parent 1:0 classid 1:10 htb rate 64kbps ceil 64kbps prio 0

Step 2
Next, we’ll setup another class to shape certain addresses to a higher speed.
We also need to setup a filter so that any packets marked as such go through this rule

tc class add dev wlan0 parent 1:1 classid 1:5 htb rate 256kbps ceil 256kbps prio 1
tc filter add dev wlan0 parent 1:0 prio 1 handle 5 fw flowid 1:5

Once that class is setup, we’ll need to setup iptables to mark the specific packets we want to shape as such.

Setting up iptables

Step 1
Firstly, we’ll create the mangle table that we need. I’ve used a custom chain in the mangle table in this snippet
The below code creates the new chains of shaper-in and shaper-out, and then sets up some rules for any packets coming in and out of wlan0 and eth0 to go through the new chains.

iptables -t mangle -N shaper-out
iptables -t mangle -N shaper-in

iptables -t mangle -I POSTROUTING -o wlan0 -j shaper-in
iptables -t mangle -I PREROUTING -i wlan0 -j shaper-out
iptables -t mangle -I PREROUTING -i eth0 -j shaper-in
iptables -t mangle -I POSTROUTING -o eth0 -j shaper-out

Step 2
Once that is done, we can then setup the packet marking so that any packets from the 10.0.0.0/24 subnet gets marked with a 1, otherwise if the IP address is 10.0.0.5, they will get marked with a 5

iptables -t mangle -A shaper-out -s 10.0.0.0/24 -j MARK --set-mark 1
iptables -t mangle -A shaper-in -d 10.0.0.0/24 -j MARK --set-mark 1

iptables -t mangle -A shaper-out -s 10.0.0.5 -j MARK --set-mark 5
iptables -t mangle -A shaper-in -d 10.0.0.5 -j MARK --set-mark 5

With that done, any connections going through wlan0 should now be shaped to 64kbps unless you have the IP address of 10.0.0.5 in which case you will be shaped to 256kbps

If more shaping speeds or IP addresses are required, then step 2 in the respective sections will need to be modified / added onto to give you the extra options.
I used Speedtest Mini hosted on my own webserver to test the speeds while I was shaping the connections as it provided an easy to use interface, but wget on a big file can also be used to test the speeds.

Setting up router to auto-start

If you want the shaping to start automatically, put all the commands into 1 or 2 shell script files in /etc/network/if-up.d and they will be run automatically.
e.g. /etc/network/if-up.d/router and /etc/network/if-up.d/shaper
make sure they have both been chmodded to be executable by running this on both files

chmod +x /etc/network/if-up.d/router


Sources :
tc: Linux HTTP Outgoing Traffic Shaping (Port 80 Traffic Shaping)

Share
Jan 222013
 

Hey Everyone

I’ve had issues with my WiFi in the past, and with my Pi running headless it’s sometimes a pain to get it connected back up to WiFi, so I’ve created this little script to start hostapd and dnsmasq whenever the WiFi connection went down, which allowed me to SSH into the Pi even though it wasn’t connected to the network, because it was broadcasting it’s own network !

This can be customised to do different things if either WiFi or ethernet go down.


#!/bin/bash
#
# Interface checker
# Checks to see whether interface has an IP address, if it doesn't assume it's down and start hostapd
# Author : SirLagz
#
Interface='wlan0'
HostAPDIP='10.0.0.1'
echo "-----------------------------------"
echo "Checking connectivity of $Interface"
NetworkUp=`/sbin/ifconfig $Interface`
IP=`echo "$NetworkUp" | grep inet | wc -l`
if [[ $IP -eq 0 ]]; then
echo "Connection is down"
hostapd=`pidof hostapd`
if [[ -z $hostapd ]]; then
# If there are any more actions required when the interface goes down, add them here
echo "Attempting to start hostapd"
/etc/init.d/hostapd start
echo "Attempting to start dnsmasq"
/etc/init.d/dnsmasq start
echo "Setting IP Address for wlan0"
/sbin/ifconfig wlan0 $HostAPDIP netmask 255.255.255.0 up
fi
elif [[ $IP -eq 1 && $NetworkUp =~ $HostAPDIP ]]; then
echo "IP is $HostAPDIP - hostapd is running"
else
echo "Connection is up"
hostapd=`pidof hostapd`
if [[ ! -z $hostapd ]]; then
echo "Attempting to stop hostapd"
/etc/init.d/hostapd stop
echo "Attempting to stop dnsmasq"
/etc/init.d/dnsmasq stop
echo "Renewing IP Address for $Interface"
/sbin/dhclient wlan0
fi
fi
echo "-----------------------------------"

Share
Jan 202013
 

I have been playing around with Motion on one of my Linux boxes, and got annoyed at the constant stream of emails that I am receiving as I have setup Motion to send an email to myself when it detects motion, so I wrote up a short script to activate or deactivate motion if it can pick up certain devices by pinging them.

It’s not foolproof as my wifi is playing up a bit and some devices do not respond by ping sometimes, but it’s useful for deactivating something when you approach and reactivating it when you leave, or vice versa depending on your use of this script.
Setting this script up as a cronjob will allow it to run every say 5 minutes for automation.


#!/bin/bash
PeopleAround=0
people=( 192.168.2.72 192.168.2.117 )
MotionStarted=`wget -o getlog -O /usr/scripts/status http://localhost:8080/0/detection/status | cat /usr/scripts/status | sed -e 's/<[^>]*>//g' | tail -n 3 | head -n -2`

for ip in "${people[@]}"; do
PersonAround=`ping -c 1 $ip | grep '64 bytes' | wc -l`
if [[ PersonAround -eq 1 ]]; then
PeopleAround=1
break
fi
done

if [[ $PeopleAround -eq 1 ]]; then
if [[ "$MotionStarted" =~ "ACTIVE" ]]; then
wget -O /usr/scripts/status http://127.0.0.1:8080/0/detection/pause
echo "People around, stopping motion" $run | mail -s 'Motion stopped' YOUR@EMAIL.COM
fi
else
if [[ "$MotionStarted" =~ "PAUSE" ]]; then
wget -O /usr/scripts/status http://127.0.0.1:8080/0/detection/start
echo "No One around, Starting motion" $run | mail -s 'Motion started' YOUR@EMAIL.com
fi
fi

Share