LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Wireless Networking (https://www.linuxquestions.org/questions/linux-wireless-networking-41/)
-   -   Init script for ndiswrapper with WPA support (https://www.linuxquestions.org/questions/linux-wireless-networking-41/init-script-for-ndiswrapper-with-wpa-support-574760/)

David1357 08-04-2007 07:26 PM

Init script for ndiswrapper with WPA support
 
Dear Folks,

I created an init script for Fedora Core 4 that should work on all Red Hat distros. It loads the ndiswrapper driver, initializes the wlan interface, and starts wpa_supplicant. You will need to put the script in

/etc/rc.d/init.d

and run

# chkconfig --add wlan
# chkconfig --level 345 wlan on

to get it to start at boot. It will start right after network is started and will stop right before network is stopped. You can control the wlan script using the usual Red Hat syntax:

# service wlan start
# service wlan stop
# service wlan restart

You may need to massage the script to handle any idiosyncrasies of your setup (e.g. change wlan0 to wlan1). E-mail me if you have an questions.

Code:

#!/bin/bash
#
# wlan                This shell script takes care of starting and stopping
#                the wlan interface.
#
# Copyright: (C) 2007 David Cullen  dcullen@kerneldriver.org
#
# License: GPLv3
#
# chkconfig: - 11 89
# description: wlan starts the wlan interface.

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

RETVAL=0
INTERFACE="wlan0"
LOGFILE="/var/log/${INTERFACE}.log"

wlanup()
{
        echo "Loading ndiswrapper..." > ${LOGFILE}
        if ! modprobe ndiswrapper >> ${LOGFILE} 2>&1 ; then
                echo "Could not load ndiswrapper" >> ${LOGFILE}
                return 1;
        fi
        echo "Done." >> ${LOGFILE}
        echo "Configuring ${INTERFACE}..." >> ${LOGFILE}
        if ! ifconfig ${INTERFACE} 192.168.1.251 netmask 255.255.255.0 broadcast 192.168.1.255 >> ${LOGFILE} 2>&1 ; then
                echo "Could not configure ${INTERFACE}" >> ${LOGFILE}
                return 1;
        fi
        echo "Done." >> ${LOGFILE}
        echo "Bringing up ${INTERFACE}..." >> ${LOGFILE}
        if ! ifconfig wlan0 up >> ${LOGFILE} 2>&1 ; then
                echo "Could not bring up ${INTERFACE}" >> ${LOGFILE}
                return 1;
        fi
        echo "Done." >> ${LOGFILE}
        echo "Sleeping for one second..." >> ${LOGFILE}
        if ! sleep 1 >> ${LOGFILE} 2>&1 ; then
                echo "Could not sleep" >> ${LOGFILE}
                return 1;
        fi
        echo "Done." >> ${LOGFILE}
        echo "Configuring WPA..." >> ${LOGFILE}
        if ! /usr/local/sbin/wpa_supplicant -Dwext -i${INTERFACE} -c/etc/wpa_supplicant.conf -B 2>&1 >> ${LOGFILE} ; then
                echo "Could not configure WPA" >> ${LOGFILE}
                return 1;
        fi
        echo "Done." >> ${LOGFILE}
        echo "Configuring default route..." >> ${LOGFILE}
        if ! route add -net default gw 192.168.1.254 dev ${INTERFACE} >> ${LOGFILE} 2>&1 ; then
                echo "Could not configure default route" >> ${LOGFILE}
                return 1;
        fi
        echo "Done." >> ${LOGFILE}
        return $RETVAL
}

wlandown()
{
        echo "Stopping WPA..." >> ${LOGFILE}
        if ! killproc wpa_supplicant >> ${LOGFILE} 2>&1 ; then
                echo "Could not stop WPA" >> ${LOGFILE}
                return 1;
        fi
        echo "Bringing down ${INTERFACE}..." >> ${LOGFILE}
        if ! ifconfig ${INTERFACE} down >> ${LOGFILE} 2>&1 ; then
                echo "Could not bring down ${INTERFACE}" >> ${LOGFILE}
                return 1;
        fi
        echo "Done." >> ${LOGFILE}
        echo "Unloading ndiswrapper..." >> ${LOGFILE}
        if ! rmmod ndiswrapper >> ${LOGFILE} 2>&1 ; then
                echo "Could not unload ndiswrapper" >> ${LOGFILE}
                return 1;
        fi
        echo "Done." >> ${LOGFILE}
        return $RETVAL
}

start()
{
        echo -n $"Bringing up ${INTERFACE}: "
        wlanup
        RETVAL=$?
        [ $RETVAL -eq 0 ] && echo_success || echo_failure
        echo
        return $RETVAL
}

stop()
{
        echo -n $"Bringing down ${INTERFACE}: "
        wlandown
        RETVAL=$?
        [ $RETVAL -eq 0 ] && echo_success || echo_failure
        echo
        return $RETVAL
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        stop
        start
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload}"
        exit 1
esac

exit $RETVAL



All times are GMT -5. The time now is 08:42 PM.