Scanner multi ports et de plages ip (configurable)

Description

Petit scanner permettant de scanner plusieurs ports simultanéments sur sa machine ou sur une machine distante. Permet aussi de scanner une plage d'adresses ip et résoud le nom dns des machines. Options de configuration incluses. C'est la première appli que j'ai développé en VB. Présenté en action professionnel dans le cadre de mon BTS en alternance (j'ai eu 18,5 ;)).
Je développe un version pour vb.net effectuant du multithread, ce qui permettra de scanner plusieurs ip simultanément...

Source / Exemple :


Voici une partie du code:

Private Sub Check1_Click()
Text8.Text = "1"
Text9.Text = "65535"
End Sub

Private Sub Command1_Click()
      Dim bADRESSE_VALIDE, bFIN_SCAN_IP As Boolean
      Dim nNB_IP_A_SCANNER, nTTL As Long
      Dim nNB_ADRESSES_RESOLUES As Long
      'd = départ f = fin c = cours
      Dim d1, d2, d3, d4, f1, f2, f3, f4, c1, c2, c3, c4 As Integer
      d1 = Val(Text11.Text)
      d2 = Val(Text12.Text)
      d3 = Val(Text13.Text)
      d4 = Val(Text14.Text)
      f1 = Val(Text15.Text)
      f2 = Val(Text16.Text)
      f3 = Val(Text17.Text)
      f4 = Val(Text18.Text)
      
      
    nNB_ADRESSES_RESOLUES = 0
    bADRESSE_VALIDE = False
    bFIN_SCAN_IP = False
    'vérification des adresses ip saisies
    If Not ((d1 <= 0) Or (d1 > 239) Or (f1 <= 0) Or (f1 > 239)) Then
      If Not ((d2 < 0) Or (d2 > 255) Or (f2 < 0) Or (f2 > 255)) Then
         If Not ((d3 < 0) Or (d3 > 255) Or (f3 < 0) Or (f3 > 255)) Then
            If Not ((d4 < 0) Or (d4 > 255) Or (f4 < 0) Or (f4 > 255)) Then
               If f1 > d1 Then
                  bADRESSE_VALIDE = True
               Else
                 If f1 = d1 Then
                    If f2 > d2 Then
                       bADRESSE_VALIDE = True
                    Else
                       If f2 = d2 Then
                          If f3 > d3 Then
                             bADRESSE_VALIDE = True
                          Else
                             If f3 = d3 Then
                                If f4 >= d4 Then
                                   bADRESSE_VALIDE = True
                                End If
                             End If
                          End If
                       End If
                    End If
                 End If
               End If
            End If
         End If
      End If
    End If
    
    If Not bADRESSE_VALIDE Then
      ok = MsgBox("Adresses saisies invalides", 16, "STOP")
      Exit Sub
    End If
      
  Label16.Visible = False
  MSHFlexGrid1.Clear
  Command1.Enabled = False
  Label2.Visible = True
  Label15.Visible = True
  Label17.Visible = True
  ProgressBar2.Visible = True
  MSHFlexGrid1.Visible = True
  MSHFlexGrid1.ColWidth(0) = 1450
  MSHFlexGrid1.ColWidth(1) = 2500
    
  c1 = d1
  c2 = d2
  c3 = d3
  c4 = d4
  ProgressBar2.Value = 0
  ProgressBar2.Min = 0
  nNB_IP_A_SCANNER = (f4 - d4) + ((f3 - d3) * 255) + ((f2 - d2) * 255 ^ 2) + ((f1 - d1) * 255 ^ 3) + (f3 - d3) + 1 + ((f2 - d2) * 511) + ((f1 - d1) * 195841)
  ProgressBar2.Max = nNB_IP_A_SCANNER
  
  While Not (bFIN_SCAN_IP Or Command1.Enabled)
       
        DoEvents
        ProgressBar2.Value = ProgressBar2.Value + 1
        Label15.Caption = Str(c1) & "." & Str(c2) & "." & Str(c3) & "." & Str(c4)
        TTL = Pingocx(0).Ping(c1 & "." & c2 & "." & c3 & "." & c4)
        If TTL >= 0 Then
           nNB_ADRESSES_RESOLUES = nNB_ADRESSES_RESOLUES + 1
           MSHFlexGrid1.Rows = nNB_ADRESSES_RESOLUES
           MSHFlexGrid1.Row = nNB_ADRESSES_RESOLUES - 1
           MSHFlexGrid1.Col = 0
           MSHFlexGrid1.Text = c1 & "." & c2 & "." & c3 & "." & c4
           MSHFlexGrid1.CellAlignment = flexAlignLeftCenter
           If Check2.Value = 1 Then
              MSHFlexGrid1.Row = nNB_ADRESSES_RESOLUES - 1
              MSHFlexGrid1.Col = 1
              MSHFlexGrid1.Text = Pingocx(0).ToNom(c1 & "." & c2 & "." & c3 & "." & c4)
              MSHFlexGrid1.CellAlignment = flexAlignLeftCenter
           End If
           
           MSHFlexGrid1.Col = 2
           MSHFlexGrid1.Text = Val(TTL)
           MSHFlexGrid1.CellAlignment = flexAlignLeftCenter
        End If
        If (c1 = f1) And (c2 = f2) And (c3 = f3) And (c4 = f4) Then
           bFIN_SCAN_IP = True
        End If
        'incrémentation de l'adresse ip
        If c4 < 255 Then
           c4 = c4 + 1
        Else
           c4 = 0
           If c3 < 255 Then
              c3 = c3 + 1
           Else
              c3 = 0
              If c2 < 255 Then
                 c2 = c2 + 1
              Else
                 c2 = 0
                 If c1 < 240 Then
                    c1 = c1 + 1
                 End If
              End If
           End If
        End If
  
  Wend
  Command1.Enabled = True
  Label2.Visible = False
  Label15.Visible = False
  If bFIN_SCAN_IP Then
     Label16.Visible = True
  End If
End Sub
    
Private Sub Command2_Click()
  If Command1.Enabled = False Then
     ProgressBar2.Visible = False
     Label2.Visible = False
     Label15.Visible = False
     Command1.Enabled = True
End If
End Sub

Private Sub Command3_Click()
    nMAX_PORT_SIMULT = Val(Text10.Text)
    nPORT_DEBUT = Val(Text8.Text)
    nPORT_FIN = Val(Text9.Text)
    nPORT_COURS = nPORT_DEBUT
    Label11.Visible = False
    'Test de valiité des informations saisies
    
    If nPORT_DEBUT = 0 Or nPORT_FIN = 0 Then
       ok = MsgBox("Les ports de début et de fin doivent être renseignés", 16, "STOP")
       Exit Sub
    End If
    If nPORT_FIN < nPORT_DEBUT Then
       ok = MsgBox("Le port de fin doit être supérieur au port de début de scan", 16, "STOP")
       Exit Sub
    End If
    
    
    ProgressBar1.Min = nPORT_DEBUT
    ProgressBar1.Max = nPORT_FIN + 0.1
    List1.Clear
    ProgressBar1.Visible = True
    List1.Visible = True
    Label12.Visible = True
    Label13.Visible = True
    Command3.Enabled = False
    
    'chargement des winsocks utilisés simultanéments
    For i = 1 To nMAX_PORT_SIMULT
       Load Winsock(i)
    Next i
    
    While nPORT_COURS <= nPORT_FIN And Command3.Enabled = False
          For j = 1 To nMAX_PORT_SIMULT
              DoEvents
              If nPORT_COURS = nPORT_FIN + 1 Or Command3.Enabled = True Then
                 GoTo FIN
              End If
              ProgressBar1.Value = nPORT_COURS + 0.1
              'si le winsock n'est pas encore libéré
              If Winsock(j).State <> 0 Then
                 GoTo PORT_SUIVANT
              End If
              Winsock(j).RemoteHost = Text7.Text
              Winsock(j).RemotePort = nPORT_COURS
              Winsock(j).Connect
              Label13.Caption = Str(nPORT_COURS)
              nPORT_COURS = nPORT_COURS + 1
PORT_SUIVANT:
          Next j
    Wend
FIN:
      
    'Puis on décharge les objets chargés
    For k = 1 To nMAX_PORT_SIMULT
        Unload Winsock(k)
    Next k
    Command3.Enabled = True
    Label12.Visible = False
    Label13.Visible = False
    If nPORT_COURS > nPORT_FIN Then
      Label11.Visible = True
    End If
    
End Sub

Private Sub Command4_Click()
If Command3.Enabled = False Then
   ProgressBar1.Visible = False
   Label12.Visible = False
   Label13.Visible = False
   Command3.Enabled = True
End If
End Sub

Private Sub DBGrid1_Click()

End Sub

Private Sub configuration_Click()

End Sub

Private Sub Command5_Click()
  Dim cADR_IP, cIP1, cIP2, cIP3 As String
  cADR_IP = Trim(Winsock(0).LocalIP)
  cIP1 = Mid(cADR_IP, 1, InStr(cADR_IP, ".") - 1)
  cADR_IP = Mid(cADR_IP, Len(cIP1) + 2)
  cIP2 = Mid(cADR_IP, 1, InStr(cADR_IP, ".") - 1)
  cADR_IP = Mid(cADR_IP, Len(cIP2) + 2)
  cIP3 = Mid(cADR_IP, 1, InStr(cADR_IP, ".") - 1)
  Text11.Text = cIP1
  Text15.Text = cIP1
  Text12.Text = cIP2
  Text16.Text = cIP2
  Text13.Text = cIP3
  Text17.Text = cIP3
  Text14.Text = "1"
  Text18.Text = "255"
  
End Sub

Private Sub Form_Load()
go = Shell("regsvr32 ping.ocx /s", 0)
End Sub

Private Sub HScroll1_Change()
  Text10.Text = Str(HScroll1.Value)
End Sub

Private Sub Pingocx1_GotFocus(Index As Integer)

End Sub

Private Sub quitter_Click()
  Form1.Hide
  Unload Form1
End Sub

Private Sub SSTab1_Click(PreviousTab As Integer)
  Label17.Visible = False
  MSHFlexGrid1.Visible = False
  Label16.Visible = False
  ProgressBar2.Visible = False
  Label2.Visible = False
  Label15.Visible = False
  Command1.Enabled = True
  Label11.Visible = False
  ProgressBar1.Visible = False
  Command3.Enabled = True
  List1.Visible = False
  Label12.Visible = False
  Label13.Visible = False
  List1.Visible = False
  'Text11.Text = ""
  'Text12.Text = ""
  'Text13.Text = ""
  'Text14.Text = ""
  'Text15.Text = ""
  'Text16.Text = ""
  'Text17.Text = ""
  'Text18.Text = ""
  'Text9.Text = ""
  'Text8.Text = ""
  'Text7.Text = "127.0.0.1"
  'Check1.Value = 0
End Sub

Private Sub Timer1_Timer()
  Text1.Text = Winsock(0).LocalHostName
  Text3.Text = Winsock(0).LocalIP
  Text4.Text = MACAddress
End Sub

Private Sub Winsock_Connect(Index As Integer)
  List1.AddItem ("[OUVERT] PORT: " & Winsock(Index).RemotePort)
  Winsock(Index).Close
End Sub
Private Sub Winsock_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  Winsock(Index).Close
End Sub

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.