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 102013
 

So a few people have tried to follow Part 3 of this series of posts, and had issues with the connections once everything is setup.
The main cause for that, was that hostapd would take away the IP address of wlan0 for some reason.
I worked around it by modifying the /etc/init.d/hostapd startup file.

In that file, there is a switch case statement which controls what the file does depending on how you run the file.

The example below is what you will see part way down the file. What we need to do is add in some commands into that block of code.

case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start-stop-daemon --start --oknodo --quiet --exec "$DAEMON_SBIN" \
--pidfile "$PIDFILE" -- $DAEMON_OPTS >/dev/null
log_end_msg "$?"
;;
stop)

The command we will need to add are :
ifup wlan0

Which brings up wlan0 with the specified IP address. The IP address should have been setup in Part 3 of this series.

So the above block of code will now become –

case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start-stop-daemon --start --oknodo --quiet --exec "$DAEMON_SBIN" \
--pidfile "$PIDFILE" -- $DAEMON_OPTS >/dev/null
log_end_msg "$?"
ifup wlan0
;;
stop)

This will force wlan0 to come up after hostapd is started.

Alternatively to force wlan0 to use an ip.address, replace ifup wlan0 with this
ifconfig wlan0 10.0.0.1 netmask 255.255.255.0 up
Hopefully this helps everyone when they come to making their Pi a WiFi Router ! πŸ˜€

Share
Jan 102013
 

A while ago, I was having an issue with one of my computer’s WiFi connection not connecting correctly on startup.
Maybe the signal was too weak on startup or the WiFi adapter just wasn’t fast enough when the computer wanted it to connect, for some reason it would never connect and I would have to manually run an ifup command to make it connect.

I ended up making this script to check whether the WiFi had an IP address, and if it didn’t it would take down the WiFi adapter and bring it back up again.
Now, I never need to worry about that computer’s WiFi connection !
I was also going to use this for one of my Raspberry Pi projects but never got around to putting the script onto the Pi, so anyone who has the issue where hostapd breaks the IP address, this script is perfect for it !


#!/bin/bash

wlan=`/sbin/ifconfig wlan0 | grep inet\ addr | wc -l`
if [ $wlan -eq 0 ]; then
/sbin/ifdown wlan0 && /sbin/ifup wlan0
else
echo interface is up
fi

I’ve setup this script to run as a cronjob every 5 minutes to make sure the WiFi stays up.

Share
Aug 302012
 

So I’ve gotten this crazy idea into my head that I can write a script that will detect whether you have a USB WiFi dongle plugged in, whether you have the right drivers installed, and if you do, then it will ask you for WiFi details and attempt to connect. Or if you don’t have the right drivers / firmware etc, it will detect what hardware you have and install the right devices.

Now, in order to do that, I need to do some data gathering. The information that I need ?
Is this –
If you have a USB wifi adapter plugged into your Raspberry Pi, run these commands, and post the output into the comments.

Run lsusb first, to determine the Bus and Device that the USB Wifi adapter is on, and then run lsusb with the -v and -s arguments to display just the information relevant to the WiFi Adapter.

lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 006: ID 0c45:627b Microdia PC Camera (SN9C201 + OV7660)
Bus 004 Device 002: ID 0846:6a00 NetGear, Inc. WG111v2 54 Mbps Wireless [RealTek RTL8187L]
lsusb -v -s 004:002

If I can get enough information, then I may be able to write the script !

Thanks !

Share
Aug 112012
 

QuickLinks
Part 1 – How to create a wireless network on your RPi
Part 2 – How to make your RPi into a Wireless Access Point
Part 3A – Issues with HostAPD ? Click here
Part 3B – Issues with HostAPD ? Click here!

In Part 3, we will turn the Raspberry Pi into a router. The Pi will use the ethernet port as the “WAN” port, and the wireless adapter as the “LAN” side of things.
First thing we need to do is to enable packet forwarding.
In the file /etc/sysctl.conf, we need to uncomment the following line (should be line 28).

#net.ipv4.ip_forward=1

After changing that, run this command to re-read the sysctl.conf file

sysctl -p

We will also need to install the iptables utilities if they are not already installed, and alter the dnsmasq.conf file so that dnsmasq will assign a gateway to the computers.

Firstly we install iptables.

apt-get install iptables

Then we will add this line to dnsmasq.conf.
The IP address is the same one we gave to the WiFi adapter back in Part 1

dhcp-option=3,10.0.0.1

We will need to create an iptables script to tell the Raspberry Pi to forward packets from the WiFi interface to the LAN interface. This script will need to run at startup in order for it to be a router.
I have created the following file in /etc/network/if-up.d/ called router. By placing the script in that directory, it will be run every time the Raspberry Pi comes online.
This file contains the following lines

iptables -F
iptables -X

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -i wlan0 -j ACCEPT
iptables -A OUTPUT -o wlan0 -j ACCEPT

iptables -A POSTROUTING -t nat -o eth0 -j MASQUERADE
iptables -A FORWARD -i wlan0 -j ACCEPT

After creating this file, make it executable by using this command

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

Once that router.sh file has been run, it should start forwarding traffic from the WiFi Interface to the LAN interface !

** UPDATE **
As a few people have found out, the IP address of the Pi does not get set correctly until you take down wlan0 and bring it back up after hostapd starts.
I have not found out why yet, but the workaround I am currently using consists of editing /etc/init.d/hostapd, and modifying the start case statement to include ifdown wlan0 && ifup wlan0 at the end of it.
I failed to copy the statement I am talking about, but if you’re familiar with scripting it shouldn’t be too hard to figure out. Otherwise I’ll get the code I modified up here soon.

** UPDATE 2 **
Looks like a Pi user has successfully gotten hostapd running on the realtek devices.
Info can be found here


If you have found this post useful, please consider donating by pressing on the button below. Donating will help keep this site up and running πŸ™‚




Share