0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

hostapdとdnsmasqを使いアクセスポイントを立ち上げる

Posted at

細かい記述が面倒なのでShellを作りました。 

受信する側(アクセスポイント)

# iwconfigで出てきた送受信に使用するインタフェース(wlan0など)を設定してください
bash RX_launch_access_point.sh YourSSID YourPassphrase wlan0 192.168.4.1 192.168.4.2 192.168.4.20
  • RX_launch_access_point.sh
#!/bin/bash

# 引数のチェック
if [ $# -ne 6 ]; then
    echo "Usage: bash $0 <SSID> <PASSPHRASE> <INTERFACE> <GATEWAY_IP> <DHCP_RANGE_START> <DHCP_RANGE_END>"
    echo " "
    echo "Example:"
    echo "bash $0 YourSSID YourPassphrase wlan0 192.168.4.1 192.168.4.2 192.168.4.20"
    exit 1
fi

# hostapdとdnsmasqのインストール確認
if ! command -v hostapd &> /dev/null || ! command -v dnsmasq &> /dev/null; then
    echo "hostapd or dnsmasq is not installed. Installing them now..."
    sudo apt-get update
    sudo apt-get install -y hostapd dnsmasq

    # インストール後に再度確認
    if ! command -v hostapd &> /dev/null || ! command -v dnsmasq &> /dev/null; then
        echo "Failed to install hostapd or dnsmasq. Please install them manually."
        exit 1
    fi
else
    echo "hostapd and dnsmasq are installed and ready."
fi

# hostapdのconfファイル参照を設定
sudo sed -i 's/^#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/' /etc/default/hostapd

# 引数の取得
SSID=$1
PASSPHRASE=$2
INTERFACE=$3
GATEWAY_IP=$4
DHCP_RANGE_START=$5
DHCP_RANGE_END=$6

# ネットワークインターフェースの設定リセット
sudo ip addr flush dev $INTERFACE

# ネットワークインターフェースの設定
sudo ip link set $INTERFACE down
sudo ip addr add $GATEWAY_IP/24 dev $INTERFACE
sudo ip link set $INTERFACE up

# hostapd設定ファイルの作成
cat << EOF | sudo tee /etc/hostapd/hostapd.conf
interface=$INTERFACE
driver=nl80211
ssid=$SSID
hw_mode=g
channel=6
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=$PASSPHRASE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOF

# hostapdの設定と起動
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd

# hostapdのステータスチェック
if systemctl is-active --quiet hostapd; then
    echo "hostapd is running."
else
    echo "hostapd failed to start. Please check the configuration."
    exit 1
fi

# dnsmasq設定ファイルの作成
cat << EOF | sudo tee /etc/dnsmasq.conf
interface=$INTERFACE
dhcp-range=$DHCP_RANGE_START,$DHCP_RANGE_END,255.255.255.0,24h
EOF

# dnsmasqの再起動
sudo systemctl restart dnsmasq

# dnsmasqのステータスチェック
if systemctl is-active --quiet dnsmasq; then
    echo "dnsmasq is running."
else
    echo "dnsmasq failed to start. Please check the configuration."
    exit 1
fi

# IPフォワーディングの有効化
sudo sysctl -w net.ipv4.ip_forward=1

# iptablesルールの設定
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o $INTERFACE -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i $INTERFACE -o eth0 -j ACCEPT

echo "Setup completed successfully."
echo "SSID: $SSID"
echo "Passphrase: $PASSPHRASE"
echo "Interface: $INTERFACE"
echo "Gateway IP: $GATEWAY_IP"
echo "DHCP Range: $DHCP_RANGE_START - $DHCP_RANGE_END"

SSIDが表示されているか確認

$ nmcli device wifi list
IN-USE  SSID             MODE      CHAN  RATE        SIGNAL  BARS  SECURITY  
        YourSSID         インフラ  6     54 Mbit/s   100     ▂▄▆█  WPA2

表示されない場合

# 受信する側(アクセスポイント)でデーモン再起動 
$ sudo systemctl restart hostapd
$ sudo systemctl restart dnsmasq

接続する側(映像送信機)

# 受信側で立ち上げたネットワークに接続
jetson@nvidia:~$ bash TX_connect_to_access_point.sh wlan0 YourSSID YourPassphrase
  • TX_connect_to_access_point.sh
#!/bin/bash

# 引数のチェック
if [ $# -ne 3 ]; then
    echo "Usage: bash $0 <WLAN_INTERFACE> <SSID> <PASSWORD>"
    echo " "
    echo "Example:"
    echo "bash $0 wlan0 YourSSID YourPassphrase"
    exit 1
fi

# 引数の取得
WLAN_INTERFACE=$1
SSID=$2
PASSWORD=$3

# hostapdを停止する
sudo systemctl stop hostapd

# 既存の同名SSID接続設定を削除する
for i in {0..12}; do
    sudo nmcli connection delete "$SSID $i" 2>/dev/null
done
sudo nmcli connection delete "$SSID" 2>/dev/null

# インターフェースを起動する
echo "Bringing up the interface $WLAN_INTERFACE..."
sudo ip link set "$WLAN_INTERFACE" up

# WiFiに接続する
echo "Connecting to $SSID..."
sudo nmcli device wifi connect "$SSID" password "$PASSWORD" ifname "$WLAN_INTERFACE"

# 接続結果の確認
if [ $? -eq 0 ]; then
    echo "Successfully connected to $SSID!"
    exit 0
else
    echo "Failed to connect to $SSID."
    nmcli connection show
    exit 1
fi
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?