what you don't know can hurt you
Home Files News &[SERVICES_TAB]About Contact Add New

portchk.c

portchk.c
Posted Jan 11, 2000
Authored by Missinglnk | Site tribune.intranova.net

Simple port checker that either takes command-line input or file input and checks each host if a given port is open.

tags | tool, scanner
systems | unix
SHA-256 | c6196cb0030306034df4e5f018b488e74133893235f29df326bb4ff2de6299ec

portchk.c

Change Mirror Download
/*
* port checker for bob.
* - missnglnk@tribune.intranova.net
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/param.h>

#define MAXLEN 32768
extern int errno;

int
main(int argc, char **argv)
{
FILE *input;
int argswitch, fflag = 0, sflag = 0, sockfd, tport = -2,
verbose = 0;
char *ifile, *target, tmpdata[32768];
struct sockaddr_in rmt;
struct hostent *he;
unsigned long ip;

while ((argswitch = getopt(argc, argv, "f:h:p:v")) != -1) {
switch (argswitch) {
case 'f':
fflag = 1;
if (strlen(optarg) > MAXPATHLEN) {
printf("ERROR: Filename too long.\n");
return -1;
}
ifile = optarg;
break;
case 'h':
sflag = 1;
if (strlen(optarg) > MAXHOSTNAMELEN) {
printf("ERROR: Target hostname too long.\n");
return -1;
}
target = optarg;
break;
case 'p':
tport = atoi(optarg);
break;
case 'v':
verbose = 1;
break;
case '?':
default:
printf("port checker for bob.\n");
printf("%s [-f input file] [-p target port]\n", argv[0]);
printf("%s [-h single host] [-p target port]\n", argv[0]);
return -1;
break;
}
}

argc -= optind;
argv += optind;

if (sflag == 1 && fflag == 1) {
printf("ERROR: Cannot specify -f and -h together.\n");
return -1;
}
if (tport == -2) {
printf("ERROR: No port specified. (-p)\n");
return -1;
}
if (sflag == 1) {
if (verbose == 1) {
printf("Checking remote host %s\n", target);
printf("Checking port %d\n", tport);
}
if ((he = gethostbyname(target)) != NULL) {
ip = *(unsigned long *) he->h_addr;
} else if ((ip = inet_addr(target)) == NULL) {
if (verbose == 1) {
perror("Error resolving target");
}
return -1;
}
rmt.sin_family = AF_INET;
rmt.sin_addr.s_addr = ip;
rmt.sin_port = htons(tport);

if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
if (verbose == 1) {
perror("Error creating socket");
}
return -1;
}
if (connect(sockfd, (struct sockaddr *) & rmt, sizeof(rmt)) == 0) {
printf("%s\n", target);
} else {
if (verbose == 1) {
perror("Error connecting");
}
}

if (close(sockfd) < 0) {
if (verbose == 1) {
perror("Error closing socket");
}
return -1;
}
} else if (fflag == 1) {
if (verbose == 1) {
printf("Reading from %s\n", ifile);
}
if ((input = fopen(ifile, "r")) == NULL) {
perror("Error opening input file");
return -1;
}
if (fread(tmpdata, MAXLEN, 1, input) < 0) {
perror("Error reading from input file");
return -1;
}
for ((target = strtok(tmpdata, "\n")); target != NULL; ((target = strtok(NULL, "\n")) != NULL)) {
if (verbose == 1) {
printf("Checking remote host %s\n", target);
printf("Checking port %d\n", tport);
}
if ((he = gethostbyname(target)) != NULL) {
ip = *(unsigned long *) he->h_addr;
} else if ((ip = inet_addr(target)) == NULL) {
if (verbose == 1) {
perror("Error resolving target");
}
}
rmt.sin_family = AF_INET;
rmt.sin_addr.s_addr = ip;
rmt.sin_port = htons(tport);

if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
if (verbose == 1) {
perror("Error creating socket");
}
return -1;
}
if (connect(sockfd, (struct sockaddr *) & rmt, sizeof(rmt)) == 0) {
printf("%s\n", target);
} else {
if (verbose == 1) {
perror("Error connecting");
}
}

if (close(sockfd) < 0) {
if (verbose == 1) {
perror("Error closing socket");
}
return -1;
}
}

if (fclose(input) < 0) {
perror("Error closing input");
return -1;
}
} else {
printf("ERROR: No scan type specified, bob.\n");
return -1;
}

return 0;
}
Login or Register to add favorites

File Archive:

July 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Jul 1st
    27 Files
  • 2
    Jul 2nd
    10 Files
  • 3
    Jul 3rd
    35 Files
  • 4
    Jul 4th
    27 Files
  • 5
    Jul 5th
    18 Files
  • 6
    Jul 6th
    0 Files
  • 7
    Jul 7th
    0 Files
  • 8
    Jul 8th
    28 Files
  • 9
    Jul 9th
    44 Files
  • 10
    Jul 10th
    24 Files
  • 11
    Jul 11th
    25 Files
  • 12
    Jul 12th
    11 Files
  • 13
    Jul 13th
    0 Files
  • 14
    Jul 14th
    0 Files
  • 15
    Jul 15th
    28 Files
  • 16
    Jul 16th
    6 Files
  • 17
    Jul 17th
    34 Files
  • 18
    Jul 18th
    6 Files
  • 19
    Jul 19th
    34 Files
  • 20
    Jul 20th
    0 Files
  • 21
    Jul 21st
    0 Files
  • 22
    Jul 22nd
    0 Files
  • 23
    Jul 23rd
    0 Files
  • 24
    Jul 24th
    0 Files
  • 25
    Jul 25th
    0 Files
  • 26
    Jul 26th
    0 Files
  • 27
    Jul 27th
    0 Files
  • 28
    Jul 28th
    0 Files
  • 29
    Jul 29th
    0 Files
  • 30
    Jul 30th
    0 Files
  • 31
    Jul 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close