elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado:


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Obtener IP con hostname
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Obtener IP con hostname  (Leído 2,240 veces)
GSecurity

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Obtener IP con hostname
« en: 21 Marzo 2020, 21:38 pm »

Hola Gente,

Estoy desarrollando un proyecto de sockets para lo cual necesito validar la IP de una pc a traves del hostname el cual lo ingresare en un textfield. 

Gracias


En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Obtener IP con hostname
« Respuesta #1 en: 21 Marzo 2020, 23:38 pm »

Hola Gente,

Estoy desarrollando un proyecto de sockets para lo cual necesito validar la IP de una pc a traves del hostname el cual lo ingresare en un textfield.  

Gracias

La linea 62 contiene la magia solo para ipv4  :xD , este ejemplo te sirve para el anterior, y veas como se puede crear un JFrame.(50mil maneras diferentes las hay)

Código
  1. package foro;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.net.Inet4Address;
  6. import java.net.UnknownHostException;
  7. import java.util.function.Predicate;
  8.  
  9. /**
  10.  * https://foro.elhacker.net/java/obtener_ip_con_hostname-t503433.0.html
  11.  * @autor rubn
  12.  */
  13. public class ValidarIp extends JFrame {
  14.  
  15.    private final JLabel jLabelTitulo = new JLabel("Introduce un direccion ip");
  16.    private final JTextField jTextField = new JTextField();
  17.    private final JLabel jLabel = new JLabel("Resultado: ");
  18.    private static final String ESPACIOS = "\\s+";
  19.    private final JPanel mainJPanel = new JPanel();
  20.  
  21.    public ValidarIp() {
  22.        super("Validar Ip");
  23.  
  24.        configureLayouts();
  25.  
  26.        configureJFrame();
  27.    }
  28.  
  29.    private void configureJFrame() {
  30.        add(mainJPanel);
  31.        setResizable(Boolean.FALSE);
  32.        setPreferredSize(new Dimension(366, 150));
  33.        pack();
  34.        setLocationRelativeTo(null);
  35.        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.        setVisible(true);
  37.    }
  38.  
  39.    private void configureLayouts() {
  40.        mainJPanel.setLayout(new BoxLayout(mainJPanel, BoxLayout.Y_AXIS));
  41.        mainJPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  42.        mainJPanel.add(jLabelTitulo,BorderLayout.NORTH);
  43.        mainJPanel.add(Box.createVerticalStrut(20));
  44.        mainJPanel.add(jTextField,BorderLayout.CENTER);
  45.        mainJPanel.add(Box.createVerticalStrut(20));
  46.        mainJPanel.add(jLabel,BorderLayout.SOUTH);
  47.  
  48.        listenerJTextField();
  49.    }
  50.  
  51.  
  52.    /**
  53.      * Listener del JTextField
  54.      */
  55.    private void listenerJTextField() {
  56.        jTextField.addActionListener(e -> {
  57.            final String ip = jTextField.getText().replaceAll(ESPACIOS,"");
  58.  
  59.            final Predicate<String> predicate = p -> {
  60.                try {
  61.  
  62.                    return Inet4Address.getByName(ip).getHostAddress().equals(ip);
  63.  
  64.                } catch (UnknownHostException ex) {
  65.                    return Boolean.FALSE;
  66.                }
  67.            };
  68.  
  69.            if (predicate.test(ip)) {
  70.  
  71.                JOptionPane.showMessageDialog(null, "Ip valida");
  72.  
  73.            } else {
  74.                JOptionPane.showMessageDialog(null, "Ip invalida");
  75.            }
  76.        });
  77.    }
  78.  
  79.    public static void main(String ...blabla) {
  80.        new Thread(ValidarIp::new).start();
  81.    }
  82. }


« Última modificación: 22 Marzo 2020, 17:43 pm por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
GSecurity

Desconectado Desconectado

Mensajes: 9


Ver Perfil
Re: Obtener IP con hostname
« Respuesta #2 en: 3 Abril 2020, 08:45 am »

La linea 62 contiene la magia solo para ipv4  :xD , este ejemplo te sirve para el anterior, y veas como se puede crear un JFrame.(50mil maneras diferentes las hay)

Código
  1. package foro;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.net.Inet4Address;
  6. import java.net.UnknownHostException;
  7. import java.util.function.Predicate;
  8.  
  9. /**
  10.  * https://foro.elhacker.net/java/obtener_ip_con_hostname-t503433.0.html
  11.  * @autor rubn
  12.  */
  13. public class ValidarIp extends JFrame {
  14.  
  15.    private final JLabel jLabelTitulo = new JLabel("Introduce un direccion ip");
  16.    private final JTextField jTextField = new JTextField();
  17.    private final JLabel jLabel = new JLabel("Resultado: ");
  18.    private static final String ESPACIOS = "\\s+";
  19.    private final JPanel mainJPanel = new JPanel();
  20.  
  21.    public ValidarIp() {
  22.        super("Validar Ip");
  23.  
  24.        configureLayouts();
  25.  
  26.        configureJFrame();
  27.    }
  28.  
  29.    private void configureJFrame() {
  30.        add(mainJPanel);
  31.        setResizable(Boolean.FALSE);
  32.        setPreferredSize(new Dimension(366, 150));
  33.        pack();
  34.        setLocationRelativeTo(null);
  35.        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.        setVisible(true);
  37.    }
  38.  
  39.    private void configureLayouts() {
  40.        mainJPanel.setLayout(new BoxLayout(mainJPanel, BoxLayout.Y_AXIS));
  41.        mainJPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  42.        mainJPanel.add(jLabelTitulo,BorderLayout.NORTH);
  43.        mainJPanel.add(Box.createVerticalStrut(20));
  44.        mainJPanel.add(jTextField,BorderLayout.CENTER);
  45.        mainJPanel.add(Box.createVerticalStrut(20));
  46.        mainJPanel.add(jLabel,BorderLayout.SOUTH);
  47.  
  48.        listenerJTextField();
  49.    }
  50.  
  51.  
  52.    /**
  53.      * Listener del JTextField
  54.      */
  55.    private void listenerJTextField() {
  56.        jTextField.addActionListener(e -> {
  57.            final String ip = jTextField.getText().replaceAll(ESPACIOS,"");
  58.  
  59.            final Predicate<String> predicate = p -> {
  60.                try {
  61.  
  62.                    return Inet4Address.getByName(ip).getHostAddress().equals(ip);
  63.  
  64.                } catch (UnknownHostException ex) {
  65.                    return Boolean.FALSE;
  66.                }
  67.            };
  68.  
  69.            if (predicate.test(ip)) {
  70.  
  71.                JOptionPane.showMessageDialog(null, "Ip valida");
  72.  
  73.            } else {
  74.                JOptionPane.showMessageDialog(null, "Ip invalida");
  75.            }
  76.        });
  77.    }
  78.  
  79.    public static void main(String ...blabla) {
  80.        new Thread(ValidarIp::new).start();
  81.    }
  82. }

Hasta ahora no llego al nivel para utilizar Predicates  :xD . Gracias por tu ayuda amigo, sin embargo lo que en realidad necesito es que desde una caja de texto se ingrese el Hostname y con un boton "check" se obtena la ip de este hostname.

Gracias de ante mano  :laugh:
En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Obtener IP con hostname
« Respuesta #3 en: 3 Abril 2020, 17:23 pm »

Hasta ahora no llego al nivel para utilizar Predicates  :xD . Gracias por tu ayuda amigo, sin embargo lo que en realidad necesito es que desde una caja de texto se ingrese el Hostname y con un boton "check" se obtena la ip de este hostname.

Gracias de ante mano  :laugh:


Hasta un bebe puede usar un Predicate no tiene nada que ver.  

Son nuevas tareas extra curriculares, debes añadir un JButton al código



