Advertisement
About80Ninjas

wifi-to-eth-route.sh

May 10th, 2024 (edited)
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Share Wifi with Eth device
  4. #
  5. #
  6. # This script is created to work with Raspbian Stretch
  7. # but it can be used with most of the distributions
  8. # by making few changes.
  9. #
  10. # Make sure you have already installed `dnsmasq`
  11. # Please modify the variables according to your need
  12. # Don't forget to change the name of network interface
  13. # Check them with `ifconfig`
  14.  
  15. #credit: https://github.com/arpitjindal97/raspbian-recipes/tree/master
  16.  
  17. ip_address_and_network_mask_in_CDIR_notation="192.168.2.1/24"
  18. dhcp_range_start="192.168.2.2"
  19. dhcp_range_end="192.168.2.100"
  20. dhcp_time="12h"
  21. dns_server="1.1.1.1"
  22. eth="eth0"
  23. wlan="wlan0"
  24.  
  25. sudo systemctl start network-online.target &> /dev/null
  26.  
  27. sudo iptables -F
  28. sudo iptables -t nat -F
  29. sudo iptables -t nat -A POSTROUTING -o $wlan -j MASQUERADE
  30. sudo iptables -A FORWARD -i $wlan -o $eth -m state --state RELATED,ESTABLISHED -j ACCEPT
  31. sudo iptables -A FORWARD -i $eth -o $wlan -j ACCEPT
  32.  
  33. sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
  34.  
  35. sudo ip link set $eth down
  36. sudo ip link set $eth up
  37. sudo ip addr add  $ip_address_and_network_mask_in_CDIR_notation dev $eth
  38.  
  39. # Remove default route created by dhcpcd
  40. sudo ip route del 0/0 dev $eth &> /dev/null
  41.  
  42. sudo systemctl stop dnsmasq
  43.  
  44. sudo rm -rf /etc/dnsmasq.d/* &> /dev/null
  45.  
  46. echo -e "interface=$eth
  47. bind-interfaces
  48. server=$dns_server
  49. domain-needed
  50. bogus-priv
  51. dhcp-range=$dhcp_range_start,$dhcp_range_end,$dhcp_time" > /tmp/custom-dnsmasq.conf
  52.  
  53. sudo cp /tmp/custom-dnsmasq.conf /etc/dnsmasq.d/custom-dnsmasq.conf
  54. sudo systemctl start dnsmasq
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement