I used this python script in order to bridge wifi to tehernet but it just crashes in 1 minute
from subprocess import check_call, check_output, Popen, PIPE, run
from sys import argv, exit
from threading import Thread
from time import sleep
def get_ip(interface_name):
return check_output(['ip', '-4', '-br', 'addr', 'show', interface_name]).decode('utf-8').strip().split()[-1]
def pre_start(ip, ethernet_interface, wireless_interface):
try:
check_call(['ip', 'addr', 'add', ip, 'dev', ethernet_interface])
check_call(['ip', 'link', 'set', 'dev', ethernet_interface, 'up'])
check_call(['ip', 'link', 'set', wireless_interface, 'promisc', 'on'])
except:
print('pre_start error')
# In case of error, try to clean up
pre_stop(ip, ethernet_interface, wireless_interface)
exit(1)
def pre_stop(ip, ethernet_interface, wireless_interface):
run(['ip', 'link', 'set', wireless_interface, 'promisc', 'off'], check=False)
run(['ip', 'link', 'set', 'dev', ethernet_interface, 'down'], check=False)
run(['ip', 'addr', 'del', ip, 'dev', ethernet_interface], check=False)
def start(ethernet_interface, wireless_interface):
with Popen(['parprouted', '-d', ethernet_interface, wireless_interface], stdout=PIPE, stderr=PIPE, bufsize=1, universal_newlines=True) as process:
for line in iter(process.stdout.readline, ''):
print(line, end='')
for line in iter(process.stderr.readline, ''):
print(line, end='')
if name == 'main':
is_up = False
ip = None
ethernet_interface = argv[1]
wireless_interface = argv[2]
task = None
try:
while True:
new_ip = get_ip(wireless_interface)
if is_up:
if task and not task.is_alive():
pre_stop(ip, ethernet_interface, wireless_interface)
print('process stopped unexpectedly', task)
exit(1)
if ip and not new_ip:
pre_stop(ip, ethernet_interface, wireless_interface)
print('ip disappeared')
exit(1)
ip = new_ip
if ip and not is_up:
print('starting...')
pre_start(ip, ethernet_interface, wireless_interface)
task = Thread(target=start, args=(ethernet_interface, wireless_interface,))
task.start()
is_up = True
if not ip and is_up:
print('stopping...')
pre_stop(ip, ethernet_interface, wireless_interface)
task.join()
exit(0)
sleep(1)
except KeyboardInterrupt:
pre_stop(ip, ethernet_interface, wireless_interface)
exit(0)
More information:https://www.willhaley.com/blog/raspberry-pi-wifi-ethernet-bridge/
The goal is to connect a non-WiFi computer to a WiFi network using a Raspberry Pi. We will use a Raspberry Pi 4 Model B as a bridge between the non-WiFi computer and the WiFi network. The Raspberry Pi connects to WiFi and shares its connection with other computers over Ethernet. These instructions were only tested and verified using: A fresh ins...