import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class NetworkScannerApp extends JFrame { private JList ipList; private JProgressBar progressBar; private JButton pingButton; private JButton nslookupButton; private JPanel contentPane; public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } EventQueue.invokeLater(() -> { try { NetworkScannerApp frame = new NetworkScannerApp(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }); } public NetworkScannerApp() { setTitle("Network Scanner"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); topPanel.add(new JLabel("IP Addresses:")); contentPane.add(topPanel, BorderLayout.NORTH); List ipAddresses = getIpAddresses(); ipList = new JList<>(ipAddresses.toArray(new String[0])); ipList.setBorder(BorderFactory.createLineBorder(Color.GRAY)); JScrollPane scrollPane = new JScrollPane(ipList); contentPane.add(scrollPane, BorderLayout.CENTER); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); progressBar = new JProgressBar(); progressBar.setIndeterminate(true); progressBar.setVisible(false); bottomPanel.add(progressBar); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); pingButton = new JButton("Ping Selected IP"); pingButton.addActionListener(e -> { String selectedIp = ipList.getSelectedValue(); if (selectedIp != null) { startProcess(selectedIp, "ping"); } else { showErrorDialog(); } }); buttonPanel.add(pingButton); nslookupButton = new JButton("NSLookup Selected IP"); nslookupButton.addActionListener(e -> { String selectedIp = ipList.getSelectedValue(); if (selectedIp != null) { startProcess(selectedIp, "nslookup"); } else { showErrorDialog(); } }); buttonPanel.add(nslookupButton); bottomPanel.add(buttonPanel); bottomPanel.add(Box.createRigidArea(new Dimension(0, 5))); contentPane.add(bottomPanel, BorderLayout.SOUTH); } private List getIpAddresses() { List ipAddresses = new ArrayList<>(); String command = "arp -a"; try { Process process = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { String[] tokens = line.split("\\\\s+"); for (String token : tokens) { if (token.matches("\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.\\\\d+")) { ipAddresses.add(token); } } } process.waitFor(); } catch (Exception e) { e.printStackTrace(); } return ipAddresses; } private void startProcess(String ip, String command) { progressBar.setVisible(true); pingButton.setEnabled(false); nslookupButton.setEnabled(false); SwingWorker worker = new SwingWorker() { @Override protected String doInBackground() throws Exception { if ("ping".equals(command)) { return pingHost(ip); } else { return nslookupHost(ip); } } @Override protected void done() { try { String result = get(); showResultDialog(ip, result, command); } catch (Exception e) { e.printStackTrace(); } finally { progressBar.setVisible(false); pingButton.setEnabled(true); nslookupButton.setEnabled(true); } } }; worker.execute(); } private String pingHost(String ip) { return executeCommand("ping -n 4 " + ip); // Limit to 4 packets for Windows } private String nslookupHost(String ip) { return executeCommand("nslookup " + ip); } private String executeCommand(String command) { StringBuilder result = new StringBuilder(); try { Process process = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { result.append(line).append("\\n"); } process.waitFor(); } catch (Exception e) { e.printStackTrace(); } return result.toString(); } private void showResultDialog(String ip, String result, String command) { JDialog dialog = new JDialog(this, command.toUpperCase() + " Result for " + ip, true); dialog.setSize(400, 300); dialog.setLocationRelativeTo(this); JTextArea textArea = new JTextArea(result); textArea.setEditable(false); textArea.setFont(new Font("Consolas", Font.PLAIN, 12)); JScrollPane scrollPane = new JScrollPane(textArea); dialog.add(scrollPane); JButton closeButton = new JButton("Close"); closeButton.addActionListener(e -> dialog.dispose()); JPanel buttonPanel = new JPanel(); buttonPanel.add(closeButton); dialog.add(buttonPanel, BorderLayout.SOUTH); dialog.setVisible(true); } private void showErrorDialog() { JOptionPane.showMessageDialog(this, "Please select an IP address.", "Error", JOptionPane.ERROR_MESSAGE); } }