Hi
I'm trying to make an automated firewall script that gets all the info
like ip, netmask, network address from config files.
I got it all working exept calculating the Network Address.
#! /bin/sh
LAN_IF="eth0"
LAN_IF_IP=`ifconfig $LAN_IF | grep inet | cut -d : -f 2 | cut -d ' ' -f 1`
LAN_NETMASK=`ifconfig $LAN_IF | grep Mask | cut -d : -f 4 | cut -d ' ' -f 1`
TMP1=`echo $LAN_IF_IP | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\1/'`
TMP2=`echo $LAN_IF_IP | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\2/'`
TMP3=`echo $LAN_IF_IP | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\3/'`
TMP4=`echo $LAN_IF_IP | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\4/'`
# IP ADD as bin
TEMP1=`echo -e "obase=2; ${TMP1}" | bc | tr -d "\n" `
TEMP2=`echo -e "obase=2; ${TMP2}" | bc | tr -d "\n" `
TEMP3=`echo -e "obase=2; ${TMP3}" | bc | tr -d "\n" `
TEMP4=`echo -e "obase=2; ${TMP4}" | bc | tr -d "\n" `
TEST_NETWORK_ADD="$TEMP1 $TEMP2 $TEMP3 $TEMP4"
TMP1=`echo $LAN_NETMASK | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\1/'`
TMP2=`echo $LAN_NETMASK | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\2/'`
TMP3=`echo $LAN_NETMASK | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\3/'`
TMP4=`echo $LAN_NETMASK | sed 's/\(.*\)\.\(.*\)\.\(.*\)\.\(.*\)/\4/'`
# Subnet as bin
TEMP1=`echo -e "obase=2; ${TMP1}" | bc | tr -d "\n" `
TEMP2=`echo -e "obase=2; ${TMP2}" | bc | tr -d "\n" `
TEMP3=`echo -e "obase=2; ${TMP3}" | bc | tr -d "\n" `
TEMP4=`echo -e "obase=2; ${TMP4}" | bc | tr -d "\n" `
TEST_NETMASK="$TEMP1 $TEMP2 $TEMP3 $TEMP4"
echo "Test: IP ADDRESS $LAN_IF_IP"
echo "Test: NETMASK $LAN_NETMASK"
echo "Test: IP ADDRESS as bin: $TEST_NETWORK_ADD"
echo "Test: NETMASK as bin: $TEST_NETMASK"
Output:
Test: IP ADDRESS 10.0.0.2
Test: NETMASK 255.255.255.224
Test: IP ADDRESS as bin: 1010 0 0 10
Test: NETMASK as bin: 11111111 11111111 11111111 11100000
The fist problem im having is that the output isn't displaying as 8 digits. ex. 00001010 insted of 1010
The 2nd problem is that im not sure how to do the and'ing of the netmaks and ip address to find the network address.
Is there a better way of doing this calculation ?
any help is appreciated.
I hope the code above makes sense, im not very good at this
regards,
Adanos