LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-08-2008, 04:45 PM   #1
Adanos
LQ Newbie
 
Registered: Nov 2008
Posts: 6

Rep: Reputation: 0
Trying to calculate Network Address with shell script


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

Last edited by Adanos; 11-08-2008 at 04:49 PM.
 
Old 11-08-2008, 04:50 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,398

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
do you not just want to be using the ipcalc shell tool to do all this for you?
 
Old 11-08-2008, 04:57 PM   #3
Adanos
LQ Newbie
 
Registered: Nov 2008
Posts: 6

Original Poster
Rep: Reputation: 0
i'll do some resurge on that tool, thats properly what i need.
thanks for your help acid_kewpie.
 
Old 11-08-2008, 05:10 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,150

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Doesn't the obase=2 give you binary? Your output certainly looks like binary values . . .

<edit>Oops! Sorry, I read too fast. </edit>

Last edited by PTrenholme; 11-09-2008 at 04:32 PM. Reason: Appology.
 
Old 11-09-2008, 04:07 AM   #5
Adanos
LQ Newbie
 
Registered: Nov 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PTrenholme View Post
Doesn't the obase=2 give you binary? Your output certainly looks like binary values . . .
yes obase=2 gives binary. You can use obase=8 and obase16 aswell for oct and hex.
 
Old 11-09-2008, 10:37 AM   #6
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,563

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by Adanos View Post
The fist problem im having is that the output isn't displaying as 8 digits. ex. 00001010 insted of 1010
You can make your own function for displaying binary numbers,
for example, using bash:
Code:
function echo_bin()
{
  for ((mask=128; mask>0; mask>>=1)); do
    echo -n $((($1 & $mask) ? 1: 0))
  done
}

myIP="10.0.0.3"

TMP1=$(echo $myIP | cut -d. -f1)
TMP2=$(echo $myIP | cut -d. -f2)
TMP3=$(echo $myIP | cut -d. -f3)
TMP4=$(echo $myIP | cut -d. -f4)

echo "IP in dec: $myIP"
echo -n "IP in bin: "
echo -n $(echo_bin $TMP1).$(echo_bin $TMP2).$(echo_bin $TMP3).$(echo_bin $TMP4)
echo
 
  


Reply

Tags
find, network, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 12:57 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 11:40 AM
Calculate in shell script Ephracis Linux - General 1 12-16-2004 11:11 AM
Shell script ip address format check. rooch84 Linux - Software 6 08-18-2004 09:14 AM
Shell script for insert ip address into an ordered list of IPs inTrouble? Linux - Newbie 2 10-27-2003 02:21 AM


All times are GMT -5. The time now is 11:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration