From some switchesI get the answer in this form:
??↑?? ??#??'
I’am using protocol 23 to connect to the switch because SSH still gives an error. Executing the command and geting a response is as follows:
function readtelnet {
$response = ""
While($Stream.DataAvailable) {
$Read = $Stream.Read($Buffer, 0, 1024)
$response += ($Encoding.GetString($Buffer, 0, $Read))
}
return $response;
}
function waitfor {
param([string]$search, [int]$timeout)
if ($timeout -eq 0) {
$timeout = 3
}
$pause = 1000
$pending = readtelnet
$myResult = $pending
$counter = 0
while ($pending -notmatch $search -and $counter -le ($timeout * (1000/$pause))) {
$counter ++
Start-Sleep -m $pause
$pending = readtelnet
$myResult += $pending
}
return $myResult
}
$Port = 23
$Result = ''
$Usuario = 'admin'
$Contrasena = '********'
$AdrecaIP = "1.1.1.11"
$Socet = New-Object Net.Sockets.TcpClient
$Socet.Connect($AdrecaIP,$Port)
if ($Socet.Connected) {
$Stream = $Socet.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.ASCIIEncoding
write-host "ENTER - Username: $Usuario"
$Result = waitfor 'Username:'
$Writer.writeline($Usuario)
$Writer.Flush()
$Result += waitfor 'UNFAS1_A_\$ '
$Writer.Flush()
Write-Output $Result
Start-Sleep -s 1
readtelnet
$Writer.Close()
$Stream.Close()
$Writer = $null
$Stream = $null
$Socet.Dispose()
$Socet = $null
}
What cout be the reason I get my answer in an unreadable form and how do you fix an erron?
Many thanks!