I try to do a telnet script using diferent codes i find here, at the PHP website and other tring to connect to a server using telnet, do validation, and run a command getting the return. I dev over Win XP + Apache 2 + PHP 4.3. I start Telnet service to test aginst localhost machine. The final code is:
<?php
This is the difficult part, the Telnet header
$header1=chr(0xFF).chr(0xF.chr(0x1F).chr(0xFF).chr(0xF
.
chr(0x20).chr(0xFF).chr(0xF.chr(0x18).chr(0xFF).chr(0xF
.
chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xF.
chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
$header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
connecting
$fp=fsockopen("127.0.0.1",23);
sending the Telnet header
fputs($fp,$header1);
usleep(125000);
fputs($fp,$header2);
usleep(125000);
login
fputs($fp,"--my user name here--\r");
usleep(125000);
fputs($fp,"--my password here--\r");
usleep(125000);
execute a simple command that i can test if it do
fputs($fp,"dir > demo.txt\r");
usleep(125000);
we had to wait
usleep(125000);
show the output
do
{
$output.=fread($fp, 80);
// read line by line, or at least small chunks
$stat=socket_get_status($fp);
}
while($stat["unread_bytes"]);
$output = str_replace("\n", "<br>", $output);
echo $output;
fclose($fp);
?>
My intention is to connect by telnet, validate, run a command and test if the command is do it. This script returns like this:
每媒%每没每没每媒'每媒每媒
This returned string i can not undertand and i can not to do anything.
I test to do this (connect telnet, login, run a command) whit other script i find here but any return me results. It's posible this sample code is not valid to connect to windows plataform? Any idea to do this?
Thks.