Código
  1. /**
  2.  * https://foro.elhacker.net/java/obtener_ip_con_hostname-t503433.0.html
  3.  * @autor rubn
  4.  */
  5. public class ValidarIp extends JFrame {
  6.  
  7.    private final JLabel jLabelTitulo = new JLabel("Introduce un direccion ip");
  8.    private final JTextField jTextField = new JTextField();
  9.    private final JButton btnCheck = new JButton("Chekiar IP");
  10.    private final JLabel jLabelIpResult = new JLabel("Ip: ");
  11.    private static final String ESPACIOS = "\\s+";
  12.    private final JPanel mainJPanel = new JPanel();
  13.  
  14.    public ValidarIp() {
  15.        super("Validar Ip");
  16.  
  17.        configureLayouts();
  18.  
  19.        configureJFrame();
  20.    }
  21.  
  22.    private void configureJFrame() {
  23.        add(mainJPanel);
  24.        setResizable(Boolean.FALSE);
  25.        setPreferredSize(new Dimension(366, 150));
  26.        pack();
  27.        setLocationRelativeTo(null);
  28.        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.        getRootPane().setDefaultButton(btnCheck);
  30.        btnCheck.setBorderPainted(false);
  31.        btnCheck.setFocusPainted(false);
  32.        btnCheck.requestFocus();
  33.        setVisible(true);
  34.    }
  35.  
  36.    private void configureLayouts() {
  37.        mainJPanel.setLayout(new BoxLayout(mainJPanel, BoxLayout.Y_AXIS));
  38.        mainJPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  39.        final JPanel jPanelLabel = new JPanel(new FlowLayout(0));
  40.        jPanelLabel.add(jLabelTitulo);
  41.        mainJPanel.add(jPanelLabel);
  42.        mainJPanel.add(Box.createVerticalStrut(10));
  43.        mainJPanel.add(jTextField,BorderLayout.CENTER);
  44.        mainJPanel.add(Box.createVerticalStrut(10));
  45.  
  46.        //row boton y JLabel
  47.        final JPanel row = new JPanel(new FlowLayout(0));
  48.        row.add(btnCheck);
  49.        row.add(row.add(Box.createHorizontalStrut(20)));
  50.        row.add(jLabelIpResult);
  51.        mainJPanel.add(row);
  52.  
  53.        checkiarIp();
  54.    }
  55.  
  56.  
  57.    /**
  58.      * Listener del JButton
  59.      */
  60.    private void checkiarIp() {
  61.        btnCheck.addActionListener(e -> {
  62.            final String ip = jTextField.getText().replaceAll(ESPACIOS,"");
  63.  
  64.            final Predicate<String> predicate = p -> {
  65.                try {
  66.  
  67.                    return Inet4Address.getByName(ip).getHostAddress().equals(ip);
  68.  
  69.                } catch (UnknownHostException ex) {
  70.                    return Boolean.FALSE;
  71.                }
  72.            };
  73.  
  74.            if (predicate.test(ip)) {
  75.                JOptionPane.showMessageDialog(null, "Ip valida");
  76.                jLabelIpResult.setText("");
  77.                jLabelIpResult.setText("Ip: " + ip);
  78.            } else {
  79.                JOptionPane.showMessageDialog(null, "Ip invalida");
  80.            }
  81.        });
  82.    }
  83.  
  84.    public static void main(String ...blabla) {
  85.        new Thread(ValidarIp::new).start();
  86.    }
  87. }

« Última modificación: 3 Abril 2020, 18:06 pm por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Red Wireless me cambia mi hostname
Seguridad
reem 0 1,918 Último mensaje 21 Febrero 2012, 19:19 pm
por reem
Variable hostname en batch
Scripting
santi810 2 2,795 Último mensaje 20 Diciembre 2013, 17:46 pm
por santi810
[AYUDA] Cambiar hostname
Desarrollo Web
Miseryk 2 1,977 Último mensaje 20 Mayo 2015, 19:38 pm
por Miseryk
¿Hostname no se puede resolver?
GNU/Linux
daredcod3 5 5,305 Último mensaje 7 Julio 2015, 16:19 pm
por MinusFour
Hostname y Servidores DNS en Centos 7
Redes
Ali Baba 1 1,925 Último mensaje 1 Diciembre 2018, 15:23 pm
por #!drvy
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines