Suppose the following scenario:

  • you have a linux server on which your website is running
  • you use a windows environment to develop it
  • you want to see the results of the accesslog/errorlog/phplog in windows, not in linux.

In order to do this you need the following:

  • program listening on a port on your windows station (we can use netcat (nc) for this)
  • program that reads the log and sends the data to the windows station on the netcat port.

Let’s call the transfer program mytransfer.c. Create this file on the Linux machine:

Make sure you change the destination port and ip to fit your needs!

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main(int argc, char *argv[]) {
    char buf[1024];
    // Socket stuff
    int sockfd, portno, n;
    struct hostent *he;
    struct sockaddr_in their_addr;
    he=gethostbyname("THE.WINDOWS.IP.HERE");  // destination ip
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) printf("ERROR opening socket");
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons(39412); // port number
    their_addr.sin_addr = *((struct in_addr *)he->h_addr);
    memset(&(their_addr.sin_zero), '', 8);
    if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
        printf("Error connecting to remote host and port. Is netcat started on the destination machine?\n");
        exit(1);
    } else {
        printf("Connected!\n");
    }
    while (1) {
        fgets(buf, 1024, stdin);
        if (strlen(buf)>0) {
            write(sockfd, buf,strlen(buf));
            sleep(1);
        }
        memset(buf,0,sizeof(buf));
    }
    return 0;
}

On Windows, download and install netcat, and run it with the following command line:

nc -L -l -p 39412 > c:\php.log

Make sure you change the port number above with the one written in mytransfer.c. To run it on Linux:

killall -9 mytransfer.o;  gcc -o mytransfer.o mytransfer.c && \
tail -f /tmp/php.log | ./mytransfer.o

This means: compile the Linux program, then tail the /tmp/php.log into the input (STDIN) of mytransfer.o (the compiled program). mytransfer.o will get the data received through input and will forward it to the socket to which is connected. This way, whenever a character is added to /tmp/php.log, it will immediately be reflected in c:\php.log