博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

h0ng123 linux,oracle.unix

---在蓝天白云下自由飞翔 爱生活,爱CU---
Google


学习shell脚本编程的好地方
   h0ng123.cublog.cn
关于作者  
姓名:h0ng
职业:计算机
年龄:24
位置:广东
个性介绍:学习网络技术,UNIX/linux 系统管理.娱乐


我的分类  




How do I increment IP addresses in a shell script?
I need help with a shell script! I want to prompt someone to enter an IP address the automatically increment it and output the sequence of that address and its following addresses, but I don't know how to do that: when I try to increment a value like "10.10.10.27" it fails because it's not a number.
 
You've realized the problem already, you just need to think slightly out of the box to figure out the solution. That is, if an IP address isn't a number from the perspective of the shell, you need to break it up so that it is.
 
But first, a quick primer: IP (v4, at least) addresses are comprised of four decimal values, each ranging from 0 to 255. Effectively, however, 0 and 255 are typically reserved for routers, loopback addresses, etc, so really you'll want to range from 1-254 (but that's easy to tweak anyway).
 
A given IP address is therefore four numeric values all strung together with dot separators: 10.10.10.27. What the script needs to do is split the address back up, which can be done a number of different ways, but I'll use the cut command since I can specify what field separator to use:
 
baseaddr="$(echo $ip | cut -d. -f1-3)"lsv="$(echo $ip | cut -d. -f4)"(I'm using "lsv" to represent the least significant value here).
With this snippet of code, you'll have the following two values set:
 
baseaddr = 10.10.10lsv = 27That's perfect. Now you can step through your loop incrementing the "lsv" variable until you hit your MaxValue (254 or 255), regardless of where you start.
 
Here's my full version of your script:
 
#!/bin/sh

MaxValue=255    # highest valid IP octet value

echo -n "Enter IP address: ";  read ip
echo -n "How many IP addresses do you need: "; read count

baseaddr="$(echo $ip | cut -d. -f1-3)"
lsv="$(echo $ip | cut -d. -f4)"

while [ $count -gt 0 ]
do
   if [ $lsv -eq $MaxValue ] ; then
    # here you'll need to increment the third level IP value,
    # but that might cascade into the second, or the first.
    # consider the case of 17.255.255.255 + 1
    echo "edge case needs to be written"
  fi
  echo $baseaddr.$lsv
  lsv=$(( $lsv + 1 ))
  count=$(( $count - 1 ))
done

exit 0
 
Note that there's an important edge condition that needs to be written, which will actually make this script quite a bit more complex. The situation you need to tackle is what happens when the IP address rolls past the MaxValue?
If you have 10.10.10.255 then the next value can't be 10.10.10.256 because that's no longer a valid IP address, but rather 10.10.11.0. Doable, but you'll probably end up needing to break the IP address into all four octets rather than just the first three as a monolithic value and the fourth as the incrementing value.
But I'll leave that part of this homework assignment to you. :-)
 
Without considering the decimal of last octet exceeding the max value 255 (as Dave stated above)
I got a very small one liner for that: (my one is not taking care of that)
 
[jsaikia@zorro trap]$ cat ips.con
172.22.22.11
192.168.32.6
172.18.1.121
172.20.21.21

[jsaikia@zorro trap]$ for ip in `cat ips.con`; do echo $ip | awk -F "." '{print $1"."$2"."$3"."$4+1}'; done
172.22.22.12
192.168.32.7
172.18.1.122
172.20.21.22

 发表于: 2007-12-31,修改于: 2007-12-31 17:01 已浏览526次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:0.0219

京ICP证041476号