The blue code below is all that's needed to run the show. It is written in ANSI C and should compile, and run, on any POSIX compliant operating system (which includes Windows NT and derivatives).
#include <stdio.h>
#include <stdlib.h>
const char * env_var = "SSH_CONNECTION";
int main (int argc, char * argv[]) {
char * path = getenv(env_var);
printf("%c[%d;%d;%dm", 0x1B, 1, 31, 40); // color output red
if (path == NULL) {
printf("This is NOT an SSH session.\n");
fflush(stdout);
#ifdef FIREFOX
printf("Automatically launching Mozilla Firefox...");
system("add webbrowsers && firefox &");
#endif
printf("%c[%d;%d;%dm", 0x1B, 0, 37, 40); // reset colors
return EXIT_FAILURE;
}
char * ip_address = calloc (sizeof(char), 16);
char * path_p = path;
char * ip_address_p = ip_address;
while ( * path_p != ' ' && * path_p != '\0') {
* ip_address_p = * path_p;
ip_address_p++;
path_p++;
}
printf("You are remotely logged in from %s.\n", ip_address);
fflush(stdout);
free(ip_address);
printf("%c[%d;%d;%dm", 0x1B, 0, 37, 40); // reset colors
return EXIT_SUCCESS;
}
This program tries to open the shells $SSH_CONNECTION environment variable. If there is no such variable, we can safely assume that this session is not being conducted over SSH. I wrote this program to automatically launch Firefox, but to do so, the code must be compiled with -DFIREFOX.
The return values were structured to make shell scripting with this program possible.
Now proceed to the Makefile page to compile and install this program.