📄 remctrl.c
字号:
/* * Embedded CLI Remote Control Utility * * ./software/ch9/emcli/remtest/remctrl.c * * mtj@cogitollc.com * */#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>#include <time.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#define MAX_LINE 200#define MAX_ARG 60#define CLI_PORT 9999/* * main() -- Test Client * * This application provides a script interface to the embedded CLI * through a standard stream socket. Commands are read from the * input file and emitted through the socket to the CLI. Respones * are then logged to the defined output file. * * Options that should be passed are: * * -t <target> -- target must be the host of the CLI (IP or FQDN) * -i <file> -- script filename (input) * -o <file) -- log file (output) * */int main( int argc, char *argv[] ){ FILE *infp=NULL, *outfp=NULL; char buffer[MAX_LINE+1]; char inputfile[MAX_ARG+1], outputfile[MAX_ARG+1], target[MAX_ARG+1]; time_t curTime; int c, in, sock=-1, done=0; struct sockaddr_in servaddr; time_t theTime; if (argc > 1) { while (1) { c = getopt(argc, argv, "i:o:t:h"); if (c == -1) { if (sock == -1) { printf("Must define a target\n"); exit(-1); } /* If the user does not specify an input file, use * standard input */ if ((infp = fopen(inputfile, "r")) == NULL) { infp = fdopen(0, "r"); } /* If the user does not specify an output file, use * standard output */ if ((outfp = fopen(outputfile, "w")) == NULL) { outfp = fdopen(1, "w"); } bzero((void *)&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(CLI_PORT); /* Try first as an IP address, then try an FQDN */ servaddr.sin_addr.s_addr = inet_addr(target); if (servaddr.sin_addr.s_addr == 0xffffffff) { struct hostent *hptr = (struct hostent *)gethostbyname(target); if (hptr == NULL) { printf("Can't find %s\n", target); exit(-1); } else { struct in_addr **addrs; addrs = (struct in_addr **)hptr->h_addr_list; memcpy(&servaddr.sin_addr, *addrs, sizeof(struct in_addr)); } } connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)); /* Look for the salutation (immediate +OK from the CLI) */ in = read(sock, buffer, MAX_LINE); if (strncmp(buffer, "+OK", 3)) { printf("Embedded CLI does not appear to be accessible\n"); exit(-1); } break; } else { switch(c) { case 'i': strcpy(inputfile, optarg); break; case 'o': strcpy(outputfile, optarg); break; case 't': strcpy(target, optarg); sock = socket(AF_INET, SOCK_STREAM, 0); break; case 'h': printf("\n\tremtest -t <target> [-i infile] [-o outfile]\n\n"); exit(-1); default: printf("unknown argument %c\n", c); exit(-1); } } } } else { printf("Must at least specify a target\n"); exit(-1); } while (!feof(infp) && !done) { if (fgets(buffer, MAX_LINE, infp) == NULL) break; if (buffer[0] == '#') continue; theTime = time(NULL); fprintf(outfp, "-- %s# %s", ctime(&theTime), buffer); if (!strncmp(buffer, "quit", 4) || !strncmp(buffer, "exit", 4)) { done=1; break; } /* Send command to the target */ in = strlen(buffer); write(sock, buffer, in); for ( ; ; ) { in = read(sock, buffer, MAX_LINE); if (in <= 0) { done = 1; break; } buffer[in] = 0; fwrite(buffer, in, 1, outfp); if ((strstr(buffer, "+OK") != NULL) || (strstr(buffer, "+ERR") != NULL)) break; } } close(sock); fclose(infp); fclose(outfp);}/* * Copyright (c) 2002 Charles River Media. All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, is hereby granted without fee provided * that the following conditions are met: * * 1. Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * 3. Neither the name of Charles River Media nor the names of * its contributors may be used to endorse or promote products * derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY CHARLES RIVER MEDIA AND CONTRIBUTERS * 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CHARLES * RIVER MEDIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARAY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -