1. Go to this page and download the library: Download skrip42/php-telnet library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
skrip42 / php-telnet example snippets
.....
use Skrip42\Telnet\Client;
.....
$client = new Client($ip); //create client
$client->setPromtPattern('~\w+(>|#)$~'); //add await pattern
$client->login($login, $password); //call login procedure
$data = $client->sendMessage($telnetCommand); //set command and get response
$sequense = new CommandSequence(); //create command sequence
$sequense
->addCommand(Command::DO, Option::SUPPRESS_GO_AHEAD) //add command
->addCommand(Command::WILL, Option::ECHO); //add command
->addOption(Option::TERMINAL_TYPE, 'x-term') //add option
$this->sendSequence($sequense); //send command sequence
$sequense = new CommandSequence(); //create awaiting sequence
$sequense->addCommand(Command::DONT, Option::X_DISPLAY_LOCATION); //add command
$this->awaitSequence($sequense); //await specific sequence
$sequense = new CommandSequence(); //create command
$sequence->addText('enable'); //add text command
$this->sendSequence($sequense); //send command sequence
$result = $this->awaitPrompt('~User>~'); //awaint 'User>' string and get result
//you can set Psr\Log\LoggerInterface to client constructor;
$client = new Clent($ip, $port, $timelimit, $logger);
//or set it at any time
$client->setLogger($logger);
Client {
/**
* @param string $ip telnet host ip
* @param int $port telnet host port
* @param int $timelimit max awaiting time (ms)
* @param LoggerInterface $logger client logger instance
*/
public function __construct(string $ip, int $port = 23, int $timelimit = 100, Psr\Log\LoggerInterface $logger);
/**
* set default await pattern for all requests
*
* @param string $promtPattern set/change default await pattern
*/
public function setPromtPattern(string $promtPattern) : self;
/**
* set psr logger for client
*
* @param LoggerInterface $logger set/change default logger instance
*/
public function setLogger(Psr\Log\LoggerInterface $logger) : self;
/**
* execute login procedure
*
* @param string $login user login
* @param string $password user password
* @param string $promtPattern await pattern (optional)
*
* @return CommandSequence (see Skrip42\Telet\Components\CommandSequence class synopsis)
*/
public function login(string $login, string $password, string $promtPattern = null) : Skrip42\Telnet\Components\CommandSequence;
/**
* hight level method for send telnet command
*
* @param string $message telnet command string
* @param string $promtPattern await pattern (optional)
* @param int $timelimit max awaiting time (optional)
*
* @return string
*/
public function sendMessage(string $message, string $promtPattern = null, int $timelimit = null) : string;
/**
* send commands
*
* @param CommandSequence $swquence (see Skrip42\Telet\Components\CommandSequence class synopsis)
*/
public function sendSequence(Skrip42\Telnet\Components\CommandSequence $sequence);
/**
* await specific command sequence from response and return response command sequence
*
* @param CommandSequence $sequence (see Skrip42\Telet\Components\CommandSequence class synopsis)
* @param int $timelimit max awaiting time (optional)
*
* @return CommandSequence (see Skrip42\Telnet\Components\CommandSequence class synopsis)
*/
public function awaitSequence(Skrip42\Telnet\Components\CommandSequence $sequence, int $timelimit = null) : Skrip42\Telnet\Components\CommandSequence;
/**
* await specific promt text from response and return response command sequence
*
* @param string $promtPattern await pattern (optional)
* @param int $timelimit max awaiting time (optional)
*
* @return CommandSequence (see Skrip42\Telnet\Components\CommandSequence class synopsis)
*/
public function awaitPrompt(string $promtPattern = null, int $timeLimit = null) : Skrip42\Telnet\Components\CommandSequence;
}
CommandSequence {
/**
* @param string $rawByteString if define prepare raw string to command sequence (optional)
*/
public function __construct(string $rawByteString = null);
/**
* add command to sequence
*
* @param int $command telnet command (see Skrip42\Telnet\Components\Command constants)
* @param int $options telnet options (see Skrip42\Telnet\Components\Command constants)
*/
public function addCommand(int $command, int $option = null) : self;
/**
* add raw byte string to sequence
*
* @param ...$parts one or more raw byte strings
*/
public function addText(...$parts) : self;
/**
* add option to sequence
*
* @param int $options telnet options (see Skrip42\Telnet\Components\Command constants)
* @param string $data option data
*/
public function addOption(int $option, string $data) : self;
/**
* get all text parts from sequence
*/
public function getText() : string;
/**
* dump sequence
*/
public function dump() : string;
/**
* compile sequence to one byte string
*/
public function compile() : string;
}