Check the connectiont type SSH to the switch

What exactly did you search for?

https://www.google.com/search?q=test-netconnection

Since I have to test connections of different types https, http, ssh, telnet I wanted to know how often this command is used for these types of connections.
I was wrong, there are no Test-NetConnection examples yet. Searching the forum showed me many themas. I will try to make and compare the results of Test-NetConnection and TcpClient

Sorry … I know this question is off topic … but I have to ask if those are “real” ip’s or you used fake data?

I find it hard to believe all those Class A devices have telnet open …

Again, off topic and out of curiosity only. Thanks.

I did not change the IP address. I checked the connection with each manually. TELNET and SSH - using PuTTY. HTTPS and HTTP - using a browser. Surely it was worth changing the IP address for security? But each connection requieres autorizacion.

This is how I test the connection to the switches using Net.Socets.TcpClient.

$ExcelFile = '\\tsfccifspsv01\flcprofiles\adminsantoniuk\Documents\XLS\IP_de_ Switches.xlsx'
$objExcel = New-Object -ComObject Excel.Application
$WorkBook = $objExcel.Workbooks.Open($ExcelFile)
$WorkSheets = $WorkBook.Sheets.Item(1)
$lastRow = $WorkSheets.UsedRange.rows.count
#$xlLasstCell = [Microsoft.Office.Interep.Excel.Constants]::xlLastCell
$WorkSheets.Cells.Item($i, 10) = "Telnet-SC"
$WorkSheets.Cells.Item($i, 11) = "Ssh-SC"
for ($i = 2; $i -le $lastRow; $i++)
{
    $AdrecaIP = $WorkSheets.Cells.Item($i, 3).text

    if (($AdrecaIP) -ne $null -and ($AdrecaIP) -ne "")                  # Si IP no es NULL
    {
        if (Test-Connection -IPv4 $AdrecaIP -Count 1 -Quiet)            # Ping probar
        {
            $Msg = "Ping - ONLINE!"
            # Telnet
            $SocetTelnet = New-Object Net.Sockets.TcpClient
            $ErrorActionPreference = 'SilentlyContinue'
            try {
                $SocetTelnet.Connect($AdrecaIP,"23")
                $ErrorActionPreference = 'Continue'                
                if ($SocetTelnet.Connected)
                {
                    $MsgSocetTelnet = "Telnet - .T."
                }
                else
                {
                    $MsgSocetTelnet = "Telnet - .F."
                }
            }
            catch {
                $MsgSocetTelnet = "Telnet - .F."
            }
            finally {
                $SocetTelnet.Dispose()
            }
            #$SocetTelnet = $null
            [System.Runtime.InteropServices.Marshal]::ReleaseComObject[$SocetTelnet]
            # SSH
            $SocetSsh = New-Object Net.Sockets.TcpClient
            $ErrorActionPreference = 'SilentlyContinue'
            try {
                $SocetSsh.Connect($AdrecaIP,"22")
                $ErrorActionPreference = 'Continue'
                if ($SocetSsh.Connected)
                {
                    $MsgSocetSsh = "Ssh - .T."
                }
                else
                {
                    $MsgSocetSsh = "Ssh - .F."
                }
            }
            catch {
                $MsgSocetSsh = "Ssh - .F."
            }
            finally {
                $SocetSsh.Dispose()
            }
        }
        else 
        {
            $Msg = "OFFLINE!"
            $MsgSocetTelnet = "Telnet - .F."
            $MsgSocetSsh = "Ssh - .F."
        }
    }
    else {
        $Msg = "OFFLINE!"
        $MsgSocetTelnet = "Telnet - .F."
        $MsgSocetSsh = "Ssh - .F."
    }
    # Guardar resultado
    # Telnet
    if ($MsgSocetTelnet -eq "Telnet - .T.") {
        $WorkSheets.Cells.Item($i, 10) = ".T."
    }
    else {
        $WorkSheets.Cells.Item($i, 10) = ".F."
    }
    # Guardar resultado
    # SSH
    if ($MsgSocetSsh -eq "Ssh - .T.") {
        $WorkSheets.Cells.Item($i, 11) = ".T."
    }
    else {
        $WorkSheets.Cells.Item($i, 11) = ".F."
    }
    $WorkSheets.Cells.Item($i, 1).text +
        '   ' + $WorkSheets.Cells.Item($i, 2).text +
        '   ' + $WorkSheets.Cells.Item($i, 3).text +
        '   ' + $WorkSheets.Cells.Item($i, 4).text +
        '   ' + $WorkSheets.Cells.Item($i, 5).text +
        '   ' + $WorkSheets.Cells.Item($i, 6).text +
        '   ' + $WorkSheets.Cells.Item($i, 7).text +
        '   ' + $WorkSheets.Cells.Item($i, 8).text +
        '   ' + $Msg +
        '   ' + $MsgSocetTelnet +
        '   ' + $MsgSocetSsh
}
$objExcel.ActiveWorkbook.Save()
$objExcel.ActiveWorkbook.Close()
$objExcel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject[$objExcel]

Through Test-NetConnection haven’t tried it yet. Switches that have no Ssh connection, via PuTTY:

Network error: Connection timed out

also have no connection via Net.Socets.TcpClient. No PuTTY SSH connection due to other reasons, for example:

Remote side sent disconnect message type 2 (protocol error): "The connection is closed by SSH Server Current FSM is SSH_Main_SSHProcess"

I get success when I check the connection with this switch programmatically by Net.Socets.TcpClient.

Before checking https and http connections with the switch, manually by the browser, I disabled the use of Proxy in the LAN configuration of the browser: Use automatic configuration scripts. Can I do it programmatically? Thanks!

Sorry, but I have one more question. To check HTTPS and HTTP connection with the switches, is it better to use Test-NetConnection or others functions?

I check TELNET, SSH, HTTP and HTTPS connections with switches using two commands.

            # Telnet
            $SocetTelnet = New-Object Net.Sockets.TcpClient
            $ErrorActionPreference = 'SilentlyContinue'
            $SocetTelnet.Connect($AdrecaIP,"23")
            $ErrorActionPreference = 'Continue'                
            $SocetTelnet.Connected)
            $NetConTelnet = Test-NetConnection -ComputerName $AdrecaIP -Port 23 -InformationLevel                         
                        Quiet -WarningAction SilentlyContinue
            # Ssh
            $SocetSsh = New-Object Net.Sockets.TcpClient
            $ErrorActionPreference = 'SilentlyContinue'
            $SocetSsh.Connect($AdrecaIP,"22")
            $ErrorActionPreference = 'Continue'                
            $SocetSsh.Connected)
            $NetConSsh = Test-NetConnection -ComputerName $AdrecaIP -Port 22 -InformationLevel                         
                        Quiet -WarningAction SilentlyContinue
            # Http
            $SocetHttp = New-Object Net.Sockets.TcpClient
            $ErrorActionPreference = 'SilentlyContinue'
            $SocetHttp.Connect($AdrecaIP,"80")
            $ErrorActionPreference = 'Continue'                
            $SocetHttp.Connected)
            $NetConHttp = Test-NetConnection -ComputerName $AdrecaIP -Port 80 -InformationLevel                         
                        Quiet -WarningAction SilentlyContinue
            # Https
            $SocetHttps = New-Object Net.Sockets.TcpClient
            $ErrorActionPreference = 'SilentlyContinue'
            $SocetHttps.Connect($AdrecaIP,"443")
            $ErrorActionPreference = 'Continue'                
            $SocetHttps.Connected)
            $NetConHttps = Test-NetConnection -ComputerName $AdrecaIP -Port 443 -InformationLevel                         
                        Quiet -WarningAction SilentlyContinue

Comparing the results of both commands shows that they work in the same way. Next, I have a task to connect to each switch using PIN, Login and Password to get, for example, information about the software version of the switch. I would be grateful if you could give me some advice on how to do it better.