LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-22-2003, 11:37 PM   #1
amitbhawnani
LQ Newbie
 
Registered: Jul 2003
Posts: 5

Rep: Reputation: 0
Help with Shell Script !


Hi,
I hope you'll guys can help me with this one
I need a function to convert an IP Number to IP Address
eg
IP Number = A x (256*256*256) + B x (256*256) + C x 256 + D

I have been reading a couple of shell scripting tutorials, but have not been able to figure it out...
 
Old 07-23-2003, 12:34 AM   #2
arax_luminarium
LQ Newbie
 
Registered: Jul 2003
Posts: 16

Rep: Reputation: 0
I think I know the answer to your question

you have an IP number , for example 130.194.166.33

and you want to find out the name that is given to the ip number, eg fred.pizza.overthere.com

If this is the case you just use the command `nslookup`

do a man on it.
 
Old 07-23-2003, 01:17 AM   #3
amitbhawnani
LQ Newbie
 
Registered: Jul 2003
Posts: 5

Original Poster
Rep: Reputation: 0
nope, i dont want the hostname

like for the IP - 130.194.166.33

i want to return the computed value of

(130*256*256*256)+(194*256*256)+(166*256)+(33)
 
Old 08-01-2003, 01:02 AM   #4
amitbhawnani
LQ Newbie
 
Registered: Jul 2003
Posts: 5

Original Poster
Rep: Reputation: 0
anybody ?
 
Old 08-01-2003, 12:32 PM   #5
MacKtheHacK
Member
 
Registered: Jul 2003
Location: Boston, MA, USA
Distribution: RedHat, SuSE, Gentoo, Slackware, Mandrake ...
Posts: 111

Rep: Reputation: 15
The nice thing about UNIX is there's tons of tools around. The not-so-nice thing is that you have to know what they are to use them. This is a job for "awk", which is good at both splitting your IP address into its four components, and doing the arithmetic. Here's a function that does it:

function IPtoNum()
{
echo "$1" | awk -F. '{printf("%ld\n", ($1 * 256 * 256 * 256) + ($2 * 256 * 256) + ($3 * 256) + ($4))}'
}

It's a bit more complicated to go the other way:

function NumtoIP()
{
echo "$1" | awk '{
a = int($1 / (256 * 256 * 256));
b = int(($1 / (256 * 256)) - (a * 256));
c = int(($1 / 256) - (a * 256 * 256) - (b * 256));
d = int($1 - (a * 256 * 256 * 256) - (b * 256 * 256) - (c * 256));
printf("%d.%d.%d.%d\n", a, b, c, d)}'
}

Not all of the parentheses are necessary, I just put them in for clarity. I hope this helps!
 
Old 08-01-2003, 09:26 PM   #6
Corin
Member
 
Registered: Jul 2003
Location: Jette, Brussels Hoofstedelijk Gewest
Distribution: Debian sid, RedHat 9, Suse 8.2
Posts: 446

Rep: Reputation: 31
For jobs like that, you should be using PERL.
 
Old 08-01-2003, 09:52 PM   #7
fsbooks
Member
 
Registered: Jan 2002
Location: Missoula. Montana, USA
Distribution: Slackware (various)
Posts: 464

Rep: Reputation: 52
Why perl for something so simple? Anyways, here is how I would do it: Someday I really have to learn awk, but I always seem to find away around it :-)

#!/bin/bash
# For clarity, assign first argument to ip
ip=$1
#initialize
i=1
m=1
sum=0
#loop
while [ $i -lt 5 ]
do
part=`echo $ip|cut -d. -f$((5-$i))`
let sum+=$(($m*$part))
let i+=1
let m*=256
done
#print sum
echo $sum
exit
 
Old 08-04-2003, 10:09 AM   #8
MacKtheHacK
Member
 
Registered: Jul 2003
Location: Boston, MA, USA
Distribution: RedHat, SuSE, Gentoo, Slackware, Mandrake ...
Posts: 111

Rep: Reputation: 15
Yeah, PERL is serious overkill for this. I used awk because I'm an old-timer who still writes shell scripts so that they work in Bourne shell. If you want a bash-only script, you can do it entirely in the shell, without executing any external program, which is nice and fast. Here's the equivalent functions in bash:

function IPtoNum() {
echo "$1" | (IFS='.' read a b c d; \
echo $(((($a*256+$b)*256+$c)*256+$d)) )
}

function NumtoIP() {
a=$(($1/256/256/256))
b=$(($1/256/256-$a*256))
c=$(($1/256-($a*256+$b)*256))
d=$(($1-(($a*256+$b)*256+$c)*256))
echo $a.$b.$c.$d
}

Nice and simple, no? Faster than, but not as portable as, the awk scripts.
 
  


Reply



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
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:53 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration