-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
121 lines (106 loc) · 4.25 KB
/
Copy pathMain.java
File metadata and controls
121 lines (106 loc) · 4.25 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
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
MainPage();
}
public static void MainPage() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("---------------------------------------------------");
System.out.println(" Welcome To Nmap ");
System.out.println("---------------------------------------------------");
System.out.println(" 1 - Check Host Status ");
System.out.println(" 2 - Check Port Status of a Host ");
System.out.println(" 3 - Get & Post ");
System.out.println("---------------------------------------------------");
System.out.print("Choose a number : ");
int inputNumMainPage = scanner.nextInt();
switch (inputNumMainPage) {
case 1:
GetHostAndPort();
break;
case 2:
checkinhPorys();
break;
case 3:
break;
default:
MainPage();
}
}
//--------- part 1---------------------------------------------
private static void GetHostAndPort() throws IOException {
Scanner scanner = new Scanner(System.in);
// دریافت هاست و پورتها از کاربر
System.out.print("Enter the host/IP address: ");
String host = scanner.nextLine();
System.out.print("Enter the port: ");
int port = scanner.nextInt();
HostChecker(host,port);
}
private static void HostChecker(String inputHost , int inputPort) throws IOException {
String host = inputHost;
int port = inputPort;
// Create a Socket
try (Socket socket = new Socket(InetAddress.getByName(host), port)) {
// Try to connect
System.out.println(host + " is up");
} catch (IOException e) {
System.out.println(host + " is down");
}
}
//--------- part 2---------------------------------------------
private static String getServiceName(int port) { // تابع برای گرفتن نام سرویس بر اساس شماره پورت
switch (port) {
case 20:
return "FTP Data Transfer";
case 21:
return "FTP Control";
case 22:
return "SSH";
case 23:
return "Telnet";
case 25:
return "SMTP";
case 53:
return "DNS";
case 80:
return "HTTP";
case 443:
return "HTTPS";
default:
return "Unknown Service";
}
}
private static void checkinhPorys() {
Scanner scanner = new Scanner(System.in);
// دریافت هاست و رنج پورتها از کاربر
System.out.print("Enter the host/IP address: ");
String host = scanner.nextLine();
System.out.print("Enter the starting port: ");
int startPort = scanner.nextInt();
System.out.print("Enter the ending port: ");
int endPort = scanner.nextInt();
System.out.println("Scanning ports...");
// اسکن پورتها
for (int port = startPort; port <= endPort; port++) {
try {
// ایجاد یک سوکت با تاخیر زمانی 100 میلیثانیه برای هر پورت
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port), 100);
// نمایش اطلاعات پورت باز
if (getServiceName(port) != "Unknown Service")
System.out.println("Port " + port + " is open - Service: " + getServiceName(port));
// بستن سوکت
socket.close();
} catch (Exception e) {
// پورت بسته است یا خطای دیگر
}
}
// بستن اسکنر
scanner.close();
}
}