ksh - how to list all ip address between 2 ip address


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh - how to list all ip address between 2 ip address
# 1  
Old 06-12-2009
ksh - how to list all ip address between 2 ip address

Trying to do a ksh script that needs to list all ip address between ip address a and b ..


ie.
Ip address A=192.168.1.200
Ip address B=192.168.2.15

So the subnet changes from 1 to 2 but I want to list all possible ip addresses between the 2..

Which would be:
192.168.1.200
192.168.1.201..... up to .254
192.168.2.1
192.168.2.2.... up to .15


The script need to be able to cater for different subnets etc..

Any help?
# 2  
Old 06-12-2009
just use 2 for loops
Code:
awk 'BEGIN{
 for(i=1;i<=2;i++){
  for(j=1;j<=254;j++){
   print "192.168."i"."j
  }
 }
} '

# 3  
Old 06-12-2009
Quote:
Originally Posted by ghostdog74
just use 2 for loops
Code:
awk 'BEGIN{
 for(i=1;i<=2;i++){
  for(j=1;j<=254;j++){
   print "192.168."i"."j
  }
 }
} '


Thanks.. I copy /pasted this in to script on its own and ran and it works fine - but it doesnt seem to complete... it doesnt return to command line unless I CTRL-C

Do you know why this is? is there something missing to end the for loop?

-----Post Update-----

Quote:
Originally Posted by ghostdog74
just use 2 for loops
Code:
awk 'BEGIN{
 for(i=1;i<=2;i++){
  for(j=1;j<=254;j++){
   print "192.168."i"."j
  }
 }
} '

Actually - this doesnt appear to work as I had hoped.
ie. If my start ip was 192.168.1.1 and end was 192.168.2.4 then in the script above i <=1 and j<=4 so I only get the following listed

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.2.1
192.168.2.2
192.168.2.3
192.168.2.4

So everything before 192.168.1.4 and 192.168.2.1 is missing....

-----Post Update-----

Quote:
Originally Posted by frustrated1
Thanks.. I copy /pasted this in to script on its own and ran and it works fine - but it doesnt seem to complete... it doesnt return to command line unless I CTRL-C

Do you know why this is? is there something missing to end the for loop?

-----Post Update-----



Actually - this doesnt appear to work as I had hoped.
ie. If my start ip was 192.168.1.1 and end was 192.168.2.4 then in the script above i <=1 and j<=4 so I only get the following listed

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.2.1
192.168.2.2
192.168.2.3
192.168.2.4

So everything before 192.168.1.4 and 192.168.2.1 is missing....



I figured out how I can do this in a ksh script - a bit long winded and I am sure there is easier way to do it but it works..

Run script with start ip address as arugment 1 and end ip address as argument 2

./getrange.sh 192.168.1.1 192.168.3.50

getrange.sh

#!/usr/bin/ksh
#set -x

a1=$1
b1=$2

a1subnet=`echo $a1 | cut -d. -f3`
a1ipadd=`echo $a1 | cut -d. -f4`
b1subnet=`echo $b1 | cut -d. -f3`
b1ipadd=`echo $b1 | cut -d. -f4`

if [ $b1subnet -gt $a1subnet ]
then

## A1 list
a1ipaddnumb=$a1ipadd
while [ $a1ipaddnumb -le 254 ]
do
echo "194.20.$a1subnet.$a1ipaddnumb"
a1ipaddnumb=$(($a1ipaddnumb+1))
done

## Any list between A1 and B1
xsubnet=$(($a1subnet+1))

if [ $xsubnet -lt $b1subnet ]
then
while [ $xsubnet -lt $b1subnet ]
do
xipaddnumb=1
while [ $xipaddnumb -le 254 ]
do
echo "194.20.$xsubnet.$xipaddnumb"
xipaddnumb=$(($xipaddnumb+1))
done
xsubnet=$(($xsubnet+1))
done
fi


## B1 list
b1ipaddnumb=1
while [ $b1ipaddnumb -le $b1ipadd ]
do
echo "194.20.$b1subnet.$b1ipaddnumb"
b1ipaddnumb=$(($b1ipaddnumb+1))
done


fi
# 4  
Old 06-12-2009
specific to ur example

Code:
 
 awk -F"." '{ if(NR==1){ for(i=$4;i<=254;i++) print $1"."$2"."$3"."i >> "file_op" } else { for(i=1;i<=$4;i++) print $1"."$2"."$3"."i >> "file_op" }} ' input_file.txt

# 5  
Old 06-12-2009
If you are using ksh93 you can use the ?: construct to more easily handle the start and end conditions.
Code:
#!/usr/bin/ksh93

START="192.168.7.245"
STOP="192.168.8.5"

integer i j

IFS="." typeset -a aStart=($START)
IFS="." typeset -a aStop=($STOP)

for (( i=${aStart[2]}; i <= ${aStop[2]}; i++ ))
do
    start=$(( i == ${aStart[2]} ? ${aStart[3]} : 0 ))
    stop=$(( i == ${aStop[2]} ? ${aStop[3]} : 254 ))

    for (( j=$start; j <= $stop; j++ ))
    do
       printf "192.168.%d.%d\n" $i $j
    done
done

gives
Code:
192.168.7.245
192.168.7.246
192.168.7.247
192.168.7.248
192.168.7.249
192.168.7.250
192.168.7.251
192.168.7.252
192.168.7.253
192.168.7.254
192.168.8.0
192.168.8.1
192.168.8.2
192.168.8.3
192.168.8.4
192.168.8.5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

IP address list padding with 0

Hi All, I'm hoping one of you helpful lot could come up with a way I can do the below. I've been trying to think how is best but have struggled to come up with a way. I have a list of IP addresses in the below format 1.1.1.1 102.1.2.3 102.102.1.10 102.102.102.1 I need to compare... (4 Replies)
Discussion started by: mutley2202
4 Replies

2. UNIX for Advanced & Expert Users

C program to detect duplicate ip address if any after assigning ip address to ethernet interface

Hi , Could someone let me know how to detect duplicate ip address after assigning ip address to ethernet interface using c program (3 Replies)
Discussion started by: Gopi Krishna P
3 Replies

3. Cybersecurity

Configure iptables to allows list of MAC address

Hi all, I want to make this nw diagram: Small NW ---(eth1)-- Linux iptables --(eth0)---LAN NW And with these requirements: 1. Allow only 1 Mac address aa-aa-aa-aa-aa-aa from Small NW to LAN NW 2. Allow list of Mac addresses from LAN NW access to Small NW What will I... (2 Replies)
Discussion started by: blackthu80
2 Replies

4. UNIX for Dummies Questions & Answers

Find unique IP address in a list

Hello, I got a list of IP address from which I would like to remove the duplicates. I cat the file and pipe it to uniq -u or uniq -c, I got the same output with all the duplicates. Can anybody please tell me how I can remove the duplicates IPs from this file? This is what I used. cat filename |... (3 Replies)
Discussion started by: Pouchie1
3 Replies

5. IP Networking

Tracing a MAC address to IP address: Solaris

Hi there I lost connectivity to one of our remote systems and when I checked the messages log I found the following: Aug 10 23:42:34 host xntpd: time reset (step) 1.681729 s Aug 16 13:20:51 host ip: WARNING: node "mac address" is using our IP address x.x.x.x on aggr1 Aug 16 13:20:51 host... (9 Replies)
Discussion started by: notreallyhere
9 Replies

6. UNIX for Dummies Questions & Answers

What would the physical address be for virtual address?

Hi guys, I got one problem which I definetily no idea. What would the physical address be for virtual address? 1) 2ABC 2) 3F4B Here is the page table:see attached Thank you sos sososososso much!! (0 Replies)
Discussion started by: lemon_06
0 Replies

7. UNIX for Dummies Questions & Answers

Panic kernal-mode address fault on user address 0x14

:) Firstly Hi all!!, im NEW!! and on here hoping that someone might be able to offer me some help... i have a server that keeps crashing every few days with the error message: PANIC KERNAL-MODE ADDRESS FAULT ON USER ADDRESS 0X14 KERNAL PAGE FAULT FROM (CS:EIP)=(100:EF71B5BD) EAX=EF822000... (10 Replies)
Discussion started by: Twix
10 Replies

8. UNIX for Dummies Questions & Answers

send email from address list and subject list

Hello, Here is my problem. there are two files. first.txt <<< contains email address ====== abc@mail.com abd@mail.com abe@mail.com second.txt <<< contains webpage links ======== http//www.test.com/abc/index.html http://www.test.com/abd/index.html http://www.test.com/abe/index.html... (2 Replies)
Discussion started by: paulds
2 Replies

9. IP Networking

How to Achive IP address through MAC(Ethernet) address

Hi sir, i want to make such programe which takes MAC(Ethernet) address of any host & give me its IP address....... but i'm nt getting that how i can pass the MAC address to Frame........ Please give me an idea for making such program... Thanks & regards Krishna (3 Replies)
Discussion started by: krishnacins
3 Replies

10. UNIX for Dummies Questions & Answers

network address and broadcast address?

say I have a IP address which is 10.0.0.12, and subnet mask is 255.255.255.240, what is the network address and what is the broadcast address which host lives on? And could you explain how to get the answer? thanx in advance! (7 Replies)
Discussion started by: pnxi
7 Replies
Login or Register to Ask a Question