SSH-2.0-OpenSSH_6.2 Protocol mismatch

Hi!
Tryning to connect to the switch to execute configuration commands:

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 = 100
    $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
}

$Result = ''
$Socet = New-Object Net.Sockets.TcpClient
try {
    $Socet.Connect("11.1.1.1",22)
    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

        $Result = waitfor ''
        $Writer.writeline('')
        $Writer.Flush()

        $Result += waitfor 'UNFAS1_A_\$ ' 30
        $Writer.Flush()

        $Result

        Start-Sleep -s 1
        readtelnet

        $Writer.Close()
        $Stream.Close()
        $Writer = $null
        $Stream = $null
        
        Write-Output "Success"
    }
    else {
        Write-Output "Failed"
    }
}
catch {
    Write-Output "Failed"
}
finally {
    $Socet.Dispose()
    $Socet = $null
}

the $Result variable returns the value:

SSH-2.0-OpenSSH_6.2
Protocol mismatch.

How can I Logon to a switch to run configuration commands?

With PiTTY Configuration successfully connects to this switch via Port 22, but for some reason asks for a username twice before asking for a password.

With you permission, I’ll remind of my question.
When tryning to connect to a switch to execute command of configuration, using SSH, program code above, get the error:

SSH-2.0-OpenSSH_7.3p1.RL
Protocol mismatch.
   T♂☺   ☻   ;Connection closed by server: identification exchange failed

or another similar error:

SSH-2.0-OpenSSH_6.2
Protocol mismatch.

With PiTTY connection is successful and all commands are executed.
With protocol TELNET this script works successfully…
What can be the cause of the error?
Thank you very much

Have you tried:

$Socet.Connect("username@11.1.1.1",22)

Hi
Thank you for answering! But I get an error:

try {
    $Socet.Connect("admin@11.1.1.1",22)
}
catch {
    Write-Output "Failed"
}
finally {

 }

o so, also - Failed:

try {
    $Socet.Connect("username@11.1.1.1",22)
}
catch {
    Write-Output "Failed"
}
finally {

 }

I don’t know how to see the text of error.

Try this:

Write-Output "Connection failed with error: $($_.Exception.Message)"

I assume you have set:

$ErrorActionPreference = 'Stop'

Somewhere at the beginning of your script.

Hi!
No, I do not use in my script:

$ErrorActionPreference = 'Stop'

this is my script:

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 = 22
$Result = ''
$Usuario = 'admin'
$Contrasena = '*********'
$AdrecaIP = "username@11.1.1.1"
$Instruccion = 'display version'

write-host "========================================================"
write-host "Connecting to $AdrecaIP on port $Port"
$Socet = New-Object Net.Sockets.TcpClient
try {
    $Socet.Connect($AdrecaIP,$Port)
    if ($Socet.Connected) {
        write-host "Create Stream"
        $Stream = $Socet.GetStream()
        $Writer = New-Object System.IO.StreamWriter($Stream)
        $Buffer = New-Object System.Byte[] 1024
        $Encoding = New-Object System.Text.ASCIIEncoding

        write-host "--------------------------------------------------------"
        write-host "ENTER empty"
        $Result = waitfor '' 5
        $Writer.writeline('')
        $Writer.Flush()
        write-host "$Result `n________________________________________________________"

        write-host "ENTER Username"
        $Result += waitfor 'Username' 5
        $Writer.writeline($Usuario)
        $Writer.Flush()
        write-host "$Result `n________________________________________________________"

        write-host "ENTER Password"
        $Result += waitfor 'Password' 5
        $Writer.writeline($Contrasena)
        $Writer.Flush()
        write-host "$Result `n________________________________________________________"

        write-host "ENTER - Instruccion: $Instruccion"
        $Result = waitfor "$Instruccion"
        $Writer.writeline($Instruccion)
        $Writer.Flush()
        write-host "$Result `n________________________________________________________"

        $Result += waitfor 'UNFAS1_A_\$ ' 5
        $Writer.Flush()

        write-host "OUTPUT $Result"

        Start-Sleep -s 1
        readtelnet

        $Writer.Close()
        $Stream.Close()
        $Writer = $null
        $Stream = $null
        
        Write-Output "Success"
    }
    else {
        Write-Output "Failed"
    }
}
catch {
    Write-Output "Failed"
    Write-Output "Connection failed with error: $($_.Exception.Message)"
}
finally {
    $Socet.Dispose()
    $Socet = $null
}

after execution, I get an error:

Failed
Connection failed with error: Exception calling "Connect" with "2" argument(s): "Error no recuperable durante una búsqueda en base de datos."

on the line:

$Socet.Connect("username@11.1.1.1", 22)

The help about TcpClient.Connect says that:

Connect(IPAddress, Int32)
Connects the client to a remote TCP host using the specified IP address and port number.

Parameters
address - IPAddress
The IPAddress of the host to which you intend to connect.

port - Int32
The port number to which you intend to connect.

It looks like the Connect method does not support this syntax.

Is there other way to connect to the switch via SSH to execute commands of the configuration?