转换IP范围的IP地址 [英] Convert IP Range to IP address
问题描述
我有一个IP原始文件范围(xx.xx.xx. XX -yy.yy.yy. YY )
我想创建转换成单个IP地址范围内的新的列表。
(所有的范围都在1-255范围)
的条件的
(1)如在每行的第四个八比特组的IP之间的差小于或等于所述最大搜索
变量(如5)中循环,并报告每个迭代为单个/ 32的地址。
(2)比最大变量更将报告与IP地址的IP地址/ 24
以下bash脚本工作正常,但它是在50000行的文件慢?
任何帮助将是AP preciated。它是一种脚本,所以我需要留在BASH,做其它功能的一部分。
因为我在$的数据;做 A = $(回声$ I | sed中的/-.*//'); B = $(回声$ I | sed中的/^.*-//')
A1 = $(回声$ A |切-d -f 4'。'); B1 = $(回声$ B |'。切-d -f 4)
差异=`$ expr的B1 - $ A1` 如果[$差异==0];然后
回声$ A>> $ OUTFILE
ELIF [$差异-gt0-a$差异-le $ MAX];然后
回声$ A>> $ OUTFILE
对于在$(小额$差异);做
数=`expr的$ A1 + $了`
回声$ A | SEDS / \\ [0-9] * / $计数/。>> $ OUTFILE
DONE
其他
回声$ A | SED的/ \\ [0-9] * $ / 0 \\ / 24 /。'>> $ OUTFILE
科幻
DONE
可能的原因你的脚本是50000线如此之慢是你有庆典
调用了很多外部程序( SED
,削减
,小额
, EXPR
),多次在您的内环和外环的每次迭代。分叉外部进程增添了不少的时间开销,当通过多次迭代雪上加霜。
如果你想这样做在bash,并提高性能,你需要利用内置到庆典的等效功能。我参加了一个刺在此为你的脚本,并与该走了过来。我试图保持的功能是相同的:
因为我在$的数据;做 A =$ {I% - *}; B =$ {I#* - }
A1 =$ {A ##。*}; B1 =$ {B ## *。}
差异= $(($ B1 - $ A1)) 如果[$差异==0];然后
回声$ A>> $ OUTFILE
ELIF [$差异-gt0-a$差异-le $ MAX];然后
回声$ A>> $ OUTFILE
为((A = 1;&下; = $差异;一个++));做
数= $(($ A1 + $ a)条)
回声$ {A%*} $计数。>> $ OUTFILE
DONE
其他
回声$ {A%*} 0/24。>> $ OUTFILE
科幻
DONE
在我特别做了一个大量使用的参数扩展和算术扩展。我很想看看有什么加速的种类(如果有的话),这已超过原来的。我觉得应该是显著更快。
I have a raw file with IP ranges (xx.xx.xx.xx-yy.yy.yy.yy) I want to create a new list with the range converted into single IP addresses. (All ranges are in a 1-255 range)
conditions
(1) If the difference between the fourth IP octet on each line is less or equal to the max
variable (say 5) It will loop and report each iteration as a single /32 address.
(2) IP address with more than the max variable will be reported as ip address with /24
The following bash script works fine but it is slow on files of 50,000 lines? Any help would be appreciated. Its part of a script that does other functions so I need to stay in BASH.
for i in $data; do
A=$(echo $i | sed 's/-.*//'); B=$(echo $i | sed 's/^.*-//')
A1=$(echo $A | cut -d '.' -f 4); B1=$(echo $B | cut -d '.' -f 4)
diff=`expr $B1 - $A1`
if [ "$diff" == "0" ]; then
echo $A >> $outfile
elif [ "$diff" -gt "0" -a "$diff" -le $max ]; then
echo $A >> $outfile
for a in $(jot "$diff"); do
count=`expr $A1 + $a`
echo $A | sed "s/\.[0-9]*$/.$count/" >> $outfile
done
else
echo $A | sed 's/\.[0-9]*$/.0\/24/' >> $outfile
fi
done
The likely reason your script is so slow for 50000 lines is that you having bash
call a lot of external programs (sed
, cut
, jot
, expr
), several times in each iteration of your inner and outer loops. Forking external processes adds a lot of time overhead, when compounded over multiple iterations.
If you want to do this in bash, and improve performance, you'll need to make use of the equivalent features that are built into bash. I took a stab at this for your script and came up with this. I have tried to keep the functionality the same:
for i in $data; do
A="${i%-*}"; B="${i#*-}"
A1="${A##*.}"; B1="${B##*.}"
diff=$(($B1 - $A1))
if [ "$diff" == "0" ]; then
echo $A >> $outfile
elif [ "$diff" -gt "0" -a "$diff" -le $max ]; then
echo $A >> $outfile
for ((a=1; a<=$diff; a++)); do
count=$(($A1 + $a))
echo "${A%.*}.$count" >> $outfile
done
else
echo "${A%.*}.0/24" >> $outfile
fi
done
In particular I've made a lot of use of parameter expansions and arithmetic expansions. I'd be interested to see what kind of speedup (if any) this has over the original. I think it should be significantly faster.
这篇关于转换IP范围的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!