Linux - SoftwareThis 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.
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.
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...
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:
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.