IP范围生成器脚本 [英] IP range generator script

查看:109
本文介绍了IP范围生成器脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个会产生IP范围的脚本......比如从64.1.1.1到74.255.255.255,输出应该是这样的:

I need a script that will generate IP ranges... lets say from 64.1.1.1 to 74.255.255.255 and the output should be like this:

64.1.1.1 
64.1.1.2
64.1.1.3
........
74.255.255.254
74.255.255.255
(line-by-line)


推荐答案

#!/bin/bash

# convert IP to decimal
ip2dec() {
  set -- ${1//./ }     # split $1 with "." to $1 $2 $3 $4
  declare -i dec       # set integer attribute
  dec=$1*256*256*256+$2*256*256+$3*256+$4
  echo $dec
}

# convert decimal to IP
dec2ip() {
  declare -i ip1 ip2 ip3 ip4
  ip1=$1/256/256/256
  ip2=($1-$ip1*256*256*256)/256/256
  ip3=($1-$ip1*256*256*256-$ip2*256*256)/256
  ip4=$1-$ip1*256*256*256-$ip2*256*256-$ip3*256
  echo $ip1.$ip2.$ip3.$ip4
}

s=$(ip2dec $1)
e=$(ip2dec $2)

for ((i=$s;i<=$e;i++)); do
  dec2ip $i 
done

示例: ./script.sh 64.1.2.250 64.1.3.5

输出:


64.1.2.250
64.1.2.251
64.1.2.252
64.1.2.253
64.1.2.254
64.1.2.255
64.1.3.0
64.1.3.1
64.1.3.2
64.1.3.3
64.1.3.4
64.1.3.5

如果你需要性能,我建议使用perl。

If you need performance, I suggest to use perl.

这篇关于IP范围生成器脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