This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
sdk:telnet-run-a-command-using-telnet [2015/03/02 10:22] fachet created |
sdk:telnet-run-a-command-using-telnet [2015/04/13 14:22] hess |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Run a command using Telnet ====== | ||
<code c> | <code c> | ||
Line 10: | Line 11: | ||
string telnet(ip, port, user, password, cmd) | string telnet(ip, port, user, password, cmd) | ||
{ | { | ||
- | + | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) return NULL; | |
- | if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) return NULL; | + | if (connect(sock, ip, port) < 0) return NULL; |
- | if (connect(sock, ip, port) < 0) return NULL; | + | try { |
- | try { | + | if ((buf = recv(sock)) == NULL || strlen(buf) < 3) throw NULL; |
- | if ((buf = recv(sock)) == NULL || strlen(buf) < 3) throw NULL; | + | buf = explode(buf); |
- | buf = explode(buf); | + | if (buf[0] == "\xff") // is command |
- | if (buf[0] == "\xff") // is command | + | { |
- | { | + | if (buf[1] == "\xfd" && buf[2] == "\d031") { //negotiate |
- | if (buf[1] == "\xfd" && buf[2] == "\d031") { //negotiate | + | if (send(sock, "\d255\d251\d031") == -1) throw NULL; |
- | if (send(sock, "\d255\d251\d031") == -1) throw NULL; | + | if (send(sock, "\d255\d250\d031\d000\d080\d000\d024\d255\d240") == -1) throw NULL; |
- | if (send(sock, "\d255\d250\d031\d000\d080\d000\d024\d255\d240") == -1) throw NULL; | + | } else { |
- | } else { | + | for (i = 0; i < strlen(buf); i++) |
- | for (i = 0; i < strlen(buf); i++) | + | if (buf[i] == "\xfd") buf[i] = "\xfc"; |
- | if (buf[i] == "\xfd") buf[i] = "\xfc"; | + | else if (buf[i] == "\xfb") buf[i] = "\xfd"; |
- | else if (buf[i] == "\xfb") buf[i] = "\xfd"; | + | if (send(sock, implode(buf)) == -1) throw NULL; |
- | if (send(sock, implode(buf)) == -1) throw NULL; | + | } |
- | } | + | } |
- | } | + | if ((buf = recv(sock)) == NULL || strlen(buf) < 1) throw NULL; // user ? |
- | if ((buf = recv(sock)) == NULL || strlen(buf) < 1) throw NULL; // user ? | + | if (send(sock, strcat(user, "\r\n")) == -1) throw NULL; |
- | if (send(sock, strcat(user, "\r\n")) == -1) throw NULL; | + | if ((buf = recv(sock)) == NULL || strlen(buf) < 1)throw NULL; // password ? |
- | if ((buf = recv(sock)) == NULL || strlen(buf) < 1)throw NULL; // password ? | + | if (send(sock, strcat(password, "\r\n")) == -1) throw NULL; |
- | if (send(sock, strcat(password, "\r\n")) == -1) throw NULL; | + | while((s = select(sock, 1)) > 0 && (buf = recv(sock)) != NULL && strlen(buf) > 0) |
- | while((s = select(sock, 1)) > 0 && (buf = recv(sock)) != NULL && strlen(buf) > 0) | + | if (s == -1) throw NULL; //ignore all garbage |
- | if (s == -1) throw NULL; //ignore all garbage | + | if (send(sock, strcat(cmd, "\r\n")) == -1) throw NULL; //send command |
- | if (send(sock, strcat(cmd, "\r\n")) == -1) throw NULL; //send command | + | rbuf =""; |
- | rbuf =""; | + | while((s = select(sock, 1)) > 0 && (buf = recv(sock)) != NULL && strlen(buf) > 0){ |
- | while((s = select(sock, 1)) > 0 && (buf = recv(sock)) != NULL && strlen(buf) > 0){ | + | if (s == -1) throw NULL; |
- | if (s == -1) throw NULL; | + | else rbuf = strcat(rbuf, buf); // collect return |
- | else rbuf = strcat(rbuf, buf); // collect return | + | } |
- | } | + | rbuf = right(rbuf, strlen(rbuf) - strlen(cmd) - 2); //cut cmd |
- | rbuf = right(rbuf, strlen(rbuf) - strlen(cmd) - 2); //cut cmd | + | if (pos = strrchr(rbuf, "\r")) |
- | if (pos = strrchr(rbuf, "\r")) | + | rbuf = left(rbuf, pos); |
- | rbuf = left(rbuf, pos); | + | throw rbuf; |
- | throw rbuf; | + | } catch (ret) { |
- | } catch (ret) { | + | close (sock); |
- | close (sock); | + | return ret; |
- | return ret; | + | } |
- | } | + | |
} | } | ||