Advertisement
Guest User

Untitled

a guest
May 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. /*
  2.  * exec command
  3.  * Author: Stefano Viola ---> Esteban Sannin
  4.  *
  5.  *
  6.  *
  7.  *
  8.  *
  9.  */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include "monitor.h"
  14. #include <time.h>
  15.  
  16.  
  17. int main(int argc, char *argv[], char *envp[]){
  18.         //Genera la pagina html
  19.   printf("Content-type: text/html\n\n"
  20.      "<html>\n"
  21.      "<head>\n"
  22.      "<title>Monitoring Router</title>\n"
  23.      "</head>\n"
  24.      "<body>\n");  
  25.   stamp_command("Sistem:", "uname -a");
  26.   stamp_command("SERVER WEB:", "env | grep SERVER_SOFTWARE | cut -f 2 -d=");
  27.   stamp_command("Uptime:", "uptime");
  28.   stamp_command("Memory:", "free");
  29.   stamp_command("Disk usage:", "df");
  30.  // stamp_command("ifconfig:", "ifconfig");
  31.  
  32.   stamp_command("prova:", "./pippo.sh");
  33.  
  34.   int i;  
  35.   for (i = 0; i < 1; i++){      
  36.           int first_value = stamp_rate("ifconfig wlan0 | grep 'Byte RX' | cut -f 2 -d: | awk {'print $1'}");
  37.           sleep(2);
  38.           int second_value = stamp_rate("ifconfig wlan0 | grep 'Byte RX' | cut -f 2 -d: | awk {'print $1'}");
  39.          
  40.           printf("\nfirst: %d second: %d\n", first_value, second_value);
  41.           int difference = second_value-first_value;
  42.           float rate = (difference/2)/1024;
  43.           printf("Band Rate Download RX: %.2f\n\n",rate);
  44.          
  45.   }  
  46.  
  47.   // stamp_command("ENV:", "env");
  48.  
  49.  
  50.     printf("</body>");
  51.     return 0;
  52. }
  53.  
  54. int stamp_rate(char *string_command){
  55.         int byte = command_number(string_command);
  56.         return byte;
  57.  
  58. }
  59.  
  60. void stamp_command(char *string_title, char *string_command){
  61.  
  62.         printf("<p><b><font color='red'>%s</font></b></p>",string_title);
  63.         printf("<pre>");
  64.         command(string_command);
  65.         printf("</pre><br>");
  66.  
  67. }
  68.  
  69. //test for read number
  70. //
  71. int command_number(char *command){
  72.         FILE *fp;
  73.         char line[256];
  74.         int iRet = 0;
  75.         int number_byte;
  76.  
  77.         fp = popen(command, "r");
  78.         if (fp != NULL) {
  79.                 while (fgets(line, sizeof(line), fp)) {
  80.                       number_byte = atoi(line);
  81.                 }
  82.                 pclose(fp);
  83.                 iRet = 0;
  84.         } else {
  85.                 iRet = -1;
  86.         }
  87.         return number_byte;
  88. }
  89.  
  90.  
  91.  
  92. ///testing funzione per comandi da shell
  93. int command(char *command){
  94.  FILE *fp;
  95.  char line[256];
  96.  int iRet = 0;
  97.  fp = popen(command, "r");
  98.  
  99.  if (fp != NULL) {
  100.          while (fgets(line, sizeof(line), fp)) {
  101.                  printf("%s", line);
  102.          }
  103.          pclose(fp);
  104.          iRet = 0;
  105.  } else {
  106.          iRet = -1;
  107.  }
  108.  return iRet;
  109.                      
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement