RaspberryPi4Bを無線->LANのイーサネットコンバータへ

Wifiで受信したネットワークにLANで接続する機器も自動IP割り振りするような感じ
記載内容は参考サイトを自分用メモにしたのと、最後の箇所は、動作しなかったを解消調べ

![NoName_2021-2-4_13-50-6_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/48015/33bba861-d49a-3991-baa1-bab6fb4b5182.png)

## 1. /etc/network/interfacesの編集

![NoName_2021-2-3_17-5-1_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/48015/db733e09-b3ce-c5e8-38c1-faa5f1930bb7.png)

###1.1.追加内容

```
auto lo
iface lo inet loopback
 
auto eth0
allow-hotplug eth0
iface eth0 inet manual
 
auto wlan1
allow-hotplug wlan1
iface wlan1 inet dhcp
        wpa-conf  /etc/wpa_supplicant/wpa_supplicant.conf
```

###1.2.全体ソース

/etc/network/interfaces


```
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback
 
auto eth0
allow-hotplug eth0
iface eth0 inet manual
 
auto wlan1
allow-hotplug wlan1
iface wlan1 inet dhcp
        wpa-conf  /etc/wpa_supplicant/wpa_supplicant.conf
```

##2./etc/wpa_supplicant/wpa_supplicant.confの編集

Wifi接続情報を保持を複数を記述できるようです

![NoName_2021-2-3_17-7-55_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/48015/058eeb0e-4124-2d66-f8f3-d0ac6fd5a204.png)

###2.1.追加内容

```
network={
	ssid="SSID"
	psk="パスワード"
	key_mgmt=WPA-PSK
	scan_ssid=1
}
```

###2.2.全体ソース

/etc/wpa_supplicant/wpa_supplicant.conf


```
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=JP

network={
	ssid="SSID"
	psk="パスワード"
	key_mgmt=WPA-PSK
	scan_ssid=1
}
```

##3./etc/rc.localでIP forwardの設定

![NoName_2021-2-3_17-13-4_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/48015/67955134-e9ab-193b-381e-316d8544204e.png)

###3.1.追加内容

```
echo 1 > /proc/sys/net/ipv4/ip_forward
```

###3.2.全体ソース

/etc/rc.local

```
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

echo 1 > /proc/sys/net/ipv4/ip_forward

exit 0
```

##4.DHCPリレーエージェントのインストールと設定の編集


###4.1.インストール

```
sudo apt-get install dhcp-helper
```

![NoName_2021-2-3_17-16-58_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/48015/ed252c99-b7ae-c541-218a-afa553206a97.png)

##4.2./etc/default/dhcp-helperでDHCPリレーエージェントの設定を編集

![NoName_2021-2-3_17-17-40_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/48015/0f06a774-a755-1708-ac6d-ab53e43d1ff8.png)

###4.2.1.追加内容

```
DHCPHELPER_OPTS="-b wlan1"
```

###4.2.2.全体ソース

/etc/default/dhcp-helper

``` 
# Option flags used to start dhcp-helper.
#
# You will need at least "-s <DHCP server>" or
# "-b <interface> so that dhcp-helper knows where 
# to relay DHCP requests. 
#
# See "man 8 dhcp-helper" for more details.  

DHCPHELPER_OPTS="-b wlan1"
```

##5.Proxy ARPブリッジのインストールと設定編集

###5.1.インストール

```
sudo apt-get install parprouted
```

![NoName_2021-2-3_17-20-0_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/48015/b637eeec-a670-41bd-6034-e6be887d4080.png)


##5.2./etc/rc.localでProxyでARPブリッジ設定を編集

![NoName_2021-2-3_17-21-29_No-00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/48015/717526d7-afcc-093f-467f-d2c659194213.png)


###5.2.1.追加内容

``` 
ip addr add 192.168.1.203 dev eth0
parprouted wlan1 eth0
```

###5.2.2.全体ソース

/etc/rc.local

```
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

echo 1 > /proc/sys/net/ipv4/ip_forward
ip addr add 192.168.1.203 dev eth0
parprouted wlan1 eth0

exit 0
```

---

参考サイトはここまでで、動作しなかったので、追記した箇所は以下

---

##6.動作しないを解消


###6.1./etc/network/interfacesを編集

####6.1.1.追記内容

```
post-up /sbin/ip addr add $(/sbin/ip addr show wlan1 | perl -wne 'm|^\s+inet (.*)/| && print $1')/32 dev eth0
  post-up /sbin/ifup eth0
  post-up /usr/sbin/parprouted wlan1 eth0
  post-up /etc/init.d/dhcp-helper start
  post-down /usr/bin/killall /usr/sbin/parprouted
  post-down /etc/init.d/dhcp-helper stop
  post-down /sbin/ifdown eth0
```

####6.1.2.全体ソースコード

/etc/network/interfaces


```
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback
 
auto eth0
allow-hotplug eth0
iface eth0 inet manual
 
auto wlan1
allow-hotplug wlan1
iface wlan1 inet dhcp
        wpa-conf  /etc/wpa_supplicant/wpa_supplicant.conf

post-up /sbin/ip addr add $(/sbin/ip addr show wlan1 | perl -wne 'm|^\s+inet (.*)/| && print $1')/32 dev eth0
  post-up /sbin/ifup eth0
  post-up /usr/sbin/parprouted wlan1 eth0
  post-up /etc/init.d/dhcp-helper start
  post-down /usr/bin/killall /usr/sbin/parprouted
  post-down /etc/init.d/dhcp-helper stop
  post-down /sbin/ifdown eth0
```

##7.本体のWifiをOFF

### OFF
``` 
sudo iwconfig wlan0 txpower off
```

### ON
``` 
sudo iwconfig wlan0 txpower auto
```



##参考サイト

Raspberry Pi3を無線LANコンバータにする
http://rufas.manyoldmoon.com/blog/1604

Bridging Wireless & Ethernet networks with a Raspberry Pi
https://www.yergler.net/tag/network/