IP地址是66.102.13.19,从这个地址收到的地址就是这个地址。
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,从这个地址收到的地址就是这个地址。
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
"http://" "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"
顺便说一句,您问题中的号码与IP地址不匹配。
要将小数点转换为 IP:
#!/bin/bash
dec2ip () {
local ip delim dec=$@
for e in {3..0}
do
((octet = dec / (256 ** e) ))
((dec -= octet * 256 ** e))
ip+=$delim$octet
delim=.
done
printf %s
"$ip"
}
dec2ip "$@"
要将 IP 转换为小数点:
#!/bin/bash
ip2dec () {
local a b c d ip=$@
IFS=. read -r a b c d <<< "$ip"
printf %d
"$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"
}
ip2dec "$@"
演示 :
$ ./dec2ip 1113984275
66.102.13.19
$ ./ip2dec 66.102.13.19
1113984275
这两部剧本依赖Bash的特征,这些特征在一些包恩派的炮弹中并不存在。
#!/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
", ip)
}
和
#!/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
", dec)
}
他们可以和上面的巴什剧本一样被调用。
IP 地址 - & gt; 数字 :
echo 66.102.13.19 | tr .
| awk {s = s*256 + $1} END{print s}
数字 - & gt; IP 地址 :
(export ip=1113984275; for i in {1..4}; do s= . $((ip%256))$s && ((ip>>=8)); done; echo ${s:1})
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 <<<"$*";
echo "$(((a<<24)+(b<<16)+(c<<8)+d))";
}
dec2ip(){ # Convert an IPv4 decimal IP value to an IPv4 IP.
declare -i a=$((0xff)) b=$1;
c="$((b>>24&a)).$((b>>16&a)).$((b>>8&a)).$((b&a))";
echo "$c";
}
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.
要解决这个问题,每个值都必须用下列方法清洗:
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; }
对于(非常)旧的炮弹:自2.04起击落,或破折或任何(合理的)炮弹:
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;
c="$((b>>24&a)).$((b>>16&a)).$((b>>8&a)).$((b&a))";
echo "$c";
}
ip=$1
a=$(ip2dec "$ip")
b=$(dec2ip "$a")
echo "$ip DecIP=$a IPv4=$b "
我用Int到 ip 转换的版本 :
=========================================================================================================================================================================================================================================================================================================
这里,我的假设是:
$ 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}
我认为有一个更简单的解决方案, 也处理任意数量的octets, 没有提到固定的抵消等。
echo 66.102.13.19 |
tr .
|
while read octet; do
printf "%.08d" $(echo "obase=2;$octet" | bc)
done |
echo $((2#$(cat)))
产出:1113984275
用于bash 的 IP 转换
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
$i1 $i2 $i3 $i4
}
I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...
How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks
I like to write bash shell scripts on my iPhone, put them in an app folder with an icon, plist, etc., so they execute like apps by tapping their icon on SpringBoard. This is not interactive like ...
From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...
I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...
I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...
I m 试图撰写一个双面文字,处理一个文件清单,其名字在投入档案中每栏一栏储存,就像一个一样。
Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...