System.Text.AsciiEncoding.GetString for System.Net.Sockets.TcpClient.GetStream()

Let me ask again. Use:

System.Net.Sockets.TcpClient.GetStream()
System.IO.StreamWriter()
System.Text.AsciiEncoding.GetString

for execute configuration commandsa on switches and get the result of their execution.

$remoteHost = '1.1.1.2'
# '1.1.1.1'
# '1.1..2'
$port = '23'

$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port) 

## Open the socket, and connect to the computer on the specified port 
write-host "Connecting to $remoteHost on port $port"
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)  
if($socket -eq $null) { return; } 
 
$stream = $socket.GetStream() 
$writer = new-object System.IO.StreamWriter($stream) 
 
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding

start-sleep -m 500 
## Read all the data available from the stream, writing it to the 
## screen when done. 
$RTresponse = ""
while($stream.DataAvailable)  
{  
    #, 0, 1024
  $read = $stream.Read($buffer, 0, 1024)
  write-host -n "Read = $read `n"
  $RTresponse += ($encoding.GetString($buffer, 0, $read))
  write-host "Response = $RTresponse"
}

## Read the user's command, quitting if they hit ^D 
$command = ''
       
## Write their command to the remote host      
$writer.WriteLine($command) 
$writer.Flush()

## Close the streams
$writer.Close() 
$stream.Close() 
$socket.Dispose()

Get satisfactory results for some switch types:

Read = 469
Response = ??☺??☺??☺??♥??↑??▼
********************************************************************************
*  Copyright(c) 2004-2007 3Com Corp. and its licensors. All rights reserved.   *
*  Without the owner's prior written consent,                                  *
*  no decompiling or reverse-engineering shall be allowed.                     *
********************************************************************************


Login authentication


Username:

For other types of switches (Cisco), get the result in a non-readable way:

Read = 12
Response = ??↑?? ??#??'

The told me to play the encoding:

$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
...
$read = $stream.Read($buffer, 0, 1024)
$RTresponse += ($encoding.GetString($buffer, 0, $read))

but I can’t find a solution to this problem yet. I’d appreciate any hint.

FWIW, I tried your script as is connecting to a Cisco switch. Aside from changing the Port to 22 (SSH), it worked perfectly. Here is the response:

Connecting to x.x.x.x on port 22
Read = 19
Response = SSH-2.0-Cisco-1.25

I also found this, which seems to relate to your problem:
https://social.msdn.microsoft.com/Forums/en-US/57a15405-6ba3-4522-a4ac-c3913215c7a2/using-tcpclient-to-do-a-telnet-session?forum=netfxnetcom

Thenks for the reply.
If I change port to 22, I get the answer:

Connecting to 1.1.1.1 on port 22
Read = 21
Response = SSH-2.0-OpenSSH_6.2

but this script does not work correctly for SSH switch configaricion commands.
I read the tema on you link, but so far I fine no answer.

That link is mostly C# solutions. I was thinking you could get some ideas from that. They seem to be having the exact same problem as you. Sorry for not being any help. We have long ago abandoned telnet so I am unable to test and try some of those solutions.

Thenks for the help! We also plan to give up TELNET. I will try do the same task with SSH