2
\$\begingroup\$

This script creates an adhoc network using hostapd. I have tested it and seems to work reliably. I am new to linux networking and not sure if this is a recommended way to create an adhoc network this way.

#!/bin/bash
#set -x
DEVICE=wlan0
CONFIG_FILE=./hostapd.conf
ConfigureDevice()
{
 if ! sudo iwconfig 2>/dev/null | grep $DEVICE >/dev/null;then
 echo $DEVICE not found!
 exit -1
 fi
 #Ensure config file exists
 if [ ! -e $CONFIG_FILE ]
 then
 echo "Can't find hostapd config file"
 exit -1
 fi
 sudo service isc-dhcp-server stop
 sudo pkill hostapd
 sudo ifdown $DEVICE 
 sudo ifconfig $DEVICE up 192.168.1.100 netmask 255.255.255.0
 sudo service isc-dhcp-server start
 sudo hostapd -B $CONFIG_FILE
}
cd ${0%/*} #make current working directory the directory of the bash script
ConfigureDevice
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Oct 9, 2013 at 13:09
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Not answering your actual question, but offering a couple of code comments:

use cd -- "$(dirname -- "0ドル")"

  • if the script is in the PATH and someone just enters the script name, you will try to cd to the (probably non-existant) "script name" directory in the current dir.

use grep -q $DEVICE

  • that is a bit speedier since, if it finds a match, it exits immediately instead of having to read the entire input looking for all matches.
answered Oct 10, 2013 at 10:43
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.