Question Client Telnet Powershell "From scratch"

Plus d'informations
il y a 15 ans 2 mois #8396 par Arthur
Voici la première version de mon client Telnet.

C'est un client Telnet simple mais que je compte améliorer dans les semaines à venir. :)

Comme je le dit dans le titre il est en Full powershell et il est surtout très simple à utiliser il faut juste le script.

Pour le moment je ne l'est testé que sur le serveur Telnet de Windows 7, je n'est pas accès a une machine linux pour le moment, ca serait bien si quelqu'un pouvait le faire a ma place. :blush:

Pour ce qui est des améliorations je pense ajouter un système de session à la WinRM mais en encore plus cool.(B))Je compte aussi le rendre full asynchrone, et trouver un moyen pour que l'utilisateur puisse facilement traiter les résultats envoyés par le client Telnet.

Pour l'utiliser :

[code:1] . .\Telnet.ps1
Start-TelNetSession -Hostname localhost -Port 23[/code:1]

Je suis bien sûr ouvert à toutes autres suggestions,remarques,commentaires,etc...

[code:1]#
#.Synopsis
#
# Open a simple Telnet session on the remote host.
#
#.Description
#
# Simple Telnet client.
#
#.Example
#
# Start-TelNetSession
#
# Start a Telnet session on localhost on port 23 by default
#
#.Example
#
# Start-TelNetSession -Hostname 192.168.2.1 -Port 23
#
# Start a Telnet session on specified host.
#

Function Start-TelNetSession
{
param([string]$Hostname=\"localhost\",[int]$Port=\"23\"«»)

Function Negotiate
{
param([System.Net.Sockets.NetworkStream]$stream, [Byte[]]$Buffer, [int]$Count )
[int] $resplen = 0 ;
[int] $index = 0 ;
[byte] $GA = 249 ;
[byte] $WILL = 251 ;
[byte] $WONT = 252 ;
[byte] $DO = 253 ;
[byte] $DONT = 254 ;
[byte] $IAC = 255 ;

[byte] $ECHO = 1 ;
[byte] $SUPP = 3 ;

while ( $index -lt $Count )
{
if ( $Buffer[$index] -eq $IAC )
{
try
{
switch ($Buffer[$index + 1 ] )
{
$IAC
{
$Buffer[$resplen++ ] = $Buffer[$index ] ;

$index += 2 ;

break ;
}

$GA
{
$index += 2 ;

break ;
}

$DO
{
$Buffer[$index + 1 ] = $WONT ;

$stream.Write($Buffer ,$index , 3 ) ;

$index += 3 ;

break ;
}
$DONT
{
$Buffer[$index + 1 ] = $WONT ;

$stream.Write($Buffer , $index , 3 ) ;

$index += 3 ;

break ;
}

$WONT
{
$Buffer[$index + 1 ] = $DONT ;

$stream.Write( $Buffer , $index , 3 ) ;

$index += 3 ;

break ;
}

$WILL
{
[byte]$action = $DONT ;

if ($Buffer[ $index + 2 ] -eq $ECHO )
{
$action = $DO ;
}
elseif ( $Buffer[ $index + 2 ] -eq $SUPP )
{
$action = $DO ;
}

$Buffer[ $index + 1 ] = $action ;

$stream.Write( $Buffer , $index , 3 ) ;

$index += 3 ;

break ;
}
}
}
catch
{
$index = $Count ;
}
}
else
{
if ( $Buffer[$index] -ne 0 )
{
$Buffer[$resplen++ ] = $Buffer[$index ] ;
}

$index++ ;
}
}

return ($resplen ) ;
}

try
{
$socket = New-Object \"System.Net.Sockets.TcpClient\" -ArgumentList $Hostname,$Port
}
catch
{
$stream.close()
$socket.Close()
throw write-host \"Cannot connect to $Hostname`:$Port.\"-Foregroundcolor Red
}

$socket.NoDelay = $true
$stream = $socket.GetStream();
$buffer = New-Object \"System.Byte[]\" -ArgumentList $socket.ReceiveBufferSize
[Int]$byte = 0
$exit = $false;
$encoding = [System.Text.Encoding]::ASCII

Write-Host \"Connected to $Hostname`:$Port !\" -Foregroundcolor Green
While($exit -eq $false)
{
start-sleep -m 200
while($stream.DataAvailable)
{
try
{
$bytes = $stream.Read($buffer , 0 , $buffer.Length )
}
catch
{
$exit=$true
write-host \"Conection Lost\" -Foregroundcolor Red
}

if ( $bytes -gt 0 )
{
try
{
$bytes = Negotiate -stream $stream -buffer $Buffer -Count $bytes
}
catch
{
write-host \"Conection Lost\" -Foregroundcolor Red
$exit=$true
}
if ( $bytes -gt 0 )
{
$response = [System.Text.Encoding]::ASCII.GetString($buffer , 0 ,$bytes )
[System.Console]::Write($response)
if($response -match \"password\"«»)
{
$cmd_secure = Read-Host -AsSecureString
$BSTR = [System.Runtime.InteropServices.marshal]::«»SecureStringToBSTR($cmd_secure)
$cmd = [System.Runtime.InteropServices.marshal]::«»PtrToStringAuto($BSTR)
}
else
{
$cmd = Read-Host
}
if ($cmd -eq \"exit\"«»)
{
Write-Host \"Quitting...\" -Foregroundcolor Green
$exit=$true
}

else
{
[Byte[]]$data = [System.Text.Encoding]::ASCII.GetBytes($Cmd + \"`r\"«»)
try
{
$stream.Write($data, 0, $data.length)
}
catch
{
write-host \"Conection Lost\" -Foregroundcolor Red
$exit=$true
}
}
}
}
}
}
$stream.close()
$socket.Close()
}
[/code:1]

La pièce jointe Telnet.ps1 est absente ou indisponible

<br><br>Message édité par: bilbao, à: 22/12/10 12:13
Pièces jointes :

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.040 secondes
Propulsé par Kunena