Skip to content

Instantly share code, notes, and snippets.

@fffaraz
Last active December 17, 2023 07:56
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save fffaraz/7a7e61cca97dfa27f9ac2cda4e0296eb to your computer and use it in GitHub Desktop.
Save fffaraz/7a7e61cca97dfa27f9ac2cda4e0296eb to your computer and use it in GitHub Desktop.
Port scanner code in c
#include "stdio.h"
#include "sys/socket.h"
#include "errno.h"
#include "netdb.h"
#include "string.h"
#include "stdlib.h"
int main(int argc , char **argv)
{
struct hostent *host;
int err, i , sock ,start , end;
char hostname[100];
struct sockaddr_in sa;
//Get the hostname to scan
printf("Enter hostname or IP : ");
gets(hostname);
//Get start port number
printf("\nEnter start port number : ");
scanf("%d" , &start);
//Get end port number
printf("Enter end port number : ");
scanf("%d" , &end);
//Initialise the sockaddr_in structure
strncpy((char*)&sa , "" , sizeof sa);
sa.sin_family = AF_INET;
//direct ip address, use it
if(isdigit(hostname[0]))
{
printf("Doing inet_addr...");
sa.sin_addr.s_addr = inet_addr(hostname);
printf("Done\n");
}
//Resolve hostname to ip address
else if( (host = gethostbyname(hostname)) != 0)
{
printf("Doing gethostbyname...");
strncpy((char*)&sa.sin_addr , (char*)host->h_addr , sizeof sa.sin_addr);
printf("Done\n");
}
else
{
herror(hostname);
exit(2);
}
//Start the port scan loop
printf("Starting the portscan loop : \n");
for( i = start ; i <= end ; i++)
{
//Fill in the port number
sa.sin_port = htons(i);
//Create a socket of type internet
sock = socket(AF_INET , SOCK_STREAM , 0);
//Check whether socket created fine or not
if(sock < 0)
{
perror("\nSocket");
exit(1);
}
//Connect using that socket and sockaddr structure
err = connect(sock , (struct sockaddr*)&sa , sizeof sa);
//not connected
if( err < 0 )
{
//printf("%s %-5d %s\r" , hostname , i, strerror(errno));
fflush(stdout);
}
//connected
else
{
printf("%-5d open\n", i);
}
close(sock);
}
printf("\r");
fflush(stdout);
return(0);
}
@bugsbunny7575
Copy link

does it work??

@weakiwi
Copy link

weakiwi commented Aug 25, 2018

is < and html encode error?
counldn't find where is it's definiation

@lchoua
Copy link

lchoua commented Oct 31, 2019

Hi, when I try to compilate this code the compiler throw me an error. It's says:

use of undeclared identifier 'amp

Could you explain what is amp please??

@NoS1gnalWeb
Copy link

Hi, when I try to compilate this code the compiler throw me an error. It's says:

use of undeclared identifier 'amp

Could you explain what is amp please??

i found the same code in a other website and it dated since 2013 so i think it doesnt work

@NikoJako
Copy link

Although I don't see the "amp" you're talking about. I believe this code is an attempt to correct the HTML encoding as seen here. https://www.binarytides.com/tcp-connect-port-scanner-c-code-linux-sockets/comment-page-1/?unapproved=243282&moderation-hash=b537728250f6e31ca1745b6518b20629#comment-243282

just delete "amp" and leave the "&" which is called an "amp"ersand.

@SUmidcyber
Copy link

Nice Bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment