Check the connectiont type SSH to the switch

I check the connection type SSH to the switch in PuTTY. I get an error.

When I check the connection to the same switch programmatically, I get success.

$SocetCon = New-Object Net.Sockets.TcpClient
$SocetConTelnet = $SocetCon.Connect("", 22)

How to programmatically get the same result as in PuTTY?

Welcome to the forum. :wave:t4:

Where do you know? What’s the result of

What’s in $SocetConTelnet after you run this code?

Thank you! I get the result of executing the command Connect .T.

Please do not post images of code or error messages or console output. Instead post the plain text formatted as code.

Thanks in advance.

What’s in $SocetConTelnet

Thenks for the advice! How can I see what’s in $SocetConTelnet?

You output it on the console


I get en error:

MethodInvocationException: \\tsfccifspsv01\flcprofiles\adminsantoniuk\Documents\Desarollo\PowerShell\SocetCon.ps1:2:1
Line |
   2 |  $SocetConTelnet = $SocetCon.Connect("", 2)
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Connect" with "2" argument(s): "No se puede establecer una conexión ya que el equipo de destino denegó expresamente dicha conexión. [::ffff:]:2"

Sorry. I dont get anything.

But it seems like you get an error running your script.

To debug some code we run it line by line and watch the output. It looks like you’re using VSCode for developing. You can run individual lines of code when you select them and hit <F8>.

debagin cod with F8 in line by line

PS C:\Users\adminsantoniuk> $SocetCon = New-Object Net.Sockets.TcpClient
PS C:\Users\adminsantoniuk>
PS C:\Users\adminsantoniuk> $SocetConTelnet = $SocetCon.Connect("", 22)
PS C:\Users\adminsantoniuk>
PS C:\Users\adminsantoniuk> Write-Output "Test connect" $SocetConTelnet
Test connect

OK. So $SocetConTelnet is obviously empty. So it actually does make that much sense to capture the output when there is no output. :wink:

Are you sure you set up Putty with the correct options? I’m by no means a telnet or Putty expert but it seems like Putty tries to use the wrong protocol.

There are two switches. Checking the connection type Telnet - port 23 and type SSH - port 22 in PuTTY: - Telnet - success, SSH - success - Telnet - success, SSH - error
But programmatically I get success in all of them.

You use two different IP addresses. Are you aware of that? Is that on purpose?

OK … and wich one did you set up for the connection?

We have a lot of switches, almost 300. I manually checked the connection with each of four different types of TELNET, SSH, HTTP, HTTPS.

IPa_AdreçaIP Marca Telnet SSH Http Https 3 COM .T. .F. .T. .F. 3 COM .T. .F. .T. .F. .F. 3 COM .T. .F. .T. .F. .F. HP .T. .F. .T. .F. HP .T. .F. .T. .F. HP .T. .F. .T. .F. HP .T. .F. .T. .F. HP .T. .F. .T. .F. HP .T. .F. .T. .F. HP .T. .T. .T. .F. HP .T. .F. .F. .F. HP .T. .T. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .T. .T. .T. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F. H3C .T. .F. .T. .F.

Now I’m trying to write a script that will do this connection check.

$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
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'
                        $ErrorActionPreference = 'Continue'
            if ($SocetTelnet.Connected)
                $MsgSocetTelnet = "Telnet - SUCCESS"
                $MsgSocetTelnet = "Telnet - FAILED"
            #$SocetTelnet = $null
            # SSH
            $SocetSsh = New-Object Net.Sockets.TcpClient
            $ErrorActionPreference = 'SilentlyContinue'
            $ErrorActionPreference = 'Continue'
            if ($SocetSsh.Connected)
                $MsgSocetSsh = "Ssh - SUCCESS"
                $MsgSocetSsh = "Ssh - FAILED"
            $Msg = "OFFLINE!"
    else {
        $Msg = "OFFLINE!"

    $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

Is there a particular reason why you’re not using

? :wink:

Thank yuo!
I will try to work it out in Mondey.

I can’t find an example of using this command. I execute the command as follows:

$Socet = Test-NetConnection -ComputerName -Port 22

I get the result:       23                      True                                           True

found a link that this command is not installed on MS Server 2012:
windows server 2012 - PowerShell cmdlet Test-NetConnection not available - Stack Overflow
I am working on MS Serverr 2012 R2.

How about the examples included in the help I linked above? There are 6 of them! :man_shrugging:t4:

If you cannot update your PowerShell version or use another computer to accomplish your task you’re pretty much out of luck then. :man_shrugging:t4:

BTW: The extended support for WIndows Server 2012R2 will end in about a year … how about considering updating to a newer version? :wink:

I have only been working at this place for a month. I can’t say anything about updating the system version.
I saw these examples, but I wanted to know how this command is used in practice. But for some reason I did not find examples of use.