192.168.0.* > 192.168.0.0 How with command line?
by jmgibson1981 from LinuxQuestions.org on (#5A31G)
I'm trying to write a somewhat universal script for simple router configuration via iptables. I'm attempting to have the script get the information it needs from the `ip link` and `ip addr` commands. I can extract what I need easily.
My problem is how to change the lan interface ip from whatever it is, in my case 192.168.0.1 to 192.168.0.0 to make that entire subnet work in my iptables rules. 192.168.0.1 could be any ip in that subnet. So regardless of what it is I want to change the last field to 0.
I am terrible with regex, my only experience of any kind involves delimiters and such with cut and awk Those just get me individual fields. How can I take any ip in that subnet to end up ending with a 0?
My current script
Code:#!/bin/sh
SQUIDIP=192.168.0.1
SQUIDPORT=3128
LANSUBNET=192.168.0.0/24
# router
for iface in enp0s3 enp0s8 ; do
iptables -t nat -A POSTROUTING ! -d "$LANSUBNET" -o "$iface" -j MASQUERADE
done
route add -net "$LANSUBNET" dev enp0s9
# squid
for port in 443 80 ; do
iptables -t nat -A PREROUTING -s "$SQUIDIP" -p tcp --dport "$port" -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport "$port" -j REDIRECT --to-port "$SQUIDPORT"
done
iptables -t mangle -A PREROUTING -p tcp --dport "$SQUIDPORT" -j DROPMy plan is to wrap this in a for loop, working through the interfaces and setting the rules depending on the interface and it's given ip. I'm trying to get it geared for multiple wan inputs for failover as well.


My problem is how to change the lan interface ip from whatever it is, in my case 192.168.0.1 to 192.168.0.0 to make that entire subnet work in my iptables rules. 192.168.0.1 could be any ip in that subnet. So regardless of what it is I want to change the last field to 0.
I am terrible with regex, my only experience of any kind involves delimiters and such with cut and awk Those just get me individual fields. How can I take any ip in that subnet to end up ending with a 0?
My current script
Code:#!/bin/sh
SQUIDIP=192.168.0.1
SQUIDPORT=3128
LANSUBNET=192.168.0.0/24
# router
for iface in enp0s3 enp0s8 ; do
iptables -t nat -A POSTROUTING ! -d "$LANSUBNET" -o "$iface" -j MASQUERADE
done
route add -net "$LANSUBNET" dev enp0s9
# squid
for port in 443 80 ; do
iptables -t nat -A PREROUTING -s "$SQUIDIP" -p tcp --dport "$port" -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport "$port" -j REDIRECT --to-port "$SQUIDPORT"
done
iptables -t mangle -A PREROUTING -p tcp --dport "$SQUIDPORT" -j DROPMy plan is to wrap this in a for loop, working through the interfaces and setting the rules depending on the interface and it's given ip. I'm trying to get it geared for multiple wan inputs for failover as well.