
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)" (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.10 That'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 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. :-)
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
Shell Script Programming
(Article 5877)
Tagged: linux, script programming, shell script Previous: Are the gift cards offered on eBay legit, or scams? Next: How can I reset the admin password on MacOS 9? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. The one given by Dave is a nice one :-) . Without considering the decimal of last octet exceeding the max value 255 (as Dave stated above) [jsaikia@zorro trap]$ cat ips.con I have a lot to say, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|