Compare IP addresses and increment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare IP addresses and increment
# 1  
Old 10-04-2013
Compare IP addresses and increment

Basically, I have 2 files with IP address. For example 134.123.3.234 in the first file and 134.123.3.235 in the second. Now I want to create a file with a IP address with IP 134.123.3.236...(max 254). So i have to check files which IP address in previous two files and base on that to create a new file with the next IP address which is 134.123.3.236. I wanted to check last number of IP...If it is 254 then increment +1 to third number....


Code:
for ip in {0..254}
do
if "192.168.${ip}.${j}" > $file
 echo "192.168.${ip}.${j}"> Newfile.txt
for j in {0..254}
 do
if "192.168.${ip}.${j}">$file then 
echo "192.168.${ip}.${j}"> Newfile1.txt
done
done

Basically, this code is not correct. Have no idea how to solve this problem. Hope you will help me....thanks.

Last edited by Don Cragun; 10-04-2013 at 01:36 AM.. Reason: Add CODE tags
# 2  
Old 10-04-2013
Code:
ip=`head -n1 file.dat`					# read ip address from file
end=${ip##*\.}						# delete everthing before last dot (leaving 234/235)

while (( end++ < 254 )); do				# while the 234/235 is less than 254 (increment each time tested)
	printf 134.123.3.$end"\r\n" >> NewFile.txt	# append your incremented ip address to NewFile.txt
done


Last edited by jethrow; 10-04-2013 at 02:11 AM..
This User Gave Thanks to jethrow For This Post:
# 3  
Old 10-04-2013
Quote:
Originally Posted by jethrow
Code:
ip=`head -n1 file.dat`
end=${ip##*\.}

while (( end++ < 254 )); do
    printf 134.123.3.$end"\r\n" >> NewFile.txt
done

Thank for replying. can you explain what it does....New here, sorry... (Tried to compile but still not sure what some lines do)
# 4  
Old 10-04-2013
-- delete

Last edited by roche; 10-04-2013 at 03:00 AM.. Reason: delete
# 5  
Old 10-04-2013
Two nested loops will cycle through all combinations.
A break will exit the current loop.
Code:
#!/bin/bash
for ip in {0..254}
do
  for j in {0..254}
  do
    if grep -qw "192\.168\.${ip}\.${j}" firstfile secondfile
    then
      : # exists
    else
      echo "192.168.${ip}.${j}" >Newfile1.txt
      break 2 # exit this loop and the next loop
    fi
  done
done

# 6  
Old 10-04-2013
Not quite sure I understand your request. Start at the highest IP in your two files, and create 254 IP addresses from there? Try:
Code:
sort file1 file2 | tail -1 | { IFS="." read A B C D; for J in {1..25}; do [ $((++D)) -eq 255 ] && { ((C++)); D=0; }; echo $A.$B.$C.$D; done }
134.123.3.236
134.123.3.237
134.123.3.238
134.123.3.239
134.123.3.240
134.123.3.241
134.123.3.242
134.123.3.243
134.123.3.244
134.123.3.245
134.123.3.246
134.123.3.247
134.123.3.248
134.123.3.249
134.123.3.250
134.123.3.251
134.123.3.252
134.123.3.253
134.123.3.254
134.123.4.0
134.123.4.1
134.123.4.2
134.123.4.3
134.123.4.4
134.123.4.5
etc...

# 7  
Old 10-06-2013
Quote:
Originally Posted by jethrow
Code:
ip=`head -n1 file.dat`                    # read ip address from file
end=${ip##*\.}                        # delete everthing before last dot (leaving 234/235)

while (( end++ < 254 )); do                # while the 234/235 is less than 254 (increment each time tested)
    printf 134.123.3.$end"\r\n" >> NewFile.txt    # append your incremented ip address to NewFile.txt
done

Thats cool but when its 254 i need to increment third oct....but in code we deleted it lol how to make it happen?Smilie

---------- Post updated at 02:25 PM ---------- Previous update was at 02:09 PM ----------

Quote:
Originally Posted by RudiC
Not quite sure I understand your request. Start at the highest IP in your two files, and create 254 IP addresses from there? Try:
Code:
sort file1 file2 | tail -1 | { IFS="." read A B C D; for J in {1..25}; do [ $((++D)) -eq 255 ] && { ((C++)); D=0; }; echo $A.$B.$C.$D; done }
134.123.3.236
134.123.3.237
134.123.3.238
134.123.3.239
134.123.3.240
134.123.3.241
134.123.3.242
134.123.3.243
134.123.3.244
134.123.3.245
134.123.3.246
134.123.3.247
134.123.3.248
134.123.3.249
134.123.3.250
134.123.3.251
134.123.3.252
134.123.3.253
134.123.3.254
134.123.4.0
134.123.4.1
134.123.4.2
134.123.4.3
134.123.4.4
134.123.4.5
etc...

I see.... in sort command we have 2 files... What about if i have a path to bunch of files??? /Users/unknown/files/* I put after sort but it does not work lol How i can put this path? Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Increment the password value

I want a script which increments the count when the script runs. Basically I want to send an password reset email notification for an application, the password value should be keep on changing whenever the script is executed for example, first time i execute it should be password1, second time... (2 Replies)
Discussion started by: JAGADESH GN
2 Replies

2. Shell Programming and Scripting

Increment time

I have to increment time ... by sec but i am getting the output like this. for m in {2..3} > do > for (( i = 1; i <= 13; i++ )) > do > echo "$m:$i" > done > done 2:1 2:2 2:3 2:4 2:5 2:6 2:7 2:8 (2 Replies)
Discussion started by: kalyankalyan
2 Replies

3. Shell Programming and Scripting

Bash arrays that compare ip addresses.

I've been trying to have an array of ip addresses go through a loop one at a time. Then compare if the current element is in another array of ip addresses. I've traced my error with /bin/bash -x + for c in '"${ip}"' ./netk5: line 65: 50.17.231.23 23.64.146.110 23.64.159.139 107.14.36.129... (17 Replies)
Discussion started by: Azrael
17 Replies

4. Shell Programming and Scripting

Increment Gawk

Hi, I have a small query with gawk which i'm unsure how to solve. My csv input data is as follows: 1 58352.9 34549 -469.323 LINE_149 2 58352.9 34499 -469.323 LINE_149 3 58352.9 34549 -469.323 LINE_151 4 58352.9 34503.4 -489.841 LINE_151 5 58352.9 34549 -469.323 LINE_152 6 58352.9... (1 Reply)
Discussion started by: theflamingmoe
1 Replies

5. Shell Programming and Scripting

Awk - Compare fields and increment variables

Hi, My first post to this group... I have a need to to parse a source file which is a capture from a network analyser. I have two fields that need to be checked: - Field 7 represents the packet length (an integer), and Field 4 represents a network address (e.g. 192.168.25.3) - The... (10 Replies)
Discussion started by: mv652
10 Replies

6. UNIX for Dummies Questions & Answers

Different ip addresses

Hello! I have logged in using Putty into another machine 'tele'. The ip address which i used to login to 'tele' is 192.168.1.3. Now while at 'tele' when i run "#ifconfig -a" i get the same ip address i.e, 192.168.1.3. But when i run "#arp tele" it gives the output: tele (10.143.128.8) ... (9 Replies)
Discussion started by: suhail.sadaqat
9 Replies

7. Shell Programming and Scripting

Increment value (starttime)

Hi All, I have created a script... #!/bin/sh datafile=ABC2008101601.OUT indfile=ABCIND20081016.1.OUT waittime=600 starttime=0 while do if then echo "Indicator file has arrived." break else sleep 10; ((starttime=$starttime+10)) echo $starttime (2 Replies)
Discussion started by: Amit.Sagpariya
2 Replies

8. Shell Programming and Scripting

increment an integer

hi I want to echo the variable $i while it auto-increments till 21 I set initially i to 1 any idea how to do that? thank you (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

9. IP Networking

Ip Addresses

I'm not exactly sure what I can do with IPs... my friend won't tell me(don't ask me why, I figure it's cause he doesn't know either, hehe). I'm curious as to what theya re used for other than networking computers... if there IS any other purpose or use for them. That's all. --Evil_d00d (4 Replies)
Discussion started by: evil_d00d
4 Replies

10. IP Networking

ip addresses

this might sound corny but i need help finding out about finding a persons ip address can u halp? and another question what the hell does ping mean??? thank u. (2 Replies)
Discussion started by: dragonslayer100
2 Replies
Login or Register to Ask a Question