Check the connectiont type SSH to the switch

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

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

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

How to programmatically get the same result as in PuTTY?

Serhii,
Welcome to the forum. :wave:t4:

Where do you know? What’s the result of

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

Connectado_Ssh_PuTTY_success
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

$SocetConTelnet

I get en error:

MethodInvocationException: \\tsfccifspsv01\flcprofiles\adminsantoniuk\Documents\Desarollo\PowerShell\SocetCon.ps1:2:1
Line |
   2 |  $SocetConTelnet = $SocetCon.Connect("60.1.7.2", 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:60.1.7.2]: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("60.1.7.21", 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:
60.1.7.2 - Telnet - success, SSH - success
60.1.7.21 - 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
1.30.7.1 3 COM .T. .F. .T. .F.
1.3.7.1 3 COM .T. .F. .T. .F. .F.
3.1.7.1 3 COM .T. .F. .T. .F. .F.
60.1.7.31 HP .T. .F. .T. .F.
60.1.7.22 HP .T. .F. .T. .F.
60.1.7.12 HP .T. .F. .T. .F.
1.60.7.21 HP .T. .F. .T. .F.
60.1.7.32 HP .T. .F. .T. .F.
60.1.7.11 HP .T. .F. .T. .F.
60.1.7.2 HP .T. .T. .T. .F.
60.1.7.21 HP .T. .F. .F. .F.
13.1.7.1 HP .T. .T. .T. .F.
13.1.7.11 H3C .T. .F. .T. .F.
13.1.7.21 H3C .T. .F. .T. .F.
13.1.7.31 H3C .T. .F. .T. .F.
55.1.7.101 H3C .T. .F. .T. .F.
13.1.7.51 H3C .T. .F. .T. .F.
13.1.7.61 H3C .T. .F. .T. .F.
13.1.7.72 H3C .T. .T. .T. .T.
13.1.7.81 H3C .T. .F. .T. .F.
13.1.7.101 H3C .T. .F. .T. .F.
13.1.7.111 H3C .T. .F. .T. .F.
13.1.7.121 H3C .T. .F. .T. .F.
37.1.7.1 H3C .T. .F. .T. .F.
8.1.7.11 H3C .T. .F. .T. .F.
13.1.7.122 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'
            $SocetTelnet.Connect($AdrecaIP,"23")
                        $ErrorActionPreference = 'Continue'
            if ($SocetTelnet.Connected)
            {
                $MsgSocetTelnet = "Telnet - SUCCESS"
            }
            else
            {
                $MsgSocetTelnet = "Telnet - FAILED"
            }
            #$SocetTelnet = $null
            [System.Runtime.InteropServices.Marshal]::ReleaseComObject[$SocetTelnet]
            # SSH
            $SocetSsh = New-Object Net.Sockets.TcpClient
            $ErrorActionPreference = 'SilentlyContinue'
            $SocetSsh.Connect($AdrecaIP,"22")
            $ErrorActionPreference = 'Continue'
            if ($SocetSsh.Connected)
            {
                $MsgSocetSsh = "Ssh - SUCCESS"
            }
            else
            {
                $MsgSocetSsh = "Ssh - FAILED"
            }
        }
        else 
        {
            $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
}
$objExcel.ActiveWorkbook.Close()
$objExcel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject[$objExcel]

Is there a particular reason why you’re not using

? :wink:

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

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

$Socet = Test-NetConnection -ComputerName 60.1.7.21 -Port 22
$Socet

I get the result:

60.1.7.21       23         60.1.7.21                      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.