Results 1 to 5 of 5

Thread: Testing Access Method using Sockets (SSH,TELNET)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Testing Access Method using Sockets (SSH,TELNET)

    I need to parse a list of IPaddresses and determin if they are using SSH (port 22) or telnet (port 23). The best way I can think to do this is by attempting to open a socket (not the full connection). If port 22 is refused it's not SSH, then try port 23. The big problem is that there are 5000 entries in this list and I can't wait for the default timeout for each socket to determine the access method. That would be a total of 10,000 connection attempts and a complete waste of the users day!

    Any help would be greatly appreciated.

    P.S. I'm using VB 2005
    Last edited by Tonyf84; Mar 12th, 2007 at 03:09 PM. Reason: Adding Language

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Testing Access Method (SSH,TELNET)

    Here's what I have so far...It always comes back as Telnet though.


    VB Code:
    1. For Z = 1 To NumRows
    2.             ProgressBar2.Value = Z
    3.             ipaddr = Hostlist(Z, 0)
    4.             System.Windows.Forms.Application.DoEvents()
    5.  
    6.             Try
    7.                 Using tcpClient
    8.  
    9.                     tcpClient.Connect(ipaddr, 23)
    10.                     ProgressBar1.Value = 1
    11.                     isConnected = tcpClient.Connected
    12.                     tcpclient.close
    13.                 End Using
    14.  
    15.             Catch ex As Exception
    16.  
    17.             End Try
    18.  
    19.  
    20.  
    21.  
    22.             Select Case isConnected
    23.  
    24.                 Case True
    25.                    
    26.                     Hostlist(Z, 2) = "Telnet"
    27.                 Case False
    28.                     ProgressBar1.Value = 2
    29.                     Try
    30.                         Using tcpClient
    31.                             tcpClient.Connect(ipaddr, 22)
    32.                             ProgressBar1.Value = 1
    33.                             isConnected = tcpClient.Connected
    34.                 tcpclient.close
    35.                         End Using
    36.  
    37.                     Catch ex As Exception
    38.  
    39.                     End Try
    40.  
    41.                     If isConnected = True Then
    42.                        
    43.                         Hostlist(Z, 2) = "SSH"
    44.                     Else : Hostlist(Z, 2) = "ERR"
    45.                     End If
    46.  
    47.             End Select
    48.  
    49.  
    50.         Next
    Last edited by Tonyf84; Mar 12th, 2007 at 03:07 PM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Testing Access Method using Sockets (SSH,TELNET)

    I find it hard to believe that nobody has the skills to solve this problem. Is there another reason there are no responses?

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Testing Access Method using Sockets (SSH,TELNET)

    Your code is the reverse of your stated goal. The code first checks to see if the server accepts a telnet connection. If it does, whether the server accepts a SSH connection or not, the code says it's telnet. Try changing your code to match your goal.

    As far as the time it takes to run through 5,000 addresses, that's how long it's going to take. You can't make 60 minutes take less than an hour, and you can't make 5,000 timeouts happen in less than 5,000 * <the time of one timeout>.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Re: Testing Access Method using Sockets (SSH,TELNET)

    Thanks for the response.

    Actually, if you look at it, it tries telnet, if it connects then it calls it telnet. If it doesn't connect, it tries ssh. If ssh succeeds, it calls it ssh. The problem turned out to be that I wasn't assigning isConnected = "" so on the next iteration of the loop, it already thought it was connected and therefore everyting else after that was deemed to be telnet. I'll paste in my current working code. As for the hour taking less than an hour. Well I don't agree with you. First, there could be some code optimization that would make the routine faster. Secondly, the answer for making 60min = 30min lies in multithreading. I plan to implement that sometime in the next few weeks.

    Also, on our network the devices are either telnet with SSH disabled or SSH with Telnet disabled, so there's no reason to continue if the device responds to Telnet.

    In addition, You'd be correct about not being able to change the time it takes to iterate through 5000 timeouts * 20seconds (the default) if you simply relied on default timeouts. I managed to speed things up significantly by pinging the device first using a 500 millisecond timeout (since there's no way to change the TCP Connect timeout). If the device doesn't respond, I assume it's dead and move to the next without ever trying to connect to it, so I've just reduced the time from 20 seconds to 1/2 second.

    And for those who care (and there weren't many) here is the code that works. I hope that someone having similar issues in future finds it useful:

    vb Code:
    1. Friend Sub ParseList()
    2.         Dim Hostlist(10000, 2) As String
    3.         Dim temp, Path As String
    4.         Dim CurrentField As String
    5.         Dim CurrentRow As String()
    6.         Dim X, Y, NumRows, Z As Integer
    7.         Dim tcpClient As New System.Net.Sockets.TcpClient()
    8.         Dim isConnected, HostAvailable As Boolean
    9.         Dim ipaddr As String
    10.  
    11.  
    12.  
    13.         With ProgressBar2
    14.  
    15.             .Show()
    16.             .Style = ProgressBarStyle.Continuous
    17.             .Minimum = 0
    18.             .Maximum = 10000
    19.             .Value = 0
    20.  
    21.         End With
    22.  
    23.         Path = App_Path()
    24.  
    25.  
    26.         Using HostReader As FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(Path & "hostlist.txt")
    27.  
    28.             HostReader.SetDelimiters(vbTab)
    29.  
    30.             While Not HostReader.EndOfData
    31.  
    32.                 temp = HostReader.ReadFields.ToString
    33.                 X = X + 1
    34.                 ProgressBar2.Value = X
    35.                 System.Windows.Forms.Application.DoEvents()
    36.                 NumRows = X
    37.  
    38.             End While
    39.  
    40.  
    41.         End Using
    42.  
    43.         ProgressBar2.Hide()
    44.  
    45.         ReDim Hostlist(X, 2)
    46.  
    47.         X = 0
    48.         Y = 0
    49.  
    50.  
    51.         With ProgressBar2
    52.  
    53.             .Show()
    54.             .Style = ProgressBarStyle.Continuous
    55.             .Minimum = 0
    56.             .Maximum = 10000
    57.             .Value = 0
    58.  
    59.         End With
    60.  
    61.         Using HostReader As FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(Path & "hostlist.txt")
    62.  
    63.             HostReader.TextFieldType = FileIO.FieldType.Delimited
    64.             HostReader.SetDelimiters(vbTab)
    65.  
    66.             While Not HostReader.EndOfData
    67.  
    68.                 CurrentRow = HostReader.ReadFields
    69.  
    70.                 For Each CurrentField In CurrentRow
    71.  
    72.                     Hostlist(X, Y) = CurrentField
    73.                     Y += 1
    74.  
    75.                 Next
    76.  
    77.                 X += 1
    78.                 Y = 0
    79.                 ProgressBar2.Value = X
    80.                 System.Windows.Forms.Application.DoEvents()
    81.             End While
    82.  
    83.         End Using
    84.         PictureBox2.Show()
    85.         ProgressBar2.Value = 0
    86.  
    87.         X = 0
    88.         Y = 0
    89.  
    90.         With ProgressBar2
    91.  
    92.             .Show()
    93.             .Style = ProgressBarStyle.Continuous
    94.             .Minimum = 0
    95.             .Maximum = NumRows
    96.             .Value = 0
    97.  
    98.         End With
    99.  
    100.         With ProgressBar1
    101.  
    102.             .Show()
    103.             .Style = ProgressBarStyle.Continuous
    104.             .Minimum = 0
    105.             .Maximum = 2
    106.             .Value = 0
    107.  
    108.         End With
    109.  
    110.         Label1.Visible = True
    111.         Label2.Visible = True
    112.  
    113.         For Z = 1 To NumRows
    114.             ProgressBar2.Value = Z
    115.             ipaddr = Hostlist(Z, 0)
    116.             Label2.Text = "Currently Processing:  " & Z.ToString & " of " & NumRows.ToString
    117.             System.Windows.Forms.Application.DoEvents()
    118.  
    119.             Try
    120.                 HostAvailable = My.Computer.Network.Ping(ipaddr, 500)
    121.             Catch ex As Exception
    122.  
    123.             End Try
    124.  
    125.             If HostAvailable = True Then
    126.                 Try
    127.                     Using tcpClients As New TcpClient
    128.                         Label1.Text = "Trying: " & ipaddr & " On " & "Port 23"
    129.                         ProgressBar1.Value = 2
    130.                         System.Windows.Forms.Application.DoEvents()
    131.                         tcpClients.Connect(ipaddr, 23)
    132.  
    133.                         isConnected = tcpClients.Connected
    134.                         System.Windows.Forms.Application.DoEvents()
    135.  
    136.                     End Using
    137.  
    138.                 Catch ex As Exception
    139.                     Select Case Err.Number
    140.                         Case 5
    141.                             isConnected = False
    142.                         Case Else
    143.                             MsgBox(Err.Number)
    144.                     End Select
    145.  
    146.                 End Try
    147.  
    148.  
    149.  
    150.  
    151.                 Select Case isConnected
    152.  
    153.                     Case True
    154.                         Hostlist(Z, 2) = "Telnet"
    155.                         ProgressBar1.Value = 0
    156.  
    157.                     Case False
    158.                         ProgressBar1.Value = 2
    159.                         Try
    160.                             Using TcpClients As New TcpClient
    161.                                 Label1.Text = "Trying: " & ipaddr & " On " & "Port 22"
    162.                                 ProgressBar1.Value = 2
    163.                                 System.Windows.Forms.Application.DoEvents()
    164.                                 TcpClients.Connect(ipaddr, 22)
    165.                                 isConnected = TcpClients.Connected
    166.                                 System.Windows.Forms.Application.DoEvents()
    167.  
    168.                             End Using
    169.  
    170.                         Catch ex As Exception
    171.                             Select Case Err.Number
    172.                                 Case 5
    173.                                     isConnected = False
    174.                                 Case Else
    175.                                     MsgBox(Err.Number)
    176.                             End Select
    177.  
    178.                         End Try
    179.  
    180.                         If isConnected = True Then
    181.  
    182.                             Hostlist(Z, 2) = "SSH"
    183.                             isConnected = Nothing
    184.  
    185.                         Else : Hostlist(Z, 2) = "ERR"
    186.                         End If
    187.  
    188.                 End Select
    189.  
    190.  
    191.             End If
    192.             'Debug.Print(Z & "  " & Hostlist(Z, 0) & "  " & Hostlist(Z, 2))
    193.         Next
    194.  
    195.         PictureBox3.Show()
    196.         System.Windows.Forms.Application.DoEvents()
    197.  
    198.        
    199.     End Sub
    Last edited by Tonyf84; Mar 20th, 2007 at 04:34 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width