ip address octet increments


 
Thread Tools Search this Thread
Top Forums Programming ip address octet increments
# 1  
Old 03-21-2007
ip address octet increments

Hi all,

Situation is as below.
I would get an IP address and port from eithe r a file or command line. It probably would be as char * or string. So was wondering how I could accept this and increment the last octets?

Incrementing the port is fine. I could get that into an integer by atoi() and then increment it.
But the IP address part is getting tricky. Any say please?
# 2  
Old 03-21-2007
There are two standard calls for this (for IPv4 address formats):
inet_addr() returns an integer
inet_ntoa() returns the dotted form

Take the dotted form, convert to integer, add one, convert back to dotted form.
# 3  
Old 03-21-2007
thanks jim...
there is another way...the country road way Smilie

char inputIpstring[16];
int oct1,oct2,oct3,oct4,
sscanf(inputIpString,"%d.%d.%d.%d",&oct1,&oct2,&oct3,&oct4)

increment anyof the octetes as you want and

sprintf(inputIpString,"%d.%d.%d.%d",oct1,oct2,oct3,oct4);


BTW, if I pass 225.10.20.30 and would need all the Ip addresses from 225.10.20.30 to 225.10.30.30 without the last octet being changed....that is

225.10.20.30
225.10.21.30
225.10.22.30
225.10.23.30
225.10.24.30

etc...

willt his work with the inet_addr() method,
Would I add 510 and I will get 225.10.21.30 from 225.10.20.30?
# 4  
Old 03-22-2007
Try it and see - I've never used it that way. I believe the code works modulo 255 as you seem to indicate.

If you are shooting at enumerating the various ip addresses in your local network, there are more efficient ways than guessing ip's.
# 5  
Old 03-22-2007
jim...nope doesnt work...
works for the last octet though ...and is not linear after that.
thanks anyway. wills stick with my old method.

Quote:
Originally Posted by jim mcnamara
Try it and see - I've never used it that way. I believe the code works modulo 255 as you seem to indicate.

If you are shooting at enumerating the various ip addresses in your local network, there are more efficient ways than guessing ip's.
# 6  
Old 03-22-2007
Quote:
Originally Posted by Naanu
Would I add 510 and I will get 225.10.21.30 from 225.10.20.30?
That does not compute. How did you arrive at 510? Try 257.
# 7  
Old 03-23-2007
perderabo...sorry my bad its 255 not 510.
and it scales up fine for the last ocet if we we keep incrementing from 1 to 255, but not after that.

Quote:
Originally Posted by Perderabo
That does not compute. How did you arrive at 510? Try 257.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash subtract fourth octet of an IP by 1

Hello, Im looking to help out my team by automating a simple search list. The user will look for a peering ip /30. For example 192.168.1.2/30 and gets the result. Im trying to get the entered /30 and subtract the last octet by one. echo -n "Enter peering ip : "; read peeringip cat... (3 Replies)
Discussion started by: D'go
3 Replies

2. Programming

Celisus & fahrenheit table converter with fields + increments

no longer needed, please delete thread. (0 Replies)
Discussion started by: xtina
0 Replies

3. Shell Programming and Scripting

Awk: Help with how to remove 4rth octet :

Experts, In one example I have seen how to get output upto 3rd octet, when there is a ":" separated with the 4rth octet. However in this example how to remove 4rth octet and to keep upto 3rd octet with regular expressions and awk sub function: I have tried with :but not working: # awk '{... (3 Replies)
Discussion started by: rveri
3 Replies

4. Shell Programming and Scripting

awk Quick Help: printing upto 3rd octet .

Hi Experts, I am trying to print $2 & the IP_address upto 3rd octet only. But unable to do so, Trying # awk '{print $2, substr($4,1,9)}' file . but not correct File: HOST= cmiHOST06 :: 10.26.107.73:/data120 /nbu/cmiHOST06/athpx07/aa1 HOST= cmiHOST05 :: 10.26.12.76:/data120... (5 Replies)
Discussion started by: rveri
5 Replies

5. UNIX for Dummies Questions & Answers

What is an (application/octet-stream) file?

I'm trying to learn as much about GRUB as I can and it's stages are stored in these types of files. Any info or search terms is appreciated!:wall: (5 Replies)
Discussion started by: theKbStockpiler
5 Replies

6. Shell Programming and Scripting

Increments in a loop

Hi All, I am trying to implement a for loop in a .sh file and execute it. My for loop is for i in 1, 200, 400, 600, 800, 1000, 1200,......100000 do ... .. .. done As you could see there is an increment of 200 in each for loop until it reaches 100,000 Instead of specifying all the... (8 Replies)
Discussion started by: Lucky Ali
8 Replies

7. Shell Programming and Scripting

Averaging in increments using awk & head/tail

Hi, I only have a very limited understanding and experience with writing code and I was hoping I could get some help. I have a dataset of two columns (txt format, numbers in each row separated by a tab) Eg. 1 5 2 5 3 6 4 7 5 6 6 6 7 ... (5 Replies)
Discussion started by: Emred_Skye
5 Replies

8. UNIX for Dummies Questions & Answers

Want to process file in 1000 record increments

I have a file that varies in the # of records. I want to read 1000 records, do something, read the next 1000 records, do something, going in 1000 increment chunks, and then finish up with whatever is left over at end of file. (1 Reply)
Discussion started by: Tsamp
1 Replies

9. UNIX and Linux Applications

Any idea on 3 Octet IP address ?

Hi All, I found my weblog contain entries like 121.23.3 Instead of four octet. I am quite confused is it possible to have 3 octet ip at all ?? Is it generating by any program and hittng the website ? Is it a subdomain ? Please tell me your understanding on it ? Thanks (4 Replies)
Discussion started by: jambesh
4 Replies

10. Shell Programming and Scripting

Working with field increments

I have a new challenge that I need some help with. Each morning I have two files that contain working units and failed units that I join together to form file3 which is FTP to server for comparison against billing system. My problem, I would like to take joined data in file3 and add a field... (2 Replies)
Discussion started by: greengrass
2 Replies
Login or Register to Ask a Question