summaryrefslogtreecommitdiff
path: root/src/main/java/com/it_jaros/networkScanner/App.java
blob: 6d0292fab1c4ab8f66dc6ee6bf50f500dc3534c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.it_jaros.networkScanner;

import java.util.List;
import java.util.stream.Collectors;

public class App {
    public static void main(String[] args) {
        CliParser cliParser = new CliParser();
        try {
            ScanOptions options = cliParser.parseArgs(args);
            Scanner scanner = new Scanner(options.openSocketLimit(), options.timeoutInMillis(),
                    options.delayInMillis());
            printResults(scanner.scan(options));
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
            cliParser.usageAndExit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void printResults(List<ScanResult> results) {
        results.forEach(r -> printResult(r));
    }

    private static void printResult(ScanResult result) {
        String ports = result.openPorts().stream().map(String::valueOf).collect(Collectors.joining(","));
        String output = "[" + result.target() + "]:";
        if (ports.equals("")) {
            output = output + "No open ports found";
        } else {
            output = output + ports;
        }
        System.out.print(output + "\n");
    }
}