LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux From Scratch (https://www.linuxquestions.org/questions/linux-from-scratch-13/)
-   -   Quick and Easy wpa_supplicant / dhcpcd Script (https://www.linuxquestions.org/questions/linux-from-scratch-13/quick-and-easy-wpa_supplicant-dhcpcd-script-903722/)

blumbri 09-18-2011 04:59 PM

Quick and Easy wpa_supplicant / dhcpcd Script
 
Hey folks,

I'm relatively new to LFS but have had to rebuild it three times due to experimenting and fine-tuning. One of the first things I do after building the base is to get my wireless internet working. As neither LFS nor BLFS have a built-in bootscript for connecting my wlan0 interface with both 'dhcpcd' and 'wpa_supplicant', I thought I'd write my own. I've seen a couple other scripts posted on the BLFS wiki and in the hints, but they didn't seem simple enough. Anyways, this is my third time writing this script as I always forget to back it up prior to a reformat.

I am posting this to keep it off my computer so I can always get it again. If it helps anyone else out...all the better.

*** Ensure you have 'dhcpcd' and 'wpa_supplicant' installed and configured prior to continuing ***

First here is my 'ifconfig.wlan0' config file. I am using (at the time of writing) the development version SVN-20110918 so I have this file in the '/etc/sysconfig' directory. If you're running the stable version 6.8, place it in the '/etc/sysconfig/network-devices' directory instead:
Code:

# Begin /etc/sysconfig/ifconfig.wlan0

ONBOOT=yes
SERVICE=wpa-dhcpcd
IFACE=wlan0
DRIVER=wext
WPA_CONF="/etc/wpa_supplicant.conf"
DHCP_START=""
DHCP_STOP="-k"

# Set PRINTIP="yes" to have the script print
# the DHCP assigned IP address
PRINTIP="yes"

# Set PRINTALL="yes" to print the DHCP assigned values for
# IP, SM, DG, and 1st NS. This requires PRINTIP="yes".
PRINTALL="yes"

# End /etc/sysconfig/ifconfig.wlan0


And last, 'wpa-dhcpcd' goes in the '/lib/services' directory (if running on the development branch). If you're using 6.8, instead place this file in the '/etc/sysconfig/network-devices/services' directory:
Code:

#!/bin/sh
# Begin /lib/services/wpa-dhcpcd

# Based upon lfs-bootscripts-1.12 $network_devices/if{down,up}
# Rewritten by Nathan Coulson <nathan NOatSPAM linuxfromscratch NOdotSPAM org>
# Adapted for dhcpcd by DJ Lucas <dj NOatSPAM lucasit NOdotSPAM com>
# Adapted for wpa_supplicant by Brian Blumberg <blumbri NOatSPAM gmail NOdotSPAM com>

#$LastChangedBy: blumbri $
#$Date: 2011-09-18 11:16:21 +0000 (Sun, 18 Sep 2011) $

. /etc/sysconfig/rc
. $rc_functions
. $IFCONFIG

PIDFILE="/var/run/dhcpcd-$1.pid"
LEASEINFO="/var/lib/dhcpcd/dhcpcd-$1.info"
WPACTRL="/var/run/wpa_supplicant/$1"

case "$2" in
        up)
                boot_mesg -n "Starting wpa_supplicant on the $1 interface..."
                if [ -r "$WPACTRL" ]
                then
                        boot_mesg "wpa_supplicant already running!" ${WARNING}
                        echo_warning
                        exit 2
                fi
                /sbin/wpa_supplicant -i $IFACE -B -D $DRIVER -c "$WPA_CONF"
                # Save the return value
                RET="$?"
                echo ""
                $(exit "$RET")
                evaluate_retval

                boot_mesg -n "Starting dhcpcd on the $1 interface..."
                # Test to see if there is a stale pid file
                if [ -f "$PIDFILE" ]
                then
                        ps `cat "$PIDFILE"` | grep dhcpcd > /dev/null
                        if [ $? != 0 ]
                        then
                                rm -f $PIDFILE > /dev/null
                        else
                                boot_mesg "dhcpcd already running!" ${WARNING}
                                echo_warning
                                exit 2
                        fi
                fi
                /sbin/dhcpcd $1 $DHCP_START
                # Save the return value
                RET="$?"
                # Print the assigned settings if requested
                if [ "$RET" = "0" -a "$PRINTIP" = "yes" ]; then
                        . $LEASEINFO
                        if [ "$PRINTALL" = "yes" ]; then
                                echo ""
                                echo_ok
                                boot_mesg "          DHCP Assigned Settings for $1:"
                                boot_mesg_flush
                                boot_mesg "          IP Address:      $IPADDR"
                                boot_mesg_flush
                                boot_mesg "          Subnet Mask:    $NETMASK"
                                boot_mesg_flush
                                boot_mesg "          Default Gateway: $GATEWAYS"
                                boot_mesg_flush
                                boot_mesg "          DNS Server:      $DNSSERVERS"
                                boot_mesg_flush
                        else
                                boot_mesg " IP Addresss: $IPADDR"
                                echo_ok
                        fi
                else
                        echo ""
                        $(exit "$RET")
                        evaluate_retval
                fi
        ;;

        down)
                boot_mesg -n "Stopping dhcpcd on the $1 interface..."
                if [ -z "$DHCP_STOP" ]
                then
                        echo ""
                        killproc -p "$PIDFILE" /sbin/dhcpcd
                else
                        /sbin/dhcpcd $1 $DHCP_STOP &> /dev/null
                        RET="$?"
                        if [ "$RET" -eq 0 ]; then
                                echo ""
                                echo_ok
                        elif [ "$RET" -eq 1 ]; then
                                boot_mesg "dhcpcd not running!" ${WARNING}
                                echo_warning
                        else
                                echo ""
                                echo_failure
                        fi
                fi

                boot_mesg -n "Stopping wpa_supplicant on the $1 interface..."
                WPAPID=$(echo $(ps x | grep /sbin/wpa_supplicant | grep $IFACE) | cut -d' ' -f1)
                if [ "x$WPAPID" != "x" ]
                then
                        kill $WPAPID &> /dev/null
                        RET="$?"
                        if [ "$RET" -eq 0 ]; then
                                echo ""
                                echo_ok
                        else
                                echo ""
                                echo_failure
                        fi
                else
                        boot_mesg "wpa_supplicant not running!" ${WARNING}
                        echo_warning
                fi
        ;;

        *)
                echo "Usage: $0 [interface] {up|down}"
                exit 1
        ;;
esac

# End /lib/services/wpa-dhcpcd

After doing this, upon boot (or running '/etc/rc.d/init.d/network start') your interface should be brought up, wpa_supplicant started, and dhcpcd loaded. Hopefully this helps someone!

Please let me know if you have any questions/comments.

-- blumbri

poly_s 10-19-2011 12:37 PM

Brilliant!!

ZRD


All times are GMT -5. The time now is 09:30 PM.