Solved

Retrieving remote file information

Posted on 1997-07-07
2
143 Views
Last Modified: 2013-12-25
Is it possible to use PERL and/or CGI to query a remote server to retrieve file information?  Basically, I am looking to get the date created or last modified date of files on different servers.  If it is possible to do this, how is it done (please provide code if possible).  What information can you retrieve about remote files on servers you access anonymously?  Thanks for your assistance.  
0
Comment
Question by:mtob
2 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 100 total points
ID: 1828904
Sure, i'll give a ready routine to get file informations through use of the web server's HEAD method.

You can embedd this routine anywhere you need; just pass it the right parameters - this means you could put it into a program to invoke from the shell, or into a CGI.

Please check function return types and include files on your OS, since there may be slight differences.

--//--
#include <stdlib.h>
#include <stdio.h>
#include <string.h>     /* bzero() and bcopy() */
#include <netdb.h>      /* gethostbyname() */
#include <sys/socket.h> /* socket() and connect() */
#include <arpa/inet.h>  /* htons() */
#include <errno.h>      /* errno */

#define PORT 80

/* get_url()
 * answer: file to write to (must be already open for writing - it may be stdin)
 * host: e.g. "www.experts-exchange.com"
 * url : e.g. "/topics/comp/lang/c"
 * err_str: to eventually store an error message
 *
 * returns 0 if succesful, errno otherwise
 */

int get_url(FILE *answer, const char *host, const char *url, char err_str[])
{
  int sock;
  struct hostent *hep;
  struct sockaddr_in sai;
  FILE *infile, *outfile;
  char buff[BUFSIZ];

  if(!(hep = gethostbyname(host))) {
    strcpy(err_str, "gethostbyname error");
    return errno;
  }
  if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    strcpy(err_str, "socket error");
    return errno;
  }

  bzero((char *)&sai, sizeof(struct sockaddr_in));
  sai.sin_family = AF_INET;
  sai.sin_port = htons((unsigned short)PORT);
  bcopy(hep->h_addr, (char *)&(sai.sin_addr.s_addr), hep->h_length); /* note: could just be 'sai.sin_addr' */

  if(connect(sock, (struct sockaddr *)&sai, sizeof(sai)) == -1) {
    strcpy(err_str, "connect error");
    return errno;
  }

  /* fine: now we are connected */
  /* let's work with stdio */
  if(!(infile = fdopen(sock, "r"))) {
    strcpy(err_str, "fdopen(in) error");
    return errno;
  }
  if(!(outfile = fdopen(sock, "w"))) {
    strcpy(err_str, "fdopen(out) error");
    return errno;
  }

  /* now, the query: i'll leave error checking now on! */
  fprintf(outfile, "HEAD %s HTTP1.0\n", url);
  fflush(outfile); /* this is mandatory: outfile would buffer */

  while(fgets(buff, BUFSIZ, infile)) fputs(buff, answer);
  fflush(answer); /* this is not mandatory:) */

  /* cleanup */
  /* note: the two fclose's are also mandatory: */
  /* the file tables are not automagically deallocated */
  /* if you just close the socket (at least on my system), */
  /* and you could go out of resources if you loop */
  /* to connect to more than one site */
  fclose(infile); fclose(outfile);
  close(sock);

  return 0;
}
--//--

Cheers, julio

P.S. maybe this is worth more than 100 points...
0
 

Author Comment

by:mtob
ID: 1828905
You're right, it was worth more.  Thank you very much.  If you could help though, is there a PERL way of doing it.  I can do it through C if I have to but would much rather through PERL.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now