要是先运行这个做ad-hoc的脚本再运行hostapd就可以很快获取ip并且能上网。
这是哪里出了问题?
代码: 全选
echo "Starting Ad-hoc..."
check_dnsmasq
sleep 1
ifconfig $WInterface down
echo -n "Setting $WInterface gateway and netmask."
ifconfig $WInterface $Igateway netmask $Inetmask
sleep 1
echo -n " [ good ]"
echo
echo -n "Setting up $WInterface ..."
ifconfig $WInterface up
sleep 1
echo -n " [ good ]"
echo
echo -n "Setting $WInterface mode..."
iwconfig $WInterface mode $Wmode
sleep 1
echo -n " [ good ]"
echo
echo -n "Setting $WInterface essid..."
iwconfig $WInterface essid $Wessid
sleep 1
echo -n " [ good ]"
echo
echo -n "Setting $WInterface cannel..."
iwconfig $WInterface channel $Wchannel
sleep 1
echo -n " [ good ]"
echo
echo -n "Setting $WInterface password..."
iwconfig $WInterface key s:$WPasskey
sleep 1
echo -n " [ good ]"
echo
echo -n "Setting $WInterface key type..."
iwconfig $WInterface key $Wkey
sleep 1
echo -n " [ good ]"
echo
echo
echo -n "Setting iptable..."
sleep 1
#remove the old rules
iptables -N wireless-adhoc
iptables -F wireless-adhoc
iptables -t nat -F PREROUTING
iptables -t nat -F POSTROUTING
iptables -t nat -F
#bring up the NAT rules
iptables -A wireless-adhoc -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A wireless-adhoc -s $Inetwork/24 -j ACCEPT
iptables -A wireless-adhoc -p 47 -j ACCEPT
iptables -A wireless-adhoc -j DROP
iptables -A FORWARD -m state --state INVALID -j DROP
wireless-adhoc
iptables -t nat -I POSTROUTING -s $Inetwork/24 -j MASQUERADE
sleep 1
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
echo -n "."
sleep 1
dnsmasq -i $WInterface --resolv-file=$resolvFile --conf-file=$dnsmasqFile
echo
echo "Success!^^"
}
# stop Ad-hoc
function adhoc_stop
{
echo "Stopping adhoc ..."
sh -c "echo 0 > /proc/sys/net/ipv4/ip_forward"
echo -n "."
# stop adhoc mode
ifconfig $WInterface mode managed
sleep 1
echo -n "."
ifconfig $WInterface key off
sleep 1
echo -n "."
ifconfig $WInterface essid any
sleep 1
echo -n "."
ifconfig $WInterface down
sleep 1
echo -n "."
# remove iptabled rules
iptables -D FORWARD -j wireless-adhoc
iptables -D FORWARD -m state --state INVALID -j DROP
iptables -F wireless-adhoc
iptables -X wireless-adhoc
iptables -t nat -F PREROUTING
iptables -t nat -F POSTROUTING
iptables -t nat -F
sleep 1
if [ -f $dnsmasqPid ]; then
dnsmasqID=`cat $dnsmasqPid`
kill $dnsmasqID
sleep 1
fi
if [ -f $dnsmasqLeases ]; then
rm $dnsmasqLeases
fi
echo
echo "Wifi ad-hoc now stopped"
}
function adhoc_restart
{
echo "Now, resart ad-hoc ..."
adhoc_stop
sleep 2
adhoc_start
}
# check dnsmasq.conf
function check_dnsmasq
{
if [ -f $dnsmasqPid ]; then
echo "Dhcp is running!"
echo "Now, restart Ad-hoc"
adhoc_stop
sleep 1
fi
if [ ! -d $adhocFolder ]; then
mkdir $adhocFolder
fi
if [ ! -f $dnsmasqFile ]; then
echo "$dnsmasqFile is not exist, now building."
echo "dhcp-authoritative" > $dnsmasqFile
echo "dhcp-range=$DhcpRangeMin,$DhcpRangeMax,12h" >> $dnsmasqFile
echo "dhcp-leasefile=$dnsmasqLeases" >> $dnsmasqFile
echo "pid-file=$dnsmasqPid" >> $dnsmasqFile
echo "user=root" >> $dnsmasqFile
echo "no-negcache" >> $dnsmasqFile
fi
}
# is super user?
function super_user
{
if [ "$UID" = "0" ]; then
return 0
else
return 1
fi
}
function usage
{
echo "Wifi Ad-hoc control"
echo "$1 [-h][-s]"
echo "Default is start ad-hoc"
echo "-h show the usage"
echo "-s stop ad-hoc"
echo "-r restart ad-hoc"
}
if ! super_user ; then
echo "Need super user permission!"
exit 1
fi
if [ $# -lt 1 ]; then
adhoc_start
elif [ "$1" = "-h" ]; then
usage
elif [ "$1" = "-s" ]; then
adhoc_stop
elif [ "$1" = "-r" ]; then
adhoc_restart
else
echo "Input error!"
echo "$1 -h gives usage information."