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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-22-2003, 11:37 PM
|
#1
|
LQ Newbie
Registered: Jul 2003
Posts: 5
Rep:
|
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...
|
|
|
07-23-2003, 12:34 AM
|
#2
|
LQ Newbie
Registered: Jul 2003
Posts: 16
Rep:
|
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.
|
|
|
07-23-2003, 01:17 AM
|
#3
|
LQ Newbie
Registered: Jul 2003
Posts: 5
Original Poster
Rep:
|
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)
|
|
|
08-01-2003, 01:02 AM
|
#4
|
LQ Newbie
Registered: Jul 2003
Posts: 5
Original Poster
Rep:
|
anybody ?
|
|
|
08-01-2003, 12:32 PM
|
#5
|
Member
Registered: Jul 2003
Location: Boston, MA, USA
Distribution: RedHat, SuSE, Gentoo, Slackware, Mandrake ...
Posts: 111
Rep:
|
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!
|
|
|
08-01-2003, 09:26 PM
|
#6
|
Member
Registered: Jul 2003
Location: Jette, Brussels Hoofstedelijk Gewest
Distribution: Debian sid, RedHat 9, Suse 8.2
Posts: 446
Rep:
|
For jobs like that, you should be using PERL.
|
|
|
08-01-2003, 09:52 PM
|
#7
|
Member
Registered: Jan 2002
Location: Missoula. Montana, USA
Distribution: Slackware (various)
Posts: 464
Rep:
|
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
|
|
|
08-04-2003, 10:09 AM
|
#8
|
Member
Registered: Jul 2003
Location: Boston, MA, USA
Distribution: RedHat, SuSE, Gentoo, Slackware, Mandrake ...
Posts: 111
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 05:16 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|