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:

March 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Mar 1st
    16 Files
  • 2
    Mar 2nd
    0 Files
  • 3
    Mar 3rd
    0 Files
  • 4
    Mar 4th
    32 Files
  • 5
    Mar 5th
    28 Files
  • 6
    Mar 6th
    42 Files
  • 7
    Mar 7th
    17 Files
  • 8
    Mar 8th
    13 Files
  • 9
    Mar 9th
    0 Files
  • 10
    Mar 10th
    0 Files
  • 11
    Mar 11th
    15 Files
  • 12
    Mar 12th
    19 Files
  • 13
    Mar 13th
    21 Files
  • 14
    Mar 14th
    38 Files
  • 15
    Mar 15th
    15 Files
  • 16
    Mar 16th
    0 Files
  • 17
    Mar 17th
    0 Files
  • 18
    Mar 18th
    10 Files
  • 19
    Mar 19th
    32 Files
  • 20
    Mar 20th
    46 Files
  • 21
    Mar 21st
    16 Files
  • 22
    Mar 22nd
    13 Files
  • 23
    Mar 23rd
    0 Files
  • 24
    Mar 24th
    0 Files
  • 25
    Mar 25th
    12 Files
  • 26
    Mar 26th
    31 Files
  • 27
    Mar 27th
    19 Files
  • 28
    Mar 28th
    42 Files
  • 29
    Mar 29th
    0 Files
  • 30
    Mar 30th
    0 Files
  • 31
    Mar 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