-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb-thetering-to-ethernet
160 lines (124 loc) · 4.16 KB
/
usb-thetering-to-ethernet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
trap '' 2
while true
do
clear
echo "====================================================================================="
echo "// Traffic routing from USB Thetering to Ethernet made by @squadramunter //"
echo "====================================================================================="
echo "Enter 1 to setup the routing automatically: "
echo "Enter 2 to test the network connectivity: "
echo "Enter q to exit the menu q: "
echo -e "\n"
echo -e "Enter your selection \c"
read answer
case "$answer" in
1)
if eth=`basename -a /sys/class/net/* | grep eth`; then
echo "Adapter "$eth" is alive and working properly..."
fi
if ens=`basename -a /sys/class/net/* | grep ens`; then
echo "Adapter "$ens" is alive and working properly..."
fi
if enp=`basename -a /sys/class/net/* | grep enp`; then
echo "Adapter "$enp" is alive and working properly..."
fi
if usb=`basename -a /sys/class/net/* | grep usb`; then
echo "Adapter "$usb" is alive and working properly..."
fi
sleep 10
ethernet=`basename -a /sys/class/net/* | grep 'eth\|ens\|enp'`
usb=`basename -a /sys/class/net/* | grep 'usb'`
apt-get update && apt-get upgrade -y && apt-get install git -y
touch /etc/network/interfaces.d/{$usb,$ethernet}
CETH="/etc/network/interfaces.d/$ethernet"
/bin/cat <<EOM >$CETH
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# source /etc/network/interfaces.d/*
# The Ethernet network interface
allow-hotplug $ethernet
iface $ethernet inet static
address 172.24.1.1
netmask 255.255.255.0
network 172.24.1.0
broadcast 172.24.1.255
EOM
CUSB="/etc/network/interfaces.d/$usb"
/bin/cat <<EOM >$CUSB
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# source /etc/network/interfaces.d/*
# The Wireless network interface
allow-hotplug $usb
iface $usb inet dhcp
EOM
ip addr flush $ethernet
systemctl restart networking.service
ifdown $ethernet
ifup $ethernet
apt-get install dnsmasq -y
mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
touch /etc/dnsmasq.d/$ethernet
DNSMASQ="/etc/dnsmasq.d/$ethernet"
/bin/cat <<EOM >$DNSMASQ
interface=$ethernet
listen-address=172.24.1.1 # Explicitly specify the address to listen on
bind-interfaces # Bind to the interface to make sure we aren't sending things elsewhere
server=1.1.1.1 # Forward DNS requests to Cloudflare DNS
domain-needed # Don't forward short names
bogus-priv # Never forward addresses in the non-routed address spaces.
dhcp-range=172.24.1.50,172.24.1.150,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time
EOM
## Allow IP Forwarding
sed -i '/#net.ipv4.ip_forward=1/c\net.ipv4.ip_forward=1' /etc/sysctl.conf
sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
## Set IPTABLES for network routing
iptables -t nat -A POSTROUTING -o $usb -j MASQUERADE
iptables -A FORWARD -i $usb -o $ethernet -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $ethernet -o $usb -j ACCEPT
## Install IPTABLES-Persistent to load IPTABLES on reboot
apt-get install iptables-persistent -y
## Save the IPTABLES Permanent
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6
## Enable DNSMasq and restart
systemctl enable dnsmasq
systemctl restart dnsmasq
## service network-manager restart
ifdown $usb
ifup $usb
ifdown $ethernet
ifup $ethernet
echo "About to reboot your device!"
read -p "Would you like to proceed y/n? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
reboot
else
echo "Please reboot your system!"
fi
;;
2)
if ping -q -c 1 -W 1 172.24.1.1 >/dev/null; then
echo "IPv4 is up and network routing is enabled!"
else
echo "IPv4 is down and network routing is disabled!"
fi
if ping -q -c 1 -W 1 google.com >/dev/null; then
echo "Yes! Network connection is working as pretended!"
else
echo "Oops I can't establish a connection!"
fi
;;
q) exit ;;
esac
echo -e "Enter return to continue \c"
read input
done