找回密码
 注册

QQ登录

只需一步,快速开始

写了一个简单的WiFi认证脚本,送给大家!

#!/bin/bash
## quick and dirty AP with hostapd and dnsmasq
## exit properly with ctrl-c

echo "退出程序请狂按 Ctrl+C 并清除程序/tmp"

if [ -z $1 ]; then
   echo -n "请输入认证AP(不支持中文): "
   read ssid
else
   ssid=$1
fi

# get wep key
function get_wep_key() { 
    echo -n "WEP Key [must be exactly 5 or 13 ascii characters]: " 
    read wep_key
    if [[  $wep_key =~ ^[a-zA-Z0-9]{5}$ ]] ; then
       echo "Key accepted"
   elif [[  $wep_key =~ ^[a-zA-Z0-9]{13}$ ]] ; then
        echo "Key accepted"
    else
        echo "WEP key must be exactly 5 or 13 characters"
        get_wep_key
    fi
}

# get mac
function get_mac() {
    echo -n "Enter MAC address in the following format AB:CD:EF:12:34:56: "
    read new_mac
    if [[  $new_mac =~ ^[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}:[a-fA-F0-9]{2}$ ]] ; then
        macchanger --mac=$new_mac wlan0
   else
        echo "MAC Address format not correct."
        get_mac
    fi
}

# ask for WEP
echo -n "是否要加密此AP[y/n]: "
read wep
case $wep in
    y*)
        get_wep_key
    ;;
    *)
    ;;
esac

# ask for MAC change
echo -n "是否要改变 MAC地址 [y/n]: "
read changemac
case $changemac in
    y*)
        echo -n "Custom MAC? [y/n]: "
      read random_mac
        case $random_mac in
            y*)
                get_mac
            ;;
            n*)
                macchanger -r wlan0
            ;;
            *)
                echo "Invalid choice, keeping current MAC address."
            ;;
        esac
    ;;
   n*)
    ;;
esac

# 安装必要模块
if [ $(dpkg-query -W -f='${Status}' dnsmasq 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  apt-get install dnsmasq
fi
if [ $(dpkg-query -W -f='${Status}' hostapd 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
  apt-get install hostapd
fi

# trap control c
trap ctrl_c INT

function ctrl_c() {
   echo "wlan0 managed mode"
   iwconfig wlan0 mode managed
   echo "downing wlan0"
   echo "flushing firewall"
   iptables -F
   iptables -F -t nat
   echo "resetting wlan0 mac"
   macchanger -p wlan0
   kill -9 `cat /tmp/dnsmasq.run`
}


## script begins

# stop and disable services
service hostapd stop
service dnsmasq stop
pkill -9 dnsmasq
pkill -9 hostapd

# bring up wlan0

iwconfig wlan0 mode monitor
ifconfig wlan0 10.0.0.1/24 up

#dhcp
iptables --policy INPUT ACCEPT
iptables --policy FORWARD ACCEPT
iptables --policy OUTPUT ACCEPT
iptables -F
iptables -t nat -F
iptables -t nat -A PREROUTING -i wlan0 -p udp --dport 53 -j DNAT --to 10.0.0.1

# dns masq conf
cat > /tmp/dnsmasq.conf <<!
bind-interfaces
interface=wlan0
address=/#/10.0.0.1
dhcp-range=10.0.0.2,10.0.0.254
dhcp-option=6,10.0.0.1 #DNS
dhcp-option=3,10.0.0.1 #Gateway
dhcp-option=252,"http://wpad.example.com/wpad.dat\n" #WPAD
dhcp-authoritative
!

# hostapd conf
cat > /tmp/hostapd.conf<<!
interface=wlan0
driver=nl80211
ssid=${ssid}
hw_mode=g
channel=6
!

# if WEP key, add to hostapd conf
if [[ -n $wep_key ]]; then echo -e "wep_default_key=0\nwep_key0=\"${wep_key}\"" >> /tmp/hostapd.conf; fi

# run dnsmasq and hostapd
dnsmasq --pid-file=/tmp/dnsmasq.run -C /tmp/dnsmasq.conf
hostapd /tmp/hostapd.conf


我知道答案 本帖寻求最佳答案回答被采纳后将获得系统奖励10 金币 , 目前已有1人回答

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享至 : QQ空间
收藏
谁有敬业福啊!

1 个回复

倒序浏览
libo 来自手机 vip用户 2020-2-21 21:14:13
2#
闲了试一下!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 注册