home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / lanserv / sadmin32 / sadmin32.c < prev    next >
C/C++ Source or Header  |  1998-10-01  |  6KB  |  162 lines

  1. /****************************************************************************/
  2. /*
  3.  *    PROGRAM NAME: SADMIN32
  4.  *    ------------
  5.  *
  6.  *    What this program does: This program executes a command on a remote
  7.  *    server. The command to be executed on the remote server is passed
  8.  *    as a command line parameter to this program but it must reside
  9.  *    on a disk visible to the target server.
  10.  *
  11.  *    REQUIRED FILES:
  12.  *    --------------
  13.  *    SADMIN32.C     -  Source code for this program
  14.  *
  15.  *    REQUIRED LIBRARIES:
  16.  *    ------------------
  17.  *    NETAPI32.LIB   -  Netapi library (in \IBMLAN\NETSRC\OS2\LIB directory)
  18.  *
  19.  *    NetAPI32 functions used in this program:
  20.  *    ---------------------------------------
  21.  *    Net32ServerAdminCommand
  22.  *
  23.  *    HOW TO COMPILE THIS PROGRAM:
  24.  *    ----------------------------
  25.  *    icc /Gt+ /DPURE_32 /C sadmin32.c
  26.  *
  27.  ****************************************************************************/
  28.  
  29. /*------- OS/2 include files -----------------------------------------------*/
  30. #define  INCL_BASE
  31. #include <os2.h>
  32.  
  33. /*------- NET APIs include files -------------------------------------------*/
  34. #include <neterr.h>
  35. #include <netcons.h>
  36. #include <server.h>
  37.  
  38. /*------- C include files --------------------------------------------------*/
  39. #include <string.h>
  40. #include <stdio.h>
  41.  
  42. VOID Error_Message(USHORT, PSZ);
  43.  
  44. /*------- Definitions ------------------------------------------------------*/
  45. #define  BUFLEN 10000                   /* output buffer size */
  46.  
  47. /****************************************************************************/
  48. /* MAIN C function                                                          */
  49. /*--------------------------------------------------------------------------*/
  50. VOID
  51. main(int argc, char *argv[])
  52. {
  53.     USHORT   usRc          = 0,       /* return code */
  54.              usCmdLen      = 0,       /* command line length */
  55.              usArgc;                  /* arguments counter */
  56.     ULONG    ulBytesRead   = 0,       /* number of bytes read */
  57.              ulTotalAvail  = 0;       /* available entries */
  58.     ULONG    sResult       = 0;       /* exit code of the executed command */
  59.     PCHAR    pcBuf;                   /* pointer to output buffer */
  60.     PCHAR    pcCommand;               /* pointer to command buffer */
  61.     CHAR     servername[UNCLEN+1];    /* server name */
  62.  
  63.     strupr(argv[0]);
  64.  
  65.     if (argc < 3)
  66.     {
  67.         printf("\nUsage: %s <\\\\targetservername> <commands>\n", argv[0]);
  68.         DosExit(EXIT_PROCESS, 1);
  69.     }
  70.  
  71.     strupr(argv[1]);
  72.     strcpy(servername, argv[1]);
  73.  
  74.     /* calculate the size of command buffer */
  75.     for (usArgc = 2; usArgc < argc ; usArgc++)
  76.     {
  77.         usCmdLen += strlen(argv[usArgc]) + 1;
  78.     }
  79.  
  80.     /* allocate command buffer */
  81.     usRc = DosAllocMem((PPVOID)&pcCommand,
  82.                        usCmdLen,
  83.                        PAG_READ | PAG_WRITE | PAG_COMMIT);
  84.  
  85.     /* Print a message and exit if buffer can't be allocated. */
  86.     if (usRc)
  87.     {
  88.         printf("SADMCMD: Unable to allocate memory.\n");
  89.         Error_Message(usRc, "DosAllocMem");
  90.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  91.     }
  92.  
  93.     /* Set the buffer to all zeroes. */
  94.     memset(pcCommand, '\x00', usCmdLen);
  95.  
  96.     /* fill command buffer with arguments from command line */
  97.     strcpy(pcCommand, argv[2]);
  98.  
  99.     for (usArgc = 3; usArgc < argc ; usArgc++)
  100.     {
  101.         strcat(pcCommand," ");
  102.         strcat(pcCommand, argv[usArgc]);
  103.     }
  104.  
  105.     /* allocate output buffer */
  106.     usRc = DosAllocMem((PPVOID)&pcBuf,
  107.                        BUFLEN,
  108.                        PAG_READ | PAG_WRITE | PAG_COMMIT);
  109.  
  110.     /* Print a message and exit if buffer can't be allocated. */
  111.     if (usRc)
  112.     {
  113.         printf("SADMCMD: Unable to allocate memory.\n");
  114.         Error_Message(usRc, "DosAllocMem");
  115.         (void)DosFreeMem((PVOID)pcCommand);
  116.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  117.     }
  118.  
  119.     /* Set the buffer to all zeroes. */
  120.     memset(pcBuf, '\x00', BUFLEN);
  121.  
  122.     printf("Command sent to %s is: %s\n",servername, pcCommand);
  123.  
  124.     /* call api function */
  125.     usRc = Net32ServerAdminCommand(servername,    /* name of remote server */
  126.                                    pcCommand,     /* command to be executed */
  127.                                    &sResult,      /* returned exit code */
  128.                                    pcBuf,         /* output buffer */
  129.                                    BUFLEN,        /* output buffer size */
  130.                                    &ulBytesRead,  /* num of bytes returned */
  131.                                    &ulTotalAvail);/* num of bytes available */
  132.  
  133.  
  134.     if (usRc == NERR_Success || usRc == ERROR_MORE_DATA)
  135.     {
  136.         if (usRc == ERROR_MORE_DATA)
  137.         {
  138.             printf("The output buffer is too small to hold all the "\
  139.                    "information generated by the \"%s\" command.\n\n",
  140.                    pcCommand);
  141.  
  142.             printf("\nBytesread = %d TotalAvail = %d\n",
  143.                     ulBytesRead, ulTotalAvail);
  144.         }
  145.  
  146.         /* Print information in output buffer. */
  147.         printf("Returned exit code = %d (HIGH NIBBLE IS ALWAYS 0)\n\n",
  148.                 sResult);
  149.  
  150.         *(pcBuf+BUFLEN-1) = '\0';    /* null-terminate output buffer */
  151.         printf("%s\n", pcBuf);
  152.     }
  153.  
  154.     if (usRc && usRc != ERROR_MORE_DATA)
  155.         Error_Message(usRc, "NetServerAdminCommand");
  156.  
  157.     /* Free memory and return. */
  158.     (void)DosFreeMem((PVOID)pcCommand);
  159.     (void)DosFreeMem((PVOID)pcBuf);
  160.     DosExit(EXIT_PROCESS, (ULONG)usRc);
  161. }
  162.