-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubnet_A3.java
169 lines (134 loc) · 3.08 KB
/
Subnet_A3.java
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
167
import java.util.Scanner;
import java.net.InetAddress;
class subnet
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter the ip address=");
String ip=sc.nextLine();
//----Split the Ip Address-------
String split_ip[] = ip.split("\\.");
//----- Converting the Ip Address to binary----
String split_bip[]= new String[4];
String bip = "";
for(int i=0;i<4;i++)
{
split_bip[i]=appendZeroes(Integer.toBinaryString(Integer.parseInt(split_ip[i])));
bip+=split_bip[i];
}
System.out.println("The binary IpAddress is="+bip);
//----- Finding the Subent mask-----
System.out.println("Enter the number of address");
int n=sc.nextInt();
int bits=(int)Math.ceil(Math.log(n)/Math.log(2));
System.out.println("The number of bits required="+bits);
int mask=32-bits;
int total_address=(int)Math.pow(2,bits);
System.out.println("Subnet mask is "+mask);
//---- Finding the first and last address----
//---- First address Calculation--------
int fbip[]=new int[32];
for(int i=0;i<32;i++)
{
//Convert to the character 1,0 to integer 1,0
fbip[i]=(int)bip.charAt(i)-48;
}
for(int i=31;i>31-bits;i--)
{
//Get first address by anding the last bits with 0
fbip[i] &=0;
}
String fip[]={"","","",""};
for(int i=0;i<32;i++)
{
fip[i/8]=new String(fip[i/8]+fbip[i]);
}
int first_offset=0;
int ipAddr[]=new int[4]; ;
System.out.println("Group 1 \nThe First Address is:");
for(int i=0;i<4;i++)
{
System.out.print(ipAddr[i]=Integer.parseInt(fip[i],2));
if(i!=3)
System.out.print(".");
}
System.out.println();
//--- Last address Calculation----
int lbip[]=new int [32];
for(int i=0;i<32;i++)
{
// Convert the character 1,0 to integer 1,0
lbip[i]=(int)bip.charAt(i)-48;
}
for(int i=31;i>31-bits;i--)
{
// Get last address by oring with last bits with 1
lbip[i]|= 1;
}
String lip[]={"","","",""};
for(int i=0;i<32;i++)
{
lip[i/8]=new String(lip[i/8]+lbip[i]);
}
int ipLast[]=new int[4];
System.out.println("The Last Address is:");
for(int i=0;i<4;i++)
{
System.out.print(ipLast[i]=Integer.parseInt(lip[i],2));
if(i!=3)
System.out.print(".");
}
System.out.println();
System.out.println("How many subnets do you want to form?");
int scount=sc.nextInt();
for(int j=1;j<scount;j++)
{
System.out.println(" GROUP "+ (j+1)+" FIRST ADDRESS:");
for(int i=0;i<4;i++)
{
if(i<3)
{
System.out.print(ipAddr[i]+".");
}
else
System.out.println(ipAddr[i]=ipAddr[i]+total_address);
}
System.out.println(" GROUP "+ (j+1)+" LAST ADDRESS:");
for(int i=0;i<4;i++)
{
if(i<3)
{
System.out.print(ipLast[i]+".");
}
else
System.out.println(ipLast[i]=ipLast[i]+total_address);
}
System.out.println();
}
try
{
System.out.println("Enter the Ip address to ping=");
Scanner s=new Scanner(System.in);
String ip_add=s.nextLine();
InetAddress inet = InetAddress.getByName(ip_add);
if(inet.isReachable(5000))
{
System.out.println("The ip address is reachable"+ip_add);
}
else
{
System.out.println("The ip address is not reachable"+ip_add);
}
}
catch( Exception e)
{
System.out.println("Exception:"+e.getMessage());
}
}
static String appendZeroes(String s)
{
String temp= new String("00000000");
return temp.substring(s.length())+ s;
}
}