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] – created fachet | sdk:telnet-run-a-command-using-telnet [2015/05/06 12:50] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <code c> | + | ====== Run a command using Telnet ====== |
+ | <code c telnetcmd.are> | ||
/* DESC: This script runs a shell command using telnet. | /* DESC: This script runs a shell command using telnet. | ||
Line 10: | Line 11: | ||
string telnet(ip, port, user, password, cmd) | string telnet(ip, port, user, password, cmd) | ||
{ | { | ||
- | + | | |
- | if ((sock = socket(AF_INET, | + | if (connect(sock, |
- | if (connect(sock, | + | 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] == " |
- | if (buf[0] == " | + | { |
- | { | + | if (buf[1] == " |
- | if (buf[1] == " | + | if (send(sock, " |
- | if (send(sock, " | + | if (send(sock, " |
- | if (send(sock, " | + | } else { |
- | } else { | + | for (i = 0; i < strlen(buf); |
- | for (i = 0; i < strlen(buf); | + | if (buf[i] == " |
- | if (buf[i] == " | + | else if (buf[i] == " |
- | else if (buf[i] == " | + | 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, |
- | if (send(sock, strcat(user, | + | 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, |
- | if (send(sock, strcat(password, | + | while((s = select(sock, |
- | while((s = select(sock, | + | if (s == -1) throw NULL; //ignore all garbage |
- | if (s == -1) throw NULL; //ignore all garbage | + | if (send(sock, strcat(cmd, " |
- | if (send(sock, strcat(cmd, " | + | rbuf =""; |
- | rbuf =""; | + | while((s = select(sock, |
- | while((s = select(sock, | + | if (s == -1) throw NULL; |
- | if (s == -1) throw NULL; | + | else rbuf = strcat(rbuf, |
- | else rbuf = strcat(rbuf, | + | } |
- | } | + | rbuf = right(rbuf, strlen(rbuf) - strlen(cmd) - 2); //cut cmd |
- | rbuf = right(rbuf, strlen(rbuf) - strlen(cmd) - 2); //cut cmd | + | if (pos = strrchr(rbuf, |
- | if (pos = strrchr(rbuf, | + | rbuf = left(rbuf, pos); |
- | rbuf = left(rbuf, pos); | + | throw rbuf; |
- | throw rbuf; | + | } catch (ret) { |
- | } catch (ret) { | + | close (sock); |
- | close (sock); | + | return ret; |
- | return ret; | + | } |
- | } | + | |
} | } | ||