netgescon-day0/app/Console/Commands/PanasonicCtiProbeCommand.php

72 lines
2.8 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PanasonicCtiProbeCommand extends Command
{
protected $signature = 'cti:probe-panasonic
{--host=192.168.0.101 : Panasonic host/IP}
{--port=33333 : Panasonic CTI TCP port}
{--timeout=5 : Connect and read timeout in seconds}
{--send= : Optional ASCII payload to send after connect}
{--read-bytes=512 : Max bytes to read from socket}
{--hex : Print payload and response as hex dump}';
protected $description = 'Probe the Panasonic CTI TCP endpoint and print the first response bytes for protocol analysis.';
public function handle(): int
{
$host = (string) $this->option('host');
$port = max(1, (int) $this->option('port'));
$timeout = max(1, (int) $this->option('timeout'));
$send = (string) ($this->option('send') ?? '');
$readBytes = max(1, min(8192, (int) $this->option('read-bytes')));
$hex = (bool) $this->option('hex');
$this->line("Connessione a {$host}:{$port} con timeout {$timeout}s...");
$socket = @stream_socket_client("tcp://{$host}:{$port}", $errno, $errstr, $timeout);
if (! is_resource($socket)) {
$this->error("Connessione fallita ({$errno}): {$errstr}");
return self::FAILURE;
}
stream_set_timeout($socket, $timeout);
stream_set_blocking($socket, true);
$this->info('Connessione stabilita.');
if ($send !== '') {
fwrite($socket, $send);
$this->line($hex ? ('Payload inviato (hex): ' . strtoupper(bin2hex($send))) : ('Payload inviato: ' . $send));
}
$response = fread($socket, $readBytes);
$response = is_string($response) ? $response : '';
$meta = stream_get_meta_data($socket);
fclose($socket);
if ($response === '') {
$this->warn('Nessun banner o risposta immediata letta dalla socket.');
if (($meta['timed_out'] ?? false) === true) {
$this->line('La lettura è andata in timeout senza dati.');
}
$this->line('Nota: su Linux non possiamo implementare un TAPI/TSP nativo Panasonic senza specifica protocollo o middleware proprietario.');
return self::SUCCESS;
}
if ($hex) {
$this->line('Risposta (hex):');
$this->line(strtoupper(implode(' ', str_split(bin2hex($response), 2))));
}
$printable = preg_replace('/[^\P{C}\n\r\t]/u', '.', $response);
$this->line('Risposta (testo best-effort):');
$this->line($printable === '' ? '[vuota]' : $printable);
$this->line('Nota: su Linux non possiamo implementare un TAPI/TSP nativo Panasonic senza specifica protocollo o middleware proprietario.');
return self::SUCCESS;
}
}