PDA

Click to See Complete Forum and Search --> : Testing Access Method using Sockets (SSH,TELNET)


Tonyf84
Mar 7th, 2007, 08:37 PM
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

Tonyf84
Mar 12th, 2007, 03:03 PM
Here's what I have so far...It always comes back as Telnet though.




For Z = 1 To NumRows
ProgressBar2.Value = Z
ipaddr = Hostlist(Z, 0)
System.Windows.Forms.Application.DoEvents()

Try
Using tcpClient

tcpClient.Connect(ipaddr, 23)
ProgressBar1.Value = 1
isConnected = tcpClient.Connected
tcpclient.close
End Using

Catch ex As Exception

End Try




Select Case isConnected

Case True

Hostlist(Z, 2) = "Telnet"
Case False
ProgressBar1.Value = 2
Try
Using tcpClient
tcpClient.Connect(ipaddr, 22)
ProgressBar1.Value = 1
isConnected = tcpClient.Connected
tcpclient.close
End Using

Catch ex As Exception

End Try

If isConnected = True Then

Hostlist(Z, 2) = "SSH"
Else : Hostlist(Z, 2) = "ERR"
End If

End Select


Next

Tonyf84
Mar 18th, 2007, 03:43 PM
I find it hard to believe that nobody has the skills to solve this problem. Is there another reason there are no responses?

Al42
Mar 19th, 2007, 01:40 PM
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>.

Tonyf84
Mar 20th, 2007, 04:20 PM
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:


Friend Sub ParseList()
Dim Hostlist(10000, 2) As String
Dim temp, Path As String
Dim CurrentField As String
Dim CurrentRow As String()
Dim X, Y, NumRows, Z As Integer
Dim tcpClient As New System.Net.Sockets.TcpClient()
Dim isConnected, HostAvailable As Boolean
Dim ipaddr As String



With ProgressBar2

.Show()
.Style = ProgressBarStyle.Continuous
.Minimum = 0
.Maximum = 10000
.Value = 0

End With

Path = App_Path()


Using HostReader As FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(Path & "hostlist.txt")

HostReader.SetDelimiters(vbTab)

While Not HostReader.EndOfData

temp = HostReader.ReadFields.ToString
X = X + 1
ProgressBar2.Value = X
System.Windows.Forms.Application.DoEvents()
NumRows = X

End While


End Using

ProgressBar2.Hide()

ReDim Hostlist(X, 2)

X = 0
Y = 0


With ProgressBar2

.Show()
.Style = ProgressBarStyle.Continuous
.Minimum = 0
.Maximum = 10000
.Value = 0

End With

Using HostReader As FileIO.TextFieldParser = My.Computer.FileSystem.OpenTextFieldParser(Path & "hostlist.txt")

HostReader.TextFieldType = FileIO.FieldType.Delimited
HostReader.SetDelimiters(vbTab)

While Not HostReader.EndOfData

CurrentRow = HostReader.ReadFields

For Each CurrentField In CurrentRow

Hostlist(X, Y) = CurrentField
Y += 1

Next

X += 1
Y = 0
ProgressBar2.Value = X
System.Windows.Forms.Application.DoEvents()
End While

End Using
PictureBox2.Show()
ProgressBar2.Value = 0

X = 0
Y = 0

With ProgressBar2

.Show()
.Style = ProgressBarStyle.Continuous
.Minimum = 0
.Maximum = NumRows
.Value = 0

End With

With ProgressBar1

.Show()
.Style = ProgressBarStyle.Continuous
.Minimum = 0
.Maximum = 2
.Value = 0

End With

Label1.Visible = True
Label2.Visible = True

For Z = 1 To NumRows
ProgressBar2.Value = Z
ipaddr = Hostlist(Z, 0)
Label2.Text = "Currently Processing: " & Z.ToString & " of " & NumRows.ToString
System.Windows.Forms.Application.DoEvents()

Try
HostAvailable = My.Computer.Network.Ping(ipaddr, 500)
Catch ex As Exception

End Try

If HostAvailable = True Then
Try
Using tcpClients As New TcpClient
Label1.Text = "Trying: " & ipaddr & " On " & "Port 23"
ProgressBar1.Value = 2
System.Windows.Forms.Application.DoEvents()
tcpClients.Connect(ipaddr, 23)

isConnected = tcpClients.Connected
System.Windows.Forms.Application.DoEvents()

End Using

Catch ex As Exception
Select Case Err.Number
Case 5
isConnected = False
Case Else
MsgBox(Err.Number)
End Select

End Try




Select Case isConnected

Case True
Hostlist(Z, 2) = "Telnet"
ProgressBar1.Value = 0

Case False
ProgressBar1.Value = 2
Try
Using TcpClients As New TcpClient
Label1.Text = "Trying: " & ipaddr & " On " & "Port 22"
ProgressBar1.Value = 2
System.Windows.Forms.Application.DoEvents()
TcpClients.Connect(ipaddr, 22)
isConnected = TcpClients.Connected
System.Windows.Forms.Application.DoEvents()

End Using

Catch ex As Exception
Select Case Err.Number
Case 5
isConnected = False
Case Else
MsgBox(Err.Number)
End Select

End Try

If isConnected = True Then

Hostlist(Z, 2) = "SSH"
isConnected = Nothing

Else : Hostlist(Z, 2) = "ERR"
End If

End Select


End If
'Debug.Print(Z & " " & Hostlist(Z, 0) & " " & Hostlist(Z, 2))
Next

PictureBox3.Show()
System.Windows.Forms.Application.DoEvents()


End Sub