-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-dhcp-tftp-dns-routing.sh
More file actions
executable file
·163 lines (132 loc) · 4.41 KB
/
Copy pathsetup-dhcp-tftp-dns-routing.sh
File metadata and controls
executable file
·163 lines (132 loc) · 4.41 KB
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
161
162
163
#!/bin/bash
set -e
# Configuration
PXE_INTERFACE="enx00e04c6808b6"
PXE_IP="192.168.201.1"
PXE_SUBNET="192.168.201.0/24"
DHCP_RANGE="192.168.201.100,192.168.201.200"
TFTP_ROOT="/srv/tftp"
LEASE_TIME="24h"
# Check if running as root
if [[ $EUID -ne 0 ]]; then
echo "Error: This script must be run as root"
exit 1
fi
# Check if netboot files exist
if [[ ! -d "$TFTP_ROOT/debian-installer" ]]; then
echo "Error: Debian netboot files not found in $TFTP_ROOT"
echo "Please extract netboot.tar.gz to $TFTP_ROOT first"
exit 1
fi
# Check if interface exists
if ! ip link show "$PXE_INTERFACE" &> /dev/null; then
echo "Error: Interface $PXE_INTERFACE not found"
exit 1
fi
# Check if interface already has an IP
if ip addr show "$PXE_INTERFACE" | grep -q "inet "; then
echo "Error: Interface $PXE_INTERFACE already has an IP address configured"
exit 1
fi
# Check for IP conflicts on the network
if ping -c 1 -W 1 "$PXE_IP" &> /dev/null; then
echo "Error: IP address $PXE_IP is already in use on the network"
exit 1
fi
# Detect upstream interface from default route
UPSTREAM_INTERFACE=$(ip route | grep '^default' | awk '{print $5}' | head -n1)
if [[ -z "$UPSTREAM_INTERFACE" ]]; then
echo "Error: Could not detect upstream interface from default route"
exit 1
fi
echo "Detected upstream interface: $UPSTREAM_INTERFACE"
# Configure the PXE interface
echo "Configuring interface $PXE_INTERFACE with IP $PXE_IP..."
ip link set "$PXE_INTERFACE" up
ip addr add "$PXE_IP/24" dev "$PXE_INTERFACE"
# Wait for interface to be fully configured
sleep 2
# Verify IP is set
if ! ip addr show "$PXE_INTERFACE" | grep -q "$PXE_IP"; then
echo "Error: Failed to configure IP address on $PXE_INTERFACE"
exit 1
fi
# Enable IP forwarding
echo "Enabling IP forwarding..."
echo 1 > /proc/sys/net/ipv4/ip_forward
# Set up iptables masquerading
echo "Setting up iptables NAT/masquerading..."
iptables -t nat -A POSTROUTING -s "$PXE_SUBNET" -o "$UPSTREAM_INTERFACE" -j MASQUERADE
iptables -A FORWARD -i "$PXE_INTERFACE" -o "$UPSTREAM_INTERFACE" -j ACCEPT
iptables -A FORWARD -i "$UPSTREAM_INTERFACE" -o "$PXE_INTERFACE" -m state --state RELATED,ESTABLISHED -j ACCEPT
# Create dnsmasq configuration
DNSMASQ_CONF="/tmp/dnsmasq-pxe.conf"
cat > "$DNSMASQ_CONF" << EOF
# Bind only to PXE interface
interface=$PXE_INTERFACE
bind-interfaces
# DHCP configuration
dhcp-range=$DHCP_RANGE,$LEASE_TIME
dhcp-option=3,$PXE_IP
dhcp-option=6,$PXE_IP
# TFTP configuration
enable-tftp
tftp-root=$TFTP_ROOT
# PXE boot configuration
dhcp-boot=pxelinux.0
# Don't read /etc/hosts
no-hosts
# Log queries for debugging
log-queries
log-dhcp
EOF
# Extract nameservers from /etc/resolv.conf and add to config
if [[ -f /etc/resolv.conf ]]; then
while read -r line; do
if [[ $line =~ ^nameserver[[:space:]]+([0-9.]+) ]]; then
echo "server=${BASH_REMATCH[1]}" >> "$DNSMASQ_CONF"
fi
done < /etc/resolv.conf
fi
# If no nameservers found, add fallback
if ! grep -q "^server=" "$DNSMASQ_CONF"; then
echo "server=8.8.8.8" >> "$DNSMASQ_CONF"
echo "server=8.8.4.4" >> "$DNSMASQ_CONF"
fi
# Start dnsmasq
echo "Starting dnsmasq..."
dnsmasq -C "$DNSMASQ_CONF" --no-daemon --log-facility=/var/log/dnsmasq-pxe.log &
DNSMASQ_PID=$!
# Give dnsmasq a moment to start
sleep 1
echo ""
echo "PXE boot server started successfully!"
echo "Interface: $PXE_INTERFACE ($PXE_IP)"
echo "DHCP range: $DHCP_RANGE"
echo "Upstream: $UPSTREAM_INTERFACE"
echo "dnsmasq PID: $DNSMASQ_PID"
echo "Log file: /var/log/dnsmasq-pxe.log"
echo ""
echo "Press Ctrl+C to stop the server and clean up..."
# Cleanup function
cleanup() {
echo ""
echo "Shutting down PXE server..."
# Kill dnsmasq
if [[ -n "$DNSMASQ_PID" ]]; then
kill "$DNSMASQ_PID" 2>/dev/null || true
fi
# Remove iptables rules
iptables -t nat -D POSTROUTING -s "$PXE_SUBNET" -o "$UPSTREAM_INTERFACE" -j MASQUERADE 2>/dev/null || true
iptables -D FORWARD -i "$PXE_INTERFACE" -o "$UPSTREAM_INTERFACE" -j ACCEPT 2>/dev/null || true
iptables -D FORWARD -i "$UPSTREAM_INTERFACE" -o "$PXE_INTERFACE" -m state --state RELATED,ESTABLISHED -j ACCEPT 2>/dev/null || true
# Remove IP from interface
ip addr del "$PXE_IP/24" dev "$PXE_INTERFACE" 2>/dev/null || true
ip link set "$PXE_INTERFACE" down 2>/dev/null || true
# Remove temp config
rm -f "$DNSMASQ_CONF"
echo "Cleanup complete"
}
trap cleanup EXIT INT TERM
# Wait forever
wait "$DNSMASQ_PID"