-
Notifications
You must be signed in to change notification settings - Fork 0
/
portScanner.cpp
43 lines (34 loc) · 1002 Bytes
/
portScanner.cpp
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
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
bool isPortOpen(const std::string& ipAddress, int port) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
return false;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ipAddress.c_str());
server.sin_port = htons(port);
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == 0) {
close(sock);
return true;
}
close(sock);
return false;
}
int main() {
std::string ipAddress;
std::cout << "Enter the IP address or hostname: ";
std::cin >> ipAddress;
std::cout << "Scanning ports for " << ipAddress << "...\n";
for (int port = 1; port <= 65535; ++port) {
if (isPortOpen(ipAddress, port)) {
std::cout << "Port " << port << " is open.\n";
}
}
std::cout << "Port scanning completed.\n";
return 0;
}