Validación IP's

import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;
import javax.swing.*;
public class Principal{
public static String ValidarDirIP(String valorDirIP) {
String direccion,clase,mensaje;
String caracteristica="Publica";
direccion = valorDirIP;
 /* patrón para los números de .0 a .255 */
String _255 = "(?:25[0-5]
2[0-4][0-9]
[01]?[0-9][0-9]?)";
/* patrón para los números de .16 a .31 */
String _16_31 = "((1[6-9])
(2[0-9])
(3[01]))";
clase = "";
Pattern p = Pattern.compile( "^(?:" + _255 + "\\.){3}" + _255 + "$");
Matcher m = p.matcher(direccion);
if (!m.find()){
JOptionPane.showMessageDialog(null, "La direccion IP esta incorrecta", "Error",JOptionPane.ERROR_MESSAGE);
}
else {
/* DIRECCIONES IP PRIVADAS
* 10.0.0.0 a 10.255.255.255 CLASE A
* 172.16.0.0 a 172.31.255.255 CLASE B
* 192.168.0.0 a 192.168.255.255 CLASE C*/
if(direccion.matches("^192\\.168\\.(" + _255 + ")\\.(" + _255 + ")$")
direccion.matches("^10\\.(" + _255 + ")\\.(" + _255 + ")\\.(" + _255 + ")$")
direccion.matches("^172\\." + _16_31 + "\\.(" + _255 + ")\\.(" + _255 + ")$")){
caracteristica = "Privada";
}
if(direccion.matches("(([0-9])
([1-9][0-9])
(1[0-1][0-9]
12[0-7]))\\..*"))
{
clase = "A";
if(direccion.matches("(0)\\.(0)\\.(0)\\.(0)")){
caracteristica = "Publica, Direccion IP por default";
}
else if(direccion.matches("(127)\\..*")){
caracteristica = "Publica, denominada LOOPBACK";
}
else if(direccion.matches("(0)\\..*")){
caracteristica = "Direccion Reservada";
}
}
else if(direccion.matches("(12[8-9]
1[3-8][0-9]
19[0-1])\\..*")){
clase = "B";
}
else if(direccion.matches("(19[2-9]
2[0-1][0-9]
22[0-3])\\..*")){
clase = "C";
}
else if(direccion.matches("(22[4-9]
23[0-9])\\..*")){
clase = "D";
}
else if(direccion.matches("(24[0-9]
25[0-5])\\..*")){
clase = "E";
if(direccion.matches("(255)\\.(255)\\.(255)\\.(255)"))
caracteristica = "Publica (BROADCAST)";
}
else {
clase = "";
caracteristica = "";
JOptionPane.showMessageDialog(null, "La direccion IP no debe tener 0 (cero) antes del numero", "Error",JOptionPane.ERROR_MESSAGE);
return "La direccion IP no debe tener 0 (cero) antes del valor";
}
mensaje = "La direccion IP: \t\t" + direccion + "\nPertenece a: \nClase: \t\t" + clase+ "\nTipo: \t\t" + caracteristica;
return mensaje;
}
return "Intente Nuevamente\n\n";
}
public static void main(String args[]){
JPanel panel, caja;
JButton boton;
final JTextField txtDireccion;
JLabel titulo, ingresar;
final JTextArea resultado;
String salida;
panel = new JPanel();
boton = new JButton("Validar");
titulo = new JLabel("PROGRAMA PARA VERIFICAR LAS DIRECCIONES IP.");
ingresar = new JLabel("Ingrese la direccion IP: ");
caja = new JPanel();
caja.setLayout(new GridLayout(0, 1));
txtDireccion = new JTextField();
salida = "\n\n\n ";
resultado = new JTextArea(salida);
resultado.setEditable(false);
caja.add(ingresar, BorderLayout.NORTH);
caja.add(txtDireccion, BorderLayout.CENTER);
caja.add(boton, BorderLayout.SOUTH);
caja.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
panel.setLayout(new BorderLayout(10, 10));
txtDireccion.setSize(100, 10);
panel.add(titulo, BorderLayout.NORTH);
panel.add(caja, BorderLayout.CENTER);
panel.add(resultado, BorderLayout.SOUTH);
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
boton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String direccion = txtDireccion.getText();
resultado.setText(ValidarDirIP(direccion));
return;
}
});
JFrame f = new JFrame("VERIFICADOR DE DIRECCIONES IP - Poogramers");
f.add(panel);
f.setBounds(200, 200, 500, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

5 comentarios:

  1. Este oprograma esta super interesante y me ha servido de mucho ya que me han enviado untrabajo similar...

    ResponderEliminar
  2. wevones m salvaron la vida.. mejor dicho el semestre... sigan asi.. saludos desd cuenca

    ResponderEliminar
  3. Este comentario ha sido eliminado por el autor.

    ResponderEliminar
  4. Este comentario ha sido eliminado por el autor.

    ResponderEliminar
  5. Amigo me podrias ayudar con la descarga de este proyecto me gustaria probalo como funciona muchas gracias de ante mano !!!

    ResponderEliminar

Seguidores