There is IP address: 66.102.13.19, and from this address as that received this address
http://1113984275
But how? And how I can make this with the help of bash. For example, this service can do it, but I do not understand the algorithm.
There is IP address: 66.102.13.19, and from this address as that received this address
http://1113984275
But how? And how I can make this with the help of bash. For example, this service can do it, but I do not understand the algorithm.
ip=66.102.13.19
IFS=. read -r a b c d <<< "$ip"
printf '%s%d\n' "http://" "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"
By the way, the number in your question doesn't match the IP address.
To convert a decimal to an IP:
#!/bin/bash
dec2ip () {
local ip dec=$@
for e in {3..0}
do
((octet = dec / (256 ** e) ))
((dec -= octet * 256 ** e))
ip+=$delim$octet
delim=.
done
printf '%s\n' "$ip"
}
dec2ip "$@"
To convert an IP to a decimal:
#!/bin/bash
ip2dec () {
local a b c d ip=$@
IFS=. read -r a b c d <<< "$ip"
printf '%d\n' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"
}
ip2dec "$@"
Demo:
$ ./dec2ip 1113984275
66.102.13.19
$ ./ip2dec 66.102.13.19
1113984275
These two scripts rely on features of Bash that aren't present in some Bourne-derived shells. Here are AWK versions to use instead:
#!/usr/bin/awk -f
# dec2ip
BEGIN {
dec = ARGV[1]
for (e = 3; e >= 0; e--) {
octet = int(dec / (256 ^ e))
dec -= octet * 256 ^ e
ip = ip delim octet
delim = "."
}
printf("%s\n", ip)
}
and
#!/usr/bin/awk -f
# ip2dec
BEGIN {
ip = ARGV[1]
split(ip, octets, ".")
for (i = 1; i <= 4; i++) {
dec += octets[i] * 256 ** (4 - i)
}
printf("%i\n", dec)
}
They can be called in the same manner as the Bash scripts above.
IP address -> Number:
echo 66.102.13.19 | tr . '\n' | awk '{s = s*256 + $1} END{print s}'
Number -> IP address:
(export ip=1113984275; for i in {1..4}; do s='.'$((ip%256))$s && ((ip>>=8)); done; echo ${s:1})
my version for int to ip conversion:
echo 3232235521| awk {'print rshift(and($1, 0xFF000000), 24) "." rshift(and($1, 0x00FF0000), 16) "." rshift(and($1, 0x0000FF00), 8) "." and($1, 0x000000FF) '}
Here's my take:
$ host google.com
google.com has address 216.58.216.142
$ ./ip2d.sh 216.58.216.142
3627735182
$ ./d2ip.sh 3627735182
216.58.216.142
ip2d.sh:
#!/bin/bash
IFS=.
set -- $*
echo $(( ($1*256**3) + ($2*256**2) + ($3*256) + ($4) ))
d2ip.sh:
#!/bin/bash
IFS=" " read -r a b c d <<< $(echo "obase=256 ; $1" |bc)
echo ${a#0}.${b#0}.${c#0}.${d#0}
Binary shifting is always faster than multiplying or dividing.
Using a binary AND is faster than mod.
ip2dec(){ # Convert an IPv4 IP number to its decimal equivalent.
declare -i a b c d;
IFS=. read a b c d <<<"$1";
echo "$(((a<<24)+(b<<16)+(c<<8)+d))";
}
dec2ip(){ # Convert an IPv4 decimal IP value to an IPv4 IP.
declare -i a=$((~(-1<<8))) b=$1;
set -- "$((b>>24&a))" "$((b>>16&a))" "$((b>>8&a))" "$((b&a))";
local IFS=.;
echo "$*";
}
ip=66.102.13.19
a=$(ip2dec "$ip")
b=$(dec2ip "$a")
echo "$ip DecIP=$a IPv4=$b "
Note: This system will fail with values like 0008
.
Bash thinks it is an octal number.
To solve that, each value will have to be cleaned with something like:
IsDecInt()( # Is the value given on $1 a decimal integer (with sign)?
declare n=$1; set --
if [[ $n =~ ^([+-]?)((0)|0*([0-9]*))$ ]]; then
set -- "${BASH_REMATCH[@]:1}"
else
exit 1
fi
echo "$1$3$4";
)
a=$(IsDecInt "$a")||{ Echo "Value $a is not an integer" >&2; exit 1; }
For (very) old shells: bash since 2.04, or dash or any (reasonable) shell:
ip2dec(){ # Convert an IPv4 IP number to its decimal equivalent.
local a b c d;
IFS=. read a b c d <<-_EOF_
$1
_EOF_
echo "$(((a<<24)+(b<<16)+(c<<8)+d))";
}
dec2ip(){ # Convert an IPv4 decimal IP value to an IPv4 IP.
local a=$((~(-1<<8))) b=$1;
set -- "$((b>>24&a))" "$((b>>16&a))" "$((b>>8&a))" "$((b&a))";
local IFS=.;
echo "$*";
}
ip=$1
a=$(ip2dec "$ip")
b=$(dec2ip "$a")
echo "$ip DecIP=$a IPv4=$b "
I think there's a simpler solution that also handles an arbitrary number of octets with no references to fixed offsets etc.
echo 66.102.13.19 |
tr . '\n' |
while read octet; do
printf "%.08d" $(echo "obase=2;$octet" | bc)
done |
echo $((2#$(cat)))
output: 1113984275
Simple int to IP conversion for bash
dec2ip ()
{
local v=$1
local i1=$((v>>24&255))
local i2=$((v>>16&255))
local i3=$((v>>8&255))
local i4=$((v&255))
printf '%d.%d.%d.%d\n' $i1 $i2 $i3 $i4
}