- Published on
Port Status Checker Script in C Language
- Authors
- Name
- Ruan Bekker
- @ruanbekker
This is a simple script in the C Programming Language to test the port status of a remote address.
Requirements:
You will need the gcc package to compile the program:
For RHEL based distro's:
$ yum install gcc -y
For Debian based distro's:
$ apt install gcc -y
Check TCP Port Status in C Language:
Our file: test.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main(int argc, char *argv[]) {
int portno = 443;
char *hostname = "google.com";
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
error("Error opening socket\n");
}
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("Port is Closed\n");
} else {
printf("Port is Open\n");
}
close(sockfd);
return 0;
}
Compile:
Compile using gcc:
$ gcc -o test test.c
Execute:
Execute the script:
$ ./test
Port is Open
Thank You
Thanks for reading, if you like my content, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.
- Linktree: https://go.ruan.dev/links
- Patreon: https://go.ruan.dev/patreon