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.