#include #include #include #include #include #include #include #include #include #define TRUE 1 #define FALSE 0 void cleanup(int shut, int socket); int sock_1; /* socket */ static struct sockaddr_in sock2_name; /* Address struct for socket2.*/ struct hostent hostentstruct; /* Storage for hostent data. */ struct hostent *hostentptr; /* Pointer to hostent data. */ int flag; int retval; /* helpful for debugging */ int shut = FALSE; /* flag to cleanup */ char *pass = "",*admin = "",*nas = ""; short port; char login [] = "login:", password [] = "Password:", prompt [] = "> ", show_session [] = "show session\r", crlf[] = "\r\n", press [] = "-- Press Return for More --"; char io_buf [8192]; void login_at_nas (void) { char *cp; short len; for(cp = io_buf,len = 0;;cp += len) { if ( 0 > (len = recv(sock_1,cp,sizeof(io_buf) - (cp-io_buf),0)) ) { perror("recv login"); cleanup(shut, sock_1); } // fprintf(stdout,"%.*s",len,cp); *(cp+len) = '\0'; if ( strstr(io_buf,login) ) { send(sock_1,admin,strlen(admin),0); send(sock_1,crlf,2,0); break; } } for(cp = io_buf,len = 0;;cp += len) { if ( 0 > (len = recv(sock_1,cp,sizeof(io_buf) - (cp-io_buf),0)) ) { perror("recv Password"); cleanup(shut, sock_1); } // fprintf(stdout,"%.*s",len,cp); *(cp+len) = '\0'; if ( strstr(io_buf,password) ) { send(sock_1,pass,strlen(pass),0); send(sock_1,crlf,2,0); break; } } for(cp = io_buf,len = 0;;cp += len) { if ( 0 > (len = recv(sock_1,cp,sizeof(io_buf) - (cp-io_buf),0)) ) { perror("recv login"); cleanup(shut, sock_1); } // fprintf(stdout,"%.*s",len,cp); *(cp+len) = '\0'; if ( (*(cp+len - 2) == '>') && (*(cp+len - 1) == ' ') ) { send(sock_1,show_session,sizeof(show_session)-1,0); break; } } } void dump_nas_out (void) { char *cp; short len; for(cp = io_buf,len = 0;;cp += len) { if ( 0 > (len = recv(sock_1,cp,sizeof(io_buf) - (cp-io_buf),0)) ) { perror("recv"); cleanup(shut, sock_1); } fprintf(stdout,"%.*s",len,cp); *(cp+len) = '\0'; if ( (*(cp+len - 2) == '>') && (*(cp+len - 1) == ' ') ) break; if ( strstr(io_buf,press) ) { send(sock_1,crlf,2,0); fprintf(stdout,"\n"); cp = io_buf; len = 0; continue; } } } /*--------------------------------------------------------------------*/ main(int argc, char **argv) { char *cp0,*cp1; /* * Check input parameters. */ if (argc != 4 ) { fprintf(stdout,"Usage: rwho pass:admin port nas.\n"); exit(EXIT_FAILURE); } if ( !(cp0 = strchr(argv[1],':')) ) { fprintf(stdout,"Usage: rwho pass:admin port nas.\n"); exit(EXIT_FAILURE); } pass = argv[1]; *cp0 = '\0'; admin = (++cp0); port = atoi(argv[2]); nas = argv[3]; /* if ( 4 != sscanf(argv[1],"%[^:]:%[\!^@]@%[^:]:%d",pass,admin,nas,&port) ) { fprintf(stdout,"Usage: rwho pass:admin port nas.\n"); exit(EXIT_FAILURE); } */ // fprintf(stdout,"\nProcessing at '%s':'%s'@'%s':'%d'\n",pass,admin,nas,port); /* * Open socket 1: AF_INET, SOCK_STREAM. */ if ((sock_1 = socket (AF_INET, SOCK_STREAM, 0)) == -1) { perror( "socket"); exit(EXIT_FAILURE); } /* *Get pointer to network data structure for socket 2 (remote host). */ if ( !(hostentptr = gethostbyname (nas)) ) { perror( "gethostbyname"); cleanup(shut, sock_1); } /* * Copy hostent data to safe storage. */ hostentstruct = *hostentptr; /* * Fill in the name & address structure for socket 2. */ sock2_name.sin_family = hostentstruct.h_addrtype; sock2_name.sin_port = htons(port); sock2_name.sin_addr = * ((struct in_addr *) hostentstruct.h_addr); /* * Connect socket 1 to sock2_name. */ retval = connect(sock_1, (struct sockaddr *)&sock2_name, sizeof (sock2_name)); if (retval) { perror("connect"); cleanup(shut, sock_1); } login_at_nas(); dump_nas_out(); cleanup(shut, sock_1); } /*-----------------------------------------------------------*/ void cleanup(int shut, int socket) { int retval; /* * Shutdown socket completely -- only if it was connected */ if (shut) { retval = shutdown(socket,2); if (retval == -1) perror ("shutdown"); } /* * Close socket. */ retval = close (socket); if (retval) perror ("close"); exit(EXIT_SUCCESS); } /* end main */