PHP Telnet Users Manual

by Antone Roundy

Requirements

PHP Telnet requires PHP version 4.3 or higher, with support for fsockopen enabled.

Security

I strongly recommend that you do not store your username and password in a PHP script unless only people you trust completely have access to your server. Instead, use a form to enter the username and password each time the script is run. Exercise caution with any system that transmits passwords over unencrypted connections.

Installation

To install PHP Telnet, simply upload it to your webserver.

Usage

The normal usage pattern for PHP Telnet is:

  1. Load PHP Telnet
  2. Create a PHPTelnet object
  3. Establish a telnet connection
  4. Send commands and process their results as desired
  5. Disconnect
Basic usage
<?php
require_once "PHPTelnet.php";

$telnet = new PHPTelnet();

// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
$result = $telnet->Connect('www.somewhere.com','login name','password');

if ($result == 0) {
$telnet->DoCommand('enter command here', $result);
// NOTE: $result may contain newlines
echo $result;
$telnet->DoCommand('another command', $result);
echo $result;
// say Disconnect(0); to break the connection without explicitly logging out
$telnet->Disconnect();
}
?>


Display your own error messages
<?php
require_once "PHPTelnet.php";

$telnet = new PHPTelnet();
$telnet->show_connect_error=0;

// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
$result = $telnet->Connect('www.somewhere.com','login name','password');

switch ($result) {
case 0:
$telnet->DoCommand('enter command here', $result);
// NOTE: $result may contain newlines
echo $result;
$telnet->DoCommand('another command', $result);
echo $result;
// say Disconnect(0); to break the connection without explicitly logging out
$telnet->Disconnect();
break;
case 1:
echo '[PHP Telnet] Connect failed: Unable to open network connection';
break;
case 2:
echo '[PHP Telnet] Connect failed: Unknown host';
break;
case 3:
echo '[PHP Telnet] Connect failed: Login failed';
break;
case 4:
echo '[PHP Telnet] Connect failed: Your PHP version does not support PHP Telnet';
break;
}
?>