elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Se me cierra mi apliacion de consola compilada con VS2010 ¿array?¿puntero?
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Se me cierra mi apliacion de consola compilada con VS2010 ¿array?¿puntero?  (Leído 3,128 veces)
Drakomorh

Desconectado Desconectado

Mensajes: 7


Ver Perfil
Se me cierra mi apliacion de consola compilada con VS2010 ¿array?¿puntero?
« en: 23 Mayo 2011, 00:57 am »

Hola amigos:

Estoy programando una aplicacion para el curro.

Utiliza la libreria pthread para crear hilos, y hacer una tarea de toma de datos de forma concurrente.

Utilliza la libreria Chilkatssh para conectarse a equipo por ssh y tomar datos.

despues formateo los datos y los imprimo en pantalla.

para ello declaro un array de punteros de tipo struck que he creado para almacenar todos los datos. este puntero es el que le paso a la funcion que crea los hilos y ejecuta mi funcion que se conecta a los equipos, toma los datos, y los guarda, para que en el main, los imprima por pantalla.

La cosa, es que funcionar funciona. paro al cabo de un rato, me sale el tipico erro de win diciendome que la aplicacion ha sufrido un error y ha de cerrarse.

alguna idea¿?

Código
  1. // LinkSample.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <string>
  9. using namespace std;
  10. #include <windows.h>
  11.  
  12. // This example does nothing more than include a number of Chilkat headers
  13. // and instantiating objects for the purpose of demonstrating a clean build
  14. // and link (i.e. there are no unresolved externals).
  15.  
  16. // If you didn't ready the README.html that is included with the download,
  17. // you should probably spend a few minutes reviewing it.  It tells what
  18. // Chilkat lib should be linked, depending on the "Runtime Library" setting
  19. // in your Project Properties --> C++ --> Code Generation settings.
  20. // It also tells what additional Microsoft libs need to be added to your
  21. // list of link libraries, such as ws2_32.lib, crypt32.lib, etc.
  22.  
  23. #include "../include/CkZip.h"
  24. #include "../include/CkFtp2.h"
  25. #include "../include/CkMailMan.h"
  26. #include "../include/CkXml.h"
  27. #include "../include/CkPrivateKey.h"
  28. #include "../include/CkRsa.h"
  29. #include "../include/CkHttp.h"
  30. #include "../include/CkMime.h"
  31. #include "../include/CkMht.h"
  32. #include "../include/CkRar.h"
  33. #include "../include/CkSsh.h"
  34. #include "../include/CkSshkey.h"
  35. #include "../include/CkSFtp.h"
  36. #include "../include/CkDkim.h"
  37. #include "../include/CkDirTree.h"
  38. #include "../include/CkRarEntry.h"
  39. #include <pthread.h>
  40.  
  41. #define Tamaño_arg 10000
  42. #define NUM_THREADS     30
  43. #define x_equipo_cero 0
  44. #define y_equipo_cero 4
  45. #define x_valor_cero 22
  46. #define y_ancho_equipo 6
  47. #define x_canal 29
  48. #define y_canal 2
  49. #define x_canal_actual 12
  50. #define y_error 80
  51. #define x_num_iteracion 15
  52. #define rojo 12
  53. #define verde 10
  54. #define neutro 7
  55. #define gris 112
  56.  
  57. /*
  58.  * estructura que sirve para que la función
  59.  * 'ConectaSsh' obtenga los
  60.  *  datos del equipo
  61.  */
  62. typedef struct outThread {
  63. char * hostname;
  64. int port;
  65. int numEquipo;
  66. int icanalAP;
  67. long iteracion;
  68. int phy;
  69. int thp;
  70. int per;
  71. int snr;
  72. char *mac;
  73. char *macap;
  74. int actualizado;
  75. int error;
  76. char *errormsg;
  77. } ot;
  78.  
  79.  
  80.  
  81.  
  82. void gotoxy(int x, int y)
  83. {
  84. COORD coord;
  85. coord.X = x;
  86. coord.Y = y;
  87. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  88. }
  89.  
  90. void ColorearValores(char*tipo,int valor)
  91. {
  92.  
  93. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  94.  
  95.  SetConsoleTextAttribute(hConsole, 7);
  96.  
  97. if ((strcmp(tipo,"PHY")== 0))
  98. {
  99.  
  100. if (valor <=24000)
  101. {
  102. SetConsoleTextAttribute(hConsole, rojo);
  103. }else if ((valor > 24000) && (valor <=36000))
  104. {
  105. SetConsoleTextAttribute(hConsole, neutro);
  106. }else if (valor >36000)
  107. {
  108. SetConsoleTextAttribute(hConsole, verde);
  109. }
  110.  
  111. }else if  (strcmp(tipo,"THP")== 0)
  112. {
  113. if (valor <=10000)
  114. {
  115. SetConsoleTextAttribute(hConsole, rojo);
  116. }else if ((valor > 10000) && (valor <=18000))
  117. {
  118. SetConsoleTextAttribute(hConsole, neutro);
  119. }else if (valor >18000)
  120. {
  121. SetConsoleTextAttribute(hConsole, verde);
  122. }
  123.  
  124. }else if  (strcmp(tipo,"PER")== 0)
  125. {
  126. if (valor <=29)
  127. {
  128. SetConsoleTextAttribute(hConsole, verde);
  129. }else if ((valor > 29) && (valor <=50))
  130. {
  131. SetConsoleTextAttribute(hConsole, neutro);
  132. }else if (valor >50)
  133. {
  134. SetConsoleTextAttribute(hConsole, rojo);
  135. }
  136.  
  137. }else if  (strcmp(tipo,"SNR")== 0)
  138. {
  139. if (valor <=17)
  140. {
  141. SetConsoleTextAttribute(hConsole, rojo);
  142. }else if ((valor > 17) && (valor <=27))
  143. {
  144. SetConsoleTextAttribute(hConsole, neutro);
  145. }else if (valor >27)
  146. {
  147. SetConsoleTextAttribute(hConsole, verde);
  148. }
  149.  
  150. }
  151. return;
  152. }
  153.  
  154.  
  155. void ImprimirValores(char *tipo, int valor,int numEquipo,int canalAP)
  156. {
  157.  
  158.  
  159. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  160.  
  161. if ((strcmp(tipo,"PHY")== 0))
  162. {
  163. gotoxy(x_valor_cero,y_equipo_cero + y_ancho_equipo*numEquipo);
  164. printf_s("PHY:");
  165. gotoxy(x_canal+(canalAP-1)*4 ,y_equipo_cero + y_ancho_equipo*numEquipo);
  166. printf_s( "  " );
  167. gotoxy(x_canal+(canalAP-1)*4 ,y_equipo_cero + y_ancho_equipo*numEquipo);
  168. ColorearValores("PHY",valor);
  169. printf_s( "%d\n", valor/1000 );
  170.  
  171. }else if  (strcmp(tipo,"THP")== 0)
  172. {
  173. gotoxy(x_valor_cero,y_equipo_cero +1 + y_ancho_equipo*numEquipo);
  174. printf_s("THP:");
  175. gotoxy(x_canal+(canalAP-1)*4 ,y_equipo_cero +1 + y_ancho_equipo*numEquipo);
  176. printf_s( "  " );
  177. gotoxy(x_canal+(canalAP-1)*4 ,y_equipo_cero +1 + y_ancho_equipo*numEquipo);
  178. ColorearValores("THP",valor);
  179. printf_s( "%d\n", valor/1000 );
  180.  
  181. }else if  (strcmp(tipo,"PER")== 0)
  182. {
  183. gotoxy(x_valor_cero,y_equipo_cero +2 + y_ancho_equipo*numEquipo);
  184. printf_s("PER:");
  185. gotoxy(x_canal+(canalAP-1)*4 ,y_equipo_cero +2 + y_ancho_equipo*numEquipo);
  186. printf_s( "  " );
  187. gotoxy(x_canal+(canalAP-1)*4 ,y_equipo_cero +2 + y_ancho_equipo*numEquipo);
  188. ColorearValores("PER",valor);
  189. printf_s( "%d\n", valor );
  190.  
  191. }else if  (strcmp(tipo,"SNR")== 0)
  192. {
  193. gotoxy(x_valor_cero,y_equipo_cero +3 + y_ancho_equipo*numEquipo);
  194. printf_s("SNR:");
  195. gotoxy(x_canal+(canalAP-1)*4 ,y_equipo_cero +3 + y_ancho_equipo*numEquipo);
  196. printf_s( "  " );
  197. gotoxy(x_canal+(canalAP-1)*4 ,y_equipo_cero +3 + y_ancho_equipo*numEquipo);
  198. ColorearValores("SNR",valor);
  199. printf_s( "%d\n", valor );
  200. }
  201.  
  202. SetConsoleTextAttribute(hConsole, neutro);
  203. return;
  204. }
  205. int ComprobarMac(char *NroHexa)
  206. {
  207. //char NroHexa[100];
  208. int i, Error;
  209. /**
  210. if((strlen(NroHexa)>19) || (strlen(NroHexa)<10))
  211. {
  212. Error = 1;
  213. return Error;
  214. }
  215. **/
  216. for( Error = 0, i = strlen(NroHexa) - 2; i>=0; i--)
  217. {
  218. if( !( ( NroHexa[i] >= 'A' && NroHexa[i] <= 'F' ) ||
  219. ( NroHexa[i] >= 'a' && NroHexa[i] <= 'f' ) ||
  220. ( NroHexa[i] >= '0' && NroHexa[i] <= '9' )  || ( NroHexa[i] >= ':' && NroHexa[i] <= ':' ) ))
  221. {
  222. Error = 1;
  223. break;
  224. }
  225. }
  226.  
  227. return Error;
  228. }
  229. int ComprobarCH(char *NroHexa)
  230. {
  231. //char NroHexa[100];
  232. int i, Error;
  233.  
  234. if((atoi(NroHexa)>13) || (atoi(NroHexa)<1))
  235. {
  236. Error = 1;
  237. return Error;
  238. }
  239.  
  240. for( Error = 0, i = strlen(NroHexa) - 2; i>=0; i--)
  241. {
  242. if( !(  NroHexa[i] >= '0' && NroHexa[i] <= '9'    ))
  243. {
  244. Error = 1;
  245. break;
  246. }
  247. }
  248.  
  249. return Error;
  250. }
  251. void* ConectaSsh(void *param)
  252. {
  253.    //  Important: It is helpful to send the contents of the
  254.    //  ssh.LastErrorText property when requesting support.
  255.  
  256. //obtener valores del argumento de entrada tipo ot
  257. ot *parametros;
  258. parametros = (ot *)param;
  259.  
  260. char * hostname = parametros->hostname;
  261. long port = parametros->port;
  262. int numEquipo = parametros->numEquipo;
  263. int icanalAP = parametros->icanalAP;
  264. parametros->error=1;
  265.  
  266. //printf_s("valor de parametros.hosname %s\n de parametros.port %d\n de parametros.numEquipo %d\n de parametros.icanalAP %d\n",parametros.hostname,parametros.port,parametros.numEquipo,parametros.icanalAP);
  267.  
  268.    CkSsh ssh;
  269. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  270. /**
  271. SetConsoleTextAttribute(hConsole, 14);
  272. gotoxy(17, 2);
  273. printf_s("%d", numEquipo +1);
  274. SetConsoleTextAttribute(hConsole, 7);
  275. **/
  276.  
  277.  
  278.  
  279.    //  Any string automatically begins a fully-functional 30-day trial.
  280.    bool success;
  281.    success = ssh.UnlockComponent("SSH12345678_738EFF5D8O0Z");
  282.    if (success != true) {
  283. parametros->error=1;
  284. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  285.        pthread_exit(NULL);
  286. return NULL;
  287.    }
  288.  
  289.    //  Connect to an SSH server:  
  290.  
  291.    success = ssh.Connect(hostname,port);
  292.    if (success != true) {
  293. parametros->error=1;
  294. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  295.        pthread_exit(NULL);
  296. return NULL;
  297.    }
  298.  
  299.  
  300.    //  Wait a max of 5 seconds when reading responses..
  301.    ssh.put_IdleTimeoutMs(10000);
  302.  
  303. //  Wait a max of 5 seconds when  responses..
  304. ssh.put_ConnectTimeoutMs(10000);
  305.  
  306.    //  Authenticate using login/password:
  307.  
  308. success = ssh.AuthenticatePw("chilkat", "myPassword");
  309.    if (success != true) {
  310. parametros->error=1;
  311. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  312.        pthread_exit(NULL);
  313. return NULL;
  314.    }
  315.  
  316.  
  317.  
  318.    //  Open a session channel.  (It is possible to have multiple
  319.    //  session channels open simultaneously.)
  320.    long channelNum;
  321.    channelNum = ssh.OpenSessionChannel();
  322.    if (channelNum < 0) {
  323. parametros->error=1;
  324. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  325.        pthread_exit(NULL);
  326. return NULL;
  327.    }
  328.  
  329.  
  330. //  Some SSH servers require a pseudo-terminal
  331.    //  If so, include the call to SendReqPty.  If not, then
  332.    //  comment out the call to SendReqPty.
  333.    //  Note: The 2nd argument of SendReqPty is the terminal type,
  334.    //  which should be something like "xterm", "vt100", "dumb", etc.
  335.    //  A "dumb" terminal is one that cannot process escape sequences.
  336.    //  Smart terminals, such as "xterm", "vt100", etc. process
  337.    //  escape sequences.  If you select a type of smart terminal,
  338.    //  your application will receive these escape sequences
  339.    //  included in the command's output.  Use "dumb" if you do not
  340.    //  want to receive escape sequences.  (Assuming your SSH
  341.    //  server recognizes "dumb" as a standard dumb terminal.)
  342.    const char * termType;
  343.    termType = "dumb";
  344.    long widthInChars;
  345.    widthInChars = 120;
  346.    long heightInChars;
  347.    heightInChars = 40;
  348.    //  Use 0 for pixWidth and pixHeight when the dimensions
  349.    //  are set in number-of-chars.
  350.    long pixWidth;
  351.    pixWidth = 0;
  352.    long pixHeight;
  353.    pixHeight = 0;
  354.    success = ssh.SendReqPty(channelNum,termType,widthInChars,heightInChars,pixWidth,pixHeight);
  355.    if (success != true) {
  356. parametros->error=1;
  357. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  358.        pthread_exit(NULL);
  359. return NULL;
  360.    }
  361.  
  362.    //  Start a shell on the channel:
  363.    success = ssh.SendReqShell(channelNum);
  364.    if (success != true) {
  365. parametros->error=1;
  366. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  367.        pthread_exit(NULL);
  368. return NULL;
  369.    }
  370.  
  371. //  Start a command in the remote shell. Login.
  372.    success = ssh.ChannelSendString(channelNum,"*****\n","ansi");
  373.    if (success != true) {
  374. parametros->error=1;
  375. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  376.        pthread_exit(NULL);
  377. return NULL;
  378.    }
  379.  
  380.  
  381. //Read untel we get thr prompt fo the password:  
  382.    success = ssh.ChannelReceiveUntilMatch(channelNum,"password :","ansi",true);
  383.    if (success != true) {
  384. parametros->error=1;
  385. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  386.        pthread_exit(NULL);
  387. return NULL;
  388.    }
  389.  
  390. // Display what we've received so far:
  391. const char * cmdOutputDebug;
  392.    cmdOutputDebug = ssh.getReceivedText(channelNum,"ansi");
  393.    if (cmdOutputDebug == 0 ) {
  394. parametros->error=1;
  395. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  396.        pthread_exit(NULL);
  397. return NULL;
  398.    }
  399. //printf_s("n%s\n",cmdOutputDebug);
  400.  
  401.  
  402. //  Start a command in the remote shell.  Password
  403.    success = ssh.ChannelSendString(channelNum,"********\n","ansi");
  404.    if (success != true) {
  405. parametros->error=1;
  406. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  407.        pthread_exit(NULL);
  408. return NULL;
  409.    }
  410.  
  411. // Display what we've received so far:
  412.    cmdOutputDebug = ssh.getReceivedText(channelNum,"ansi");
  413.    if (cmdOutputDebug == 0 ) {
  414. parametros->error=1;
  415. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  416.        pthread_exit(NULL);
  417. return NULL;
  418.    }
  419. //printf_s("%s\n",cmdOutputDebug);
  420.  
  421.  
  422. //Read untel we get thr prompt fo the rkcli:  
  423.    success = ssh.ChannelReceiveUntilMatch(channelNum,"rkscli:","ansi",true);
  424.    if (success != true) {
  425. parametros->error=1;
  426. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  427.        pthread_exit(NULL);
  428. return NULL;
  429.    }
  430.  
  431. // Display what we've received so far:
  432.    cmdOutputDebug = ssh.getReceivedText(channelNum,"ansi");
  433.    if (cmdOutputDebug == 0 ) {
  434. parametros->error=1;
  435. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  436.        pthread_exit(NULL);
  437. return NULL;
  438.    }
  439. //printf_s("%s\n",cmdOutputDebug);
  440.  
  441.  
  442. //obtener macs
  443.  
  444. //  Start a command in the remote shell.  chanel
  445.    success = ssh.ChannelSendString(channelNum,"get wlanlist\n","ansi");
  446.    if (success != true) {
  447. parametros->error=1;
  448. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  449.        pthread_exit(NULL);
  450. return NULL;
  451.    }
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.    //  Read whatever output may already be available on the
  459.    //  SSH connection.  ChannelReadAndPoll returns the number of bytes
  460.    //  that are available in the channel's internal buffer that
  461.    //  are ready to be "picked up" by calling GetReceivedText
  462.    //  or GetReceivedData.
  463.    //  A return value of -1 indicates failure.
  464.    //  A return value of -2 indicates a failure via timeout.
  465.  
  466.  
  467. //  The ChannelReadAndPoll method waits
  468.    //  for data to arrive on the connection usingi the IdleTimeoutMs
  469.    //  property setting.  Once the first data arrives, it continues
  470.    //  reading but instead uses the pollTimeoutMs passed in the 2nd argument:
  471.    //  A return value of -2 indicates a timeout where no data is received.
  472.    long n;
  473.    long pollTimeoutMs;
  474.    pollTimeoutMs = 2000;
  475.    n = ssh.ChannelReadAndPoll(channelNum,pollTimeoutMs);
  476.    if (n < 0) {
  477. parametros->error=1;
  478. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  479.        pthread_exit(NULL);
  480. return NULL;
  481.    }
  482.  
  483.  
  484. // Mostramos en que canal esta el ap:
  485. const char * macs = ssh.getReceivedText(channelNum,"ansi");
  486.    if (macs == 0 ) {
  487. parametros->error=1;
  488. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  489.        pthread_exit(NULL);
  490. return NULL;
  491.    }
  492. char *string1 =const_cast<char*> (macs);
  493. char seps[]   = " \t\n";
  494. char *token1 = NULL;
  495. char *next_token1 = NULL;
  496.  
  497.    // Establish string and get the first token:
  498.    token1 = strtok_s( string1, seps, &next_token1);
  499.  
  500.    // While there are tokens in "string1" or "string2"
  501. int bmac =1;
  502.    while ((token1 != NULL) && (bmac <=100))
  503.    {
  504.        // Get next token:
  505.        if (token1 != NULL)
  506.        {
  507.            token1 = strtok_s( NULL, seps, &next_token1);
  508. bmac++;
  509. if ((bmac == 14)&&(!ComprobarMac(token1))) parametros->mac = token1;
  510. if ((bmac == 19)&&(!ComprobarMac(token1))) parametros->macap = token1;
  511.        }
  512.    }
  513.  
  514.  
  515.  
  516. //obtener chanel
  517.  
  518. //  Start a command in the remote shell.  chanel
  519.    success = ssh.ChannelSendString(channelNum,"get channel svcp\n","ansi");
  520.    if (success != true) {
  521. parametros->error=1;
  522. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  523.        pthread_exit(NULL);
  524. return NULL;
  525.    }
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.    //  Read whatever output may already be available on the
  533.    //  SSH connection.  ChannelReadAndPoll returns the number of bytes
  534.    //  that are available in the channel's internal buffer that
  535.    //  are ready to be "picked up" by calling GetReceivedText
  536.    //  or GetReceivedData.
  537.    //  A return value of -1 indicates failure.
  538.    //  A return value of -2 indicates a failure via timeout.
  539.  
  540.  
  541. //  The ChannelReadAndPoll method waits
  542.    //  for data to arrive on the connection usingi the IdleTimeoutMs
  543.    //  property setting.  Once the first data arrives, it continues
  544.    //  reading but instead uses the pollTimeoutMs passed in the 2nd argument:
  545.    //  A return value of -2 indicates a timeout where no data is received.
  546.  
  547.    pollTimeoutMs = 2000;
  548.    n = ssh.ChannelReadAndPoll(channelNum,pollTimeoutMs);
  549.    if (n < 0) {
  550. parametros->error=1;
  551. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  552.        pthread_exit(NULL);
  553. return NULL;
  554.    }
  555.  
  556.  
  557. // Mostramos en que canal esta el ap:
  558. const char * canalAP = ssh.getReceivedText(channelNum,"ansi");
  559.    if (canalAP == 0 ) {
  560. parametros->error=1;
  561. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  562.        pthread_exit(NULL);
  563. return NULL;
  564.    }
  565. char *string2 =const_cast<char*> (canalAP);
  566.  
  567.    // Establish string and get the first token:
  568.    token1 = strtok_s( string2, seps, &next_token1);
  569.  
  570.    // While there are tokens in "string1" or "string2"
  571. int bChanel =1;
  572.    while ((token1 != NULL) && (bChanel <=5))
  573.    {
  574.        // Get next token:
  575.        if (token1 != NULL)
  576.        {
  577.            token1 = strtok_s( NULL, seps, &next_token1);
  578. bChanel++;
  579.        }
  580.    }
  581. if (!ComprobarCH(token1))canalAP = token1;
  582.  
  583.  
  584.  
  585.  
  586. //debug de canalAP
  587. /**
  588. gotoxy(x_equipo_cero ,y_equipo_cero +2 + y_ancho_equipo*numEquipo);
  589. printf_s( " CH actual:       " );
  590. SetConsoleTextAttribute(hConsole, 14);
  591. gotoxy(x_canal_actual ,y_equipo_cero +2 + y_ancho_equipo*numEquipo);
  592. printf_s( "%s", canalAP );
  593. SetConsoleTextAttribute(hConsole, 7);
  594. **/
  595.  
  596. //  Start a command in the remote shell.  valores
  597.    success = ssh.ChannelSendString(channelNum,"get station svcp stats all\n","ansi");
  598.    if (success != true) {
  599. parametros->error=1;
  600. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  601.        pthread_exit(NULL);
  602. return NULL;
  603.    }
  604.  
  605. // Display what we've received so far:
  606.    cmdOutputDebug = ssh.getReceivedText(channelNum,"ansi");
  607.    if (cmdOutputDebug == 0 ) {
  608. parametros->error=1;
  609. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  610.        pthread_exit(NULL);
  611. return NULL;
  612.    }
  613. //printf_s("%s\n",cmdOutputDebug);
  614.  
  615.  
  616.  
  617.  
  618.  
  619.    //  Read whatever output may already be available on the
  620.    //  SSH connection.  ChannelReadAndPoll returns the number of bytes
  621.    //  that are available in the channel's internal buffer that
  622.    //  are ready to be "picked up" by calling GetReceivedText
  623.    //  or GetReceivedData.
  624.    //  A return value of -1 indicates failure.
  625.    //  A return value of -2 indicates a failure via timeout.
  626.  
  627.  
  628. //  The ChannelReadAndPoll method waits
  629.    //  for data to arrive on the connection usingi the IdleTimeoutMs
  630.    //  property setting.  Once the first data arrives, it continues
  631.    //  reading but instead uses the pollTimeoutMs passed in the 2nd argument:
  632.    //  A return value of -2 indicates a timeout where no data is received.
  633.    //long n;
  634.    //long pollTimeoutMs;
  635.    pollTimeoutMs = 2000;
  636.    n = ssh.ChannelReadAndPoll(channelNum,pollTimeoutMs);
  637.    if (n < 0) {
  638. parametros->error=1;
  639. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  640.        pthread_exit(NULL);
  641. return NULL;
  642.    }
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.    //  Send an EOF.  This tells the server that no more data will
  650.    //  be sent on this channel.  The channel remains open, and
  651.    //  the SSH client may still receive output on this channel.
  652.    success = ssh.ChannelSendEof(channelNum);
  653.    if (success != true) {
  654. parametros->error=1;
  655. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  656.        pthread_exit(NULL);
  657. return NULL;
  658.    }
  659.  
  660.    //  Close the channel:
  661.    success = ssh.ChannelSendClose(channelNum);
  662.    if (success != true) {
  663. parametros->error=1;
  664. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  665.        pthread_exit(NULL);
  666. return NULL;
  667.    }
  668.  
  669.    //  Perhaps we did not receive all of the commands output.
  670.    //  To make sure,  call ChannelReceiveToClose to accumulate any remaining
  671.    //  output until the server's corresponding "channel close" is received.
  672.    success = ssh.ChannelReceiveToClose(channelNum);
  673.    if (success != true) {
  674. parametros->error=1;
  675. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  676.        pthread_exit(NULL);
  677. return NULL;
  678.    }
  679.  
  680.    //  Let's pickup the accumulated output of the command:
  681.    const char * cmdOutput;
  682.    cmdOutput = ssh.getReceivedText(channelNum,"ansi");
  683.    if (cmdOutput == 0 ) {
  684. parametros->error=1;
  685. parametros->errormsg = const_cast<char*> (ssh.lastErrorText());
  686.        pthread_exit(NULL);
  687. return NULL;
  688.    }
  689.  
  690.    //  Display the remote shell's command output:
  691. //printf_s("esto es los que me da despues de cerrar el chanel:\n%s\n",cmdOutput);
  692.  
  693. //Vamos a dividir la salida e imprimir solo lo que nos interesa:
  694.  
  695.  
  696.    string1 =const_cast<char*> (cmdOutput);
  697. token1 = NULL;
  698. next_token1 = NULL;
  699.  
  700.    // Establish string and get the first token:
  701.    token1 = strtok_s( string1, seps, &next_token1);
  702.  
  703.    // While there are tokens in "string1" or "string2"
  704.    while (token1 != NULL)
  705.    {
  706.        // Get next token:
  707.        if (token1 != NULL)
  708.        {
  709. //printf_s( "%s\n", token1 );
  710. token1 = strtok_s( NULL, seps, &next_token1);
  711.  
  712.  
  713. if  (strcmp(token1,"tx_rate")== 0)
  714. {
  715. token1 = strtok_s( NULL, seps, &next_token1);
  716. parametros->phy = atoi(const_cast<char*> (token1));
  717. }else if  (strcmp(token1,"tx_kbps")== 0)
  718. {
  719. token1 = strtok_s( NULL, seps, &next_token1);
  720. parametros->thp = atoi(const_cast<char*> (token1));
  721. }else if  (strcmp(token1,"tx_per")== 0)
  722. {
  723. token1 = strtok_s( NULL, seps, &next_token1);
  724. parametros->per = atoi(const_cast<char*> (token1));
  725. }else if  (strcmp(token1,"tx_rssi")== 0)
  726. {
  727. token1 = strtok_s( NULL, seps, &next_token1);
  728. parametros->snr = atoi(const_cast<char*> (token1));
  729. break;
  730. }
  731.  
  732.        }
  733.  
  734.  
  735.    }
  736.  
  737.  
  738. //Calcular la iteracion
  739. if (parametros->icanalAP!=atoi(canalAP) )
  740. {
  741. parametros->iteracion=0;
  742. }else parametros->iteracion=parametros->iteracion+1;
  743.  
  744.  
  745. //actualizar icanalAP
  746. parametros->icanalAP = atoi(canalAP) ;
  747.  
  748.  
  749.  
  750.  
  751. //  Disconnect
  752. ssh.Disconnect();
  753.  
  754. parametros->error=0;
  755.  
  756.  
  757.    return  NULL;
  758.  
  759.  
  760. }
  761.  
  762.  
  763. int main(int argc,char  *argv[])
  764. {
  765. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  766. pthread_t threads[NUM_THREADS];
  767.  
  768.  
  769.  
  770.    int port;
  771.  
  772.    //  Hostname may be an IP address or hostname:
  773. //char *hostname;
  774.    //hostname = "212.63.98.64";
  775.    port = 22;
  776.  
  777.  
  778. //Lo usare para el tema de los colores
  779. /**
  780. // you can loop x higher to see more color choices
  781. for(int x = 1; x < 255; x++)
  782. {
  783.            
  784.  // pick the colorattribute x you want
  785.  SetConsoleTextAttribute(hConsole, x);
  786.  printf_s("%d\tColorful text!",x);
  787. }
  788.  
  789. printf_s("compilado por mi\n");
  790.  
  791. system("pause");
  792.  
  793. return 0;
  794.  
  795. **/
  796.  
  797.  
  798. //Posible configuracion de salida
  799. printf_s("Vamos a estudiar los valores de los siguientes equipos:\n");
  800.  
  801. /**
  802. int i;
  803. printf_s("Se han pasado %3d argumentos:\n", argc);
  804. for(i=0; i<argc; i++) printf_s("%5d- %s\n", i, argv[i]);
  805. system("pause");
  806. return 0;
  807. **/
  808.  
  809.  
  810.  
  811. char  ips[Tamaño_arg];
  812. if (argc==1){
  813. printf_s( "Introduce las IPs de los equipos separadas por ';' :" );
  814. scanf_s("%s",ips);
  815. }else strcpy_s(ips,sizeof(ips),argv[1]);
  816. gotoxy(0,1);
  817. printf_s("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
  818.  
  819. gotoxy(0,y_canal);
  820. printf_s("Numero de equipos: ");
  821. gotoxy(x_canal,y_canal);
  822. SetConsoleTextAttribute(hConsole, 55);
  823. printf_s("01  02  03  04  05  06  07  08  09  10  11  12  13\n");
  824. SetConsoleTextAttribute(hConsole, 7);
  825. //por si hace falta getch(); no hace eco en la pantalla del caracter leido.
  826.  
  827.  
  828.  
  829.  
  830. /**
  831. //llamar al colasoft
  832. char  colasoft[1024];
  833. strcpy_s(colasoft,"\"%propramfiles%\\COLASO~1.1\\CPing.exe\" ");
  834. printf_s("%s",ips);c:\\
  835. strcat_s(colasoft,ips);
  836.  
  837.  
  838. printf_s("%s\n",colasoft);
  839. system(colasoft);
  840.  
  841.  
  842. printf_s("compilado por mi\n");
  843.  
  844. system("pause");
  845.  
  846. return 0;
  847.  
  848. **/
  849. //char *equipos[20];
  850.  
  851.  
  852. //obtener valores del argumento de entrada tipo ot
  853. //ot *ptr[NUM_THREADS] ;
  854. ot *ptr;
  855. ot parametros[NUM_THREADS];
  856. int rc;
  857.  
  858.  
  859.    //Inicio los del los equipos    
  860.    for (int i=0;i<NUM_THREADS; i++){
  861. parametros[i].icanalAP = 0;
  862. parametros[i].iteracion = 0;
  863. parametros[i].error = 0;
  864. parametros[i].mac ="";
  865. parametros[i].macap ="";
  866. }
  867.  
  868. while(true)
  869. {
  870. //ConectaSsh(hostname,port);
  871. char string1[Tamaño_arg];
  872. strcpy_s(string1,sizeof(string1),ips);
  873. char seps[]   = ",;:\t ";
  874. char *token1 = NULL;
  875. char *next_token1 = NULL;
  876. int maxNumEquipo=0;
  877. int numEquipo=0;
  878.  
  879. // Establish string and get the first token:
  880. token1 = strtok_s( string1, seps, &next_token1);
  881.  
  882. // While there are tokens in "string1" or "string2"
  883. while (token1 != NULL)
  884. {
  885.  
  886. // Get next token:
  887. if (token1 != NULL)
  888. {
  889.  
  890.  
  891. //Imprimir hostname y numequipo
  892. gotoxy(x_equipo_cero ,y_equipo_cero + y_ancho_equipo*numEquipo);
  893. SetConsoleTextAttribute(hConsole, 14);
  894. printf_s( "%d\n", numEquipo +1 );
  895. SetConsoleTextAttribute(hConsole, 7);
  896. gotoxy(x_equipo_cero +3,y_equipo_cero + y_ancho_equipo*numEquipo);
  897. printf_s( "%s\n", token1);
  898. gotoxy(x_equipo_cero, y_canal);
  899.  
  900.  
  901. ptr = &parametros[numEquipo];
  902.  
  903. if(parametros[numEquipo].error == 0)
  904. {
  905. parametros[numEquipo].hostname = token1;
  906. parametros[numEquipo].numEquipo = numEquipo;
  907. parametros[numEquipo].port = port;
  908. rc = pthread_create(&threads[numEquipo], NULL, ConectaSsh, (void *)ptr);
  909. if (rc){
  910. gotoxy(0,y_equipo_cero + y_ancho_equipo*(maxNumEquipo+1));
  911. printf_s("ERROR; return code from pthread_create() is %d\n",numEquipo+1);
  912. exit(-1);
  913. }
  914. }
  915. numEquipo++;
  916. token1 = strtok_s( NULL, seps, &next_token1);
  917. }
  918.  
  919.  
  920. if(maxNumEquipo<=numEquipo) maxNumEquipo=numEquipo;
  921.  
  922. }
  923.  
  924.  
  925. //Imprimir nº de equipos a estudiar
  926. SetConsoleTextAttribute(hConsole, 14);
  927. gotoxy(20, y_canal);
  928. printf_s("%d", maxNumEquipo );
  929. SetConsoleTextAttribute(hConsole, 7);
  930.  
  931. Sleep(10000);
  932.  
  933. //Imprimimos los valores
  934. for(int i =0; i<maxNumEquipo; i++)
  935. {
  936.  
  937.  
  938.  
  939. //Imprimir valroes
  940. if((parametros[i].error == 0 ) && parametros[i].icanalAP){
  941. ImprimirValores("PHY",parametros[i].phy,i,parametros[i].icanalAP);
  942. ImprimirValores("THP",parametros[i].thp,i,parametros[i].icanalAP);
  943. ImprimirValores("PER",parametros[i].per,i,parametros[i].icanalAP);
  944. ImprimirValores("SNR",parametros[i].snr,i,parametros[i].icanalAP);
  945.  
  946. //Imprimir ultimo canal y macs
  947. gotoxy(x_equipo_cero ,y_equipo_cero +2 + y_ancho_equipo*i);
  948. printf_s( " Ultimo CH:       " );
  949. SetConsoleTextAttribute(hConsole, 14);
  950. gotoxy(x_canal_actual ,y_equipo_cero +2 + y_ancho_equipo*i);
  951. printf_s( "%d", parametros[i].icanalAP );
  952. SetConsoleTextAttribute(hConsole, 7);
  953. gotoxy(x_equipo_cero ,y_equipo_cero +1 + y_ancho_equipo*i);
  954. if (!ComprobarMac(parametros[i].mac)) printf_s( " %s\n", parametros[i].mac );
  955. gotoxy(x_equipo_cero ,y_equipo_cero +3 + y_ancho_equipo*i);
  956. if (!ComprobarMac(parametros[i].macap)) printf_s( "AP %s\n", parametros[i].macap );
  957.  
  958. //Imprimir nº Iteracion
  959. gotoxy(x_num_iteracion ,y_equipo_cero +2 + y_ancho_equipo*i);
  960. printf_s( "%d", parametros[i].iteracion );
  961. gotoxy(20, y_canal);
  962.  
  963.  
  964. }
  965.  
  966.  
  967. //Imprimir erores
  968.  
  969. /**
  970. if(parametros[i].error){
  971. gotoxy(0,y_equipo_cero + y_ancho_equipo*(maxNumEquipo+1));
  972. printf_s("Error en el equipo %d:\n%s",i+1, parametros[i].errormsg);
  973. gotoxy(0,y_canal);
  974. }
  975. **/
  976. }
  977.  
  978.  
  979. /**
  980. gotoxy(0,y_ancho_equipo*(maxNumEquipo+1));
  981. printf_s("numEquipo %d\nlos valores de %s\ncanal %d\nIteracion %d\nerror: %d\nMacap %s\n%d\n%d\n%d\n%d",parametros[0].numEquipo,parametros[0].hostname, parametros[0].icanalAP,parametros[0].iteracion,parametros[0].error,parametros[0].macap,parametros[0].phy,parametros[0].thp,parametros[0].per,parametros[0].snr);
  982. gotoxy(0,y_ancho_equipo*(maxNumEquipo+1)+10);
  983. printf_s("numEquipo %d\nlos valores de %s\ncanal %d\nIteracion %d\nerror: %d\nMacap %s\n%d\n%d\n%d\n%d",parametros[1].numEquipo,parametros[1].hostname, parametros[1].icanalAP,parametros[1].iteracion,parametros[1].error,parametros[1].macap,parametros[1].phy,parametros[1].thp,parametros[1].per,parametros[1].snr);
  984. gotoxy(0,y_canal);
  985. **/
  986.  
  987. }
  988.  
  989.  
  990. pthread_exit(NULL);
  991. printf_s("compilado por mi\n");
  992.  
  993. system("pause");
  994.  
  995. return 0;
  996. }
  997.  
  998.  


He cambiado las credenciales por seguridad... pero si alguien viera donde pierdo los punteros o arrays... se que hay mucha cosa comentada en plan prueba

Gracias y un saludo.


« Última modificación: 23 Mayo 2011, 01:04 am por Drakomorh » En línea

Drakomorh

Desconectado Desconectado

Mensajes: 7


Ver Perfil
Re: Se me cierra mi apliacion de consola compilada con VS2010 ¿array?¿puntero?
« Respuesta #1 en: 23 Mayo 2011, 01:11 am »

Hola:

Mirando un poco por el foro he visto que se puede poner formato al código, para que se coloree, jejeje.

Es posible que se me cierre la aplicación por que en la cabecera he puesto como 3 librerías para string?


Gracias y un saludo.


En línea

Drakomorh

Desconectado Desconectado

Mensajes: 7


Ver Perfil
Re: Se me cierra mi apliacion de consola compilada con VS2010 ¿array?¿puntero?
« Respuesta #2 en: 23 Mayo 2011, 11:10 am »

Hola de nuevo:

El tema de usar las clases <iostring> y <vector>, haria mi aplicacion menos susceptible de estos cierres¿?

Gracias y un saludo.
En línea

Drakomorh

Desconectado Desconectado

Mensajes: 7


Ver Perfil
Re: Se me cierra mi apliacion de consola compilada con VS2010 ¿array?¿puntero?
« Respuesta #3 en: 23 Mayo 2011, 22:45 pm »

Hola amigos:

Creo que mas o menos he acotado el problema. pero no se como solucionarlo, pues creo que están bien definidos los punteros y arrays.

Parece que llegado a un punto. Los datos que guardo en
Código
  1. ot*parametros[MAX_THREADS]

Se corrompe.

Pero no tiene mucha logica, pues cada hilo solo trabaja con el puntero de tipo
Código
  1. ot parametros[numEquipo]

y en teoría no se deberían machacar... a alguien se le ocurre por que ocurre¿?

Gracias y un saludo.
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines