-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSubnetting.java
More file actions
167 lines (132 loc) · 3.15 KB
/
Subnetting.java
File metadata and controls
167 lines (132 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Write a program in Java/Python to demonstrate sub netting
for a network 192.168.4.10 / 26 (form 4 subnets) and find the sub net masks.
*/
import java.util.Scanner;
import java.net.InetAddress;
import java.util.*;
import java.io.*;
public class SUB {
public static void main(String[] args) {
Scanner s1=new Scanner(System.in);
String ipadd="";
System.out.println("ENter IP Address");
ipadd=s1.nextLine();
String splitip[]=ipadd.split("\\.");
//convert binary
String binip="";
for(int i=0;i<4;i++)
{
splitip[i]=appendZeros(Integer.toBinaryString(Integer.parseInt(splitip[i])));
binip+=splitip[i];
}
System.out.println("Binary is : " + binip);
System.out.println("ENter CIDR bits");
int cidr=s1.nextInt();
int bits=8-(cidr%8);
int total_address=(int)Math.pow(2,bits);
getsubnet(cidr);
//convert binary
int firstaddbint[]=new int[32];
int lastaddbint[]=new int[32];
for(int i=0;i<32;i++)
{
firstaddbint[i]=binip.charAt(i) - 48;
lastaddbint[i]=binip.charAt(i) - 48;
}
//anding
for(int i=31;i>31-bits;i--)
{
firstaddbint[i]=firstaddbint[i] & 0;
}
//move this to a string array
String ipinBin[]= {"","","",""};
for(int i=0;i<32;i++)
{
ipinBin[i/8]=new String(ipinBin[i/8]+firstaddbint[i]);
}
//convert to a decimal value
int FirstAddGrp[]=new int[4];
for(int i=0;i<4;i++)
{
FirstAddGrp[i]=Integer.parseInt(ipinBin[i],2);
System.out.print(FirstAddGrp[i]);
if(i!=3)
{
System.out.print(".");
}
}
//get last
for(int i=31;i>31-bits;i--)
{
lastaddbint[i]=lastaddbint[i] | 1;
}
//move this to a string
String LastipGrp[]= {"","","",""};
for(int i=0;i<32;i++)
{
LastipGrp[i/8]=new String (LastipGrp[i/8]+lastaddbint[i]);
}
//cnvrt in decimal
int LastipGrpinDec[]=new int[4];
for(int i=0;i<4;i++)
{
LastipGrpinDec[i]=Integer.parseInt(LastipGrp[i],2);
System.out.print(LastipGrpinDec[i]);
if(i!=3)
{
System.out.print(".");
}
}
System.out.println("How many subnets you want to form");
int scont=s1.nextInt();
for(int j=1;j<scont;j++)
{
System.out.println("Group " + (j+1)+ "First Address : ");
for(int i=0;i<4;i++)
{
if(i<3)
{
System.out.print(FirstAddGrp[i] + " .");
}
else
{
System.out.print(FirstAddGrp[i]=FirstAddGrp[i]+total_address);
}
}
System.out.println("Group " + (j+1)+ "Last Address : ");
for(int i=0;i<4;i++)
{
if(i<3)
{
System.out.print(LastipGrpinDec[i] + " .");
}
else
{
System.out.print(LastipGrpinDec[i]=LastipGrpinDec[i]+total_address);
}
}
}
}
private static void getsubnet(int cidr) {
// TODO Auto-generated method stub
int x=cidr%8;
int t=8-x;
int a=1;
int lastbit=0;
while(a<=x)
{
lastbit+=Math.pow(2,t);
t++;
a++;
}
String subnemask="255.255.255.";
subnemask+=String.valueOf(lastbit);
System.out.println("SUbnet is : " + subnemask);
}
private static String appendZeros(String binaryString) {
// TODO Auto-generated method stub
String temp="00000000";
return temp.substring(binaryString.length()) + binaryString;
}
}