Skip to content

Instantly share code, notes, and snippets.

@fernandoporazzi
Created May 9, 2025 14:05
Show Gist options
  • Save fernandoporazzi/8b198da0e47b8f8238d4fcd148e3755a to your computer and use it in GitHub Desktop.
Save fernandoporazzi/8b198da0e47b8f8238d4fcd148e3755a to your computer and use it in GitHub Desktop.
Open ports scanner
#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#define BLUE "\x1b[34m"
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define RESET "\x1b[0m"
void display_results(int open_count, int total, double scan_time)
{
printf(BLUE "\nScan completed in %.2f seconds\n" RESET, scan_time);
printf("Result: " GREEN "%d open" RESET " and " RED "%d closed" RESET " ports found\n", open_count, total - open_count);
}
int resolve_hostname(const char* hostname, struct sockaddr_in* sa)
{
struct hostent *host;
memset(sa, 0, sizeof(*sa));
sa->sin_family = AF_INET;
// is ip address or domain?
if (isdigit(hostname[0]))
{
sa->sin_addr.s_addr = inet_addr(hostname);
return 1;
} else if ((host = gethostbyname(hostname)) != 0)
{
memcpy(&sa->sin_addr, host->h_addr, host->h_length);
return 1;
}
herror(hostname);
return 0;
}
void scan_port_range(const char* hostname, int start_port, int end_port)
{
struct sockaddr_in sa;
int sock, err, i;
int open_count = 0;
clock_t begin_time, end_time;
double scan_time;
struct timeval timeout;
begin_time = clock();
if (!resolve_hostname(hostname, &sa))
{
fprintf(stderr, RED "Err: cannot resolve hostname %s\n" RESET, hostname);
exit(2);
}
printf(BLUE "\nStarting scan on %s port %d-%d\n\n" RESET, hostname, start_port, end_port);
timeout.tv_sec = 0;
timeout.tv_usec = 100000;
for (i = start_port; i <= end_port; i++)
{
printf("Scanning port %i", i);
fflush(stdout);
sa.sin_port = htons(i);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror("Socket creation failed");
exit(1);
}
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout);
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof timeout);
err = connect(sock, (struct sockaddr*)&sa, sizeof sa);
if (err < 0)
{
printf("\r \r");
} else
{
printf("\r" GREEN "Port %i is open" RESET "\n", i);
open_count++;
}
close(sock);
}
end_time = clock();
scan_time = (double)(end_time - begin_time) / CLOCKS_PER_SEC;
display_results(open_count, end_port - start_port + 1, scan_time);
}
int main(int argc, char **argv)
{
char hostname[100];
int start, end;
printf(BLUE "=== Simple port scanner ===\n" RESET);
printf("Enter hostname or IP: ");
fgets(hostname, sizeof(hostname), stdin);
hostname[strcspn(hostname, "\n")] = 0;
printf("Enter start port number: ");
scanf("%d", &start);
printf("Enter end port number: ");
scanf("%d", &end);
scan_port_range(hostname, start, end);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment