User Tools

Site Tools


java:network:network-scanner

Network Scanner

Description

Network Scanner is a Java application that provides a graphical user interface for scanning the local network and performing basic network operations such as ping and nslookup.

Features

  • Scan local network to find active IP addresses
  • Perform ping on a selected IP address
  • Perform nslookup on a selected IP address
  • Intuitive Windows-style graphical interface

System Requirements

  • Java Runtime Environment (JRE) 8 or later
  • Windows operating system (for native Windows look and feel)

Installation

  1. Download the application JAR file
  2. Ensure you have JRE installed
  3. Double-click the JAR file to run the application

Usage

  1. Launch the application
  2. The list of IP addresses from the local network will be automatically displayed
  3. Select an IP address from the list
  4. Click the “Ping Selected IP” button to perform a ping
  5. Click the “NSLookup Selected IP” button to perform an nslookup
  6. Results will be displayed in a separate window

Source Code

NetworkScannerApp.java
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<String> 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<String> 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<String> getIpAddresses() {
        List<String> 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<String, Void> worker = new SwingWorker<String, Void>() {
            @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);
    }
}

Troubleshooting

If you encounter issues:

  • Make sure you're running the application with administrator rights
  • Check if your firewall is not blocking the application
  • Ensure you have the necessary permissions to execute network commands
java/network/network-scanner.txt · Last modified: 2024/08/17 05:38 by odefta