<code c telnetcmd.are>
/* DESC: This script runs a shell command using telnet.
* Only the first response line is returned.
* Don't forget to enable the telnet server
* For special chars in command string use escape: apostrophe = "\x27"
* Copyright (C) 2014 NetModule AG, Switzerland, rfa
*/
string telnet(ip, port, user, password, cmd)
{
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) return NULL;
if (connect(sock, ip, port) < 0) return NULL;
try {
if ((buf = recv(sock)) == NULL || strlen(buf) < 3) throw NULL;
buf = explode(buf);
if (buf[0] == "\xff") // is command
{
if (buf[1] == "\xfd" && buf[2] == "\d031") { //negotiate
if (send(sock, "\d255\d251\d031") == -1) throw NULL;
if (send(sock, "\d255\d250\d031\d000\d080\d000\d024\d255\d240") == -1) throw NULL;
} else {
for (i = 0; i < strlen(buf); i++)
if (buf[i] == "\xfd") buf[i] = "\xfc";
else if (buf[i] == "\xfb") buf[i] = "\xfd";
if (send(sock, implode(buf)) == -1) throw NULL;
}