OPENWRT 设置AP STA并连接WIFI 脚本

openwrt环境:网络接口为自己精简的,只留有lo和wlan0,没有bridge、eth、wan。


删除以太网相关配置

1. /etc/config/dhcpwan删除

config dhcp wan

option interface wan

option ignore 1

 

 2. /etc/config/firewallwan删除

config zone

option name wan

list   network 'wan'

list   network 'wan6'

option input REJECT

option output ACCEPT

option forward REJECT

option masq 1

option mtu_fix 1

 

config forwarding

option src lan

option dest wan

 

# We need to accept udp packets on port 68,

# see https://dev.openwrt.org/ticket/4108

config rule

option name Allow-DHCP-Renew

option src wan

option proto udp

option dest_port 68

option target ACCEPT

option family ipv4

# Allow DHCPv6 replies

# see https://dev.openwrt.org/ticket/10381

config rule

option name Allow-DHCPv6

option src wan

option proto udp

option src_ip fe80::/10

option src_port 547

option dest_ip fe80::/10

option dest_port 546

option family ipv6

option target ACCEPT

 

# Allow essential incoming IPv6 ICMP traffic

config rule

option name Allow-ICMPv6-Input

option src wan

option proto icmp

list icmp_type echo-request

list icmp_type echo-reply

list icmp_type destination-unreachable

list icmp_type packet-too-big

list icmp_type time-exceeded

list icmp_type bad-header

list icmp_type unknown-header-type

list icmp_type router-solicitation

list icmp_type neighbour-solicitation

list icmp_type router-advertisement

list icmp_type neighbour-advertisement

option limit 1000/sec

option family ipv6

option target ACCEPT

 

# Allow essential forwarded IPv6 ICMP traffic

config rule

option name Allow-ICMPv6-Forward

option src wan

option dest *

option proto icmp

list icmp_type echo-request

list icmp_type echo-reply

list icmp_type destination-unreachable

list icmp_type packet-too-big

list icmp_type time-exceeded

list icmp_type bad-header

list icmp_type unknown-header-type

option limit 1000/sec

option family ipv6

option target ACCEPT

 

将config rule下的wan改lan

 

3. /etc/config/network

修改为

config interface 'loopback'

option ifname 'lo'

option proto 'static'

option ipaddr '127.0.0.1'

option netmask '255.0.0.0'

 

config globals 'globals'

option ula_prefix 'fd79:734a:0a4e::/48'

 

config interface 'lan'

option force_link '1'

option proto 'static'

option ipaddr '192.168.10.1'

option netmask '255.255.255.0'

option ip6assign '60'

option _orig_ifname 'wlan0'




STA.sh


#!/bin/sh


ENCRYPTION="psk2"
SSID="test"
KEY="12345678"
CHANNEL="11"

_wifi_sta_set_firewall(){
    echo > /etc/config/firewall
    uci add firewall defaults 1>/dev/null
    uci set firewall.@defaults[0]=defaults
    uci set firewall.@defaults[0].syn_flood=1
    uci set firewall.@defaults[0].input=ACCEPT
    uci set firewall.@defaults[0].output=ACCEPT
    uci set firewall.@defaults[0].forward=REJECT
    uci add firewall zone 1>/dev/null
    uci set firewall.@zone[0]=zone
    uci set firewall.@zone[0].name=lan
    uci set firewall.@zone[0].network=lan
    uci set firewall.@zone[0].input=ACCEPT
    uci set firewall.@zone[0].output=ACCEPT
    uci set firewall.@zone[0].forward=ACCEPT
    uci add firewall zone 1>/dev/null
    uci set firewall.@zone[1]=zone
    uci set firewall.@zone[1].name=wan
    uci add_list firewall.@zone[1].network=wan0
    uci add_list firewall.@zone[1].network=wan1
#uci add_list firewall.@zone[1].network=wan6
    uci set firewall.@zone[1].input=ACCEPT
    uci set firewall.@zone[1].output=ACCEPT
    uci set firewall.@zone[1].forward=ACCEPT
    uci set firewall.@zone[1].masq=1
    uci set firewall.@zone[1].mtu_fix=1
    uci add firewall forwarding 1>/dev/null
    uci set firewall.@forwarding[0]=forwarding
    uci set firewall.@forwarding[0].src=lan
    uci set firewall.@forwarding[0].dest=wan
    uci add firewall rule 1>/dev/null
    uci set firewall.@rule[0]=rule
    uci set firewall.@rule[0].name=Allow-DHCP-Renew
    uci set firewall.@rule[0].src=wan
    uci set firewall.@rule[0].proto=udp
    uci set firewall.@rule[0].dest_port=68
    uci set firewall.@rule[0].target=ACCEPT
    uci set firewall.@rule[0].family=ipv4
    uci add firewall rule 1>/dev/null
    uci set firewall.@rule[1]=rule
    uci set firewall.@rule[1].name=Allow-Ping
    uci set firewall.@rule[1].src=wan
    uci set firewall.@rule[1].proto=icmp
    uci set firewall.@rule[1].icmp_type=echo-request
    uci set firewall.@rule[1].family=ipv4
    uci set firewall.@rule[1].target=ACCEPT
    uci add firewall rule 1>/dev/null
    uci set firewall.@rule[2]=rule
    uci set firewall.@rule[2].name=Allow-DHCPv6
    uci set firewall.@rule[2].src=wan
    uci set firewall.@rule[2].proto=udp
    uci set firewall.@rule[2].src_ip=fe80::/10
    uci set firewall.@rule[2].src_port=547
    uci set firewall.@rule[2].dest_ip=fe80::/10
    uci set firewall.@rule[2].dest_port=546
    uci set firewall.@rule[2].family=ipv6
    uci set firewall.@rule[2].target=ACCEPT
    echo OK
    uci add firewall include 1>/dev/null
    uci set firewall.@include[0]=include
    uci set firewall.@include[0].path='/etc/firewall.user'
    uci commit
}

_wifi_sta_set_network(){
    echo > /etc/config/network
    uci set network.loopback=interface
    uci set network.loopback.ifname=lo
    uci set network.loopback.proto=static
    uci set network.loopback.ipaddr=127.0.0.1
    uci set network.loopback.netmask=255.0.0.0
    uci set network.lan=interface
    uci set network.lan.ifname=wlan0
    uci set network.lan.proto=dhcp
    uci commit
}

_wifi_sta_set_dhcp(){
    uci delete dhcp.wan 2>/dev/null
    uci set dhcp.lan=dhcp
    uci set dhcp.lan.interface=lan
    uci set dhcp.lan.ignore=1
    uci set dhcp.odhcpd=odhcpd
    uci set dhcp.odhcpd.maindhcp=0
    uci set dhcp.odhcpd.leasefile=/tmp/hosts/odhcpd
    uci set dhcp.odhcpd.leasetrigger=/usr/sbin/odhcpd-update
    uci commit
}

#param: <ssid> <encrymode> <key> <channel>
wifi_connect_to(){
    echo > /etc/config/wireless
    uci set wireless.radio0=wifi-device
    uci set wireless.radio0.type=mac80211
    uci set wireless.radio0.channel=$4
    uci set wireless.radio0.hwmode=11n
    uci set wireless.radio0.path=10180000.wmac
    uci set wireless.radio0.htmode=HT40
    if ! uci get wireless.@wifi-iface[0] 1>/dev/null 2>/dev/null
    then
        uci add wireless wifi-iface 1>/dev/null 2>/dev/null
    fi
    uci set wireless.@wifi-iface[0]=wifi-iface
    uci set wireless.@wifi-iface[0].device=radio0
    uci set wireless.@wifi-iface[0].network=lan
    uci set wireless.@wifi-iface[0].mode=sta
    uci set wireless.@wifi-iface[0].ssid=$1
    uci set wireless.@wifi-iface[0].encryption=$2
    uci set wireless.@wifi-iface[0].key=$3
    uci commit
}

#_wifi_sta_set_firewall
_wifi_sta_set_network
_wifi_sta_set_dhcp
wifi_connect_to $SSID $ENCRYPTION $KEY $CHANNEL
#/etc/init.d/firewall enable
#/etc/init.d/firewall restart

/etc/init.d/network restart



恢复默认设置(即/rom的内容覆盖到/下)

recovery.sh

#!/bin/sh

mount_root
echo y | firstboot
reboot -f


你可能感兴趣的:(OPENWRT 设置AP STA并连接WIFI 脚本)