Question

Java Language

Do not scan other networks. You may only be allowed to scan your own network (that you are in charge of, such as home network, to find vulnerabilities so that you can take steps to secure your network. 1) Create file fports with the first line as a hostname that needs to be scanned for certain ports to be opened on it (the values shown are examples... you need to select your own values): host name 80 22 443 445 2) Write a java program to accomplish the following: Open file fports -Read the hostname to be scanned and store it in variable hostname Read the ports into array of integers ports -Create array status (of boolean type) Write a for loop to scan one port at a time and make note of its status (true if the port is open false if the port is closed) Write a for loop to store the result of the scan in file fports result as follows (the values shown are examples): host name 80 Open Close 22 443 Open 445 Close Write a for loop to print the result of the scan as well

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I tried my best and commented the code for you. In case of difficulties, ping me...

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
*
* @author Sam
*/
public class PortChecker {

    public static void main(final String... args) throws InterruptedException, ExecutionException, FileNotFoundException, IOException {
        final ExecutorService es = Executors.newFixedThreadPool(20);
        BufferedReader br = new BufferedReader(new FileReader("fports"));
        final String ip = br.readLine(); //read ip or hostnaem
        final int timeout = 200;
        System.out.println(ip);
        String port;
        final List<Integer> ports = new ArrayList<>();
        while ((port = br.readLine()) != null) {
            ports.add(Integer.parseInt(port)); //read and addd ports
            System.out.println(port); //print port
        }
        final List<Future<Boolean>> futures = new ArrayList<>();
        ports.stream().forEach((p) -> {
            futures.add(portIsOpen(es, ip, p, timeout)); //try for each saved ports
        });
        es.shutdown();
        System.out.println("\n\n\n");
        System.out.println(ip);
        PrintWriter pw = new PrintWriter("fports_results");
        for (int i = 0; i < ports.size(); i++) {
            pw.println(ports.get(i) + "\t" + (futures.get(i).get() ? "open" : "close")); //print port and status as open/clsoe
        }
        pw.flush();
        pw.close();
    }

    public static Future<Boolean> portIsOpen(final ExecutorService executorService, final String ip, final int port, final int timeout) { //to parallelizeand reduce time
        return executorService.submit(() -> {
            try (Socket socket = new Socket()) {
                socket.connect(new InetSocketAddress(ip, port), timeout);
                return true; //if port open,return true
            } catch (Exception ex) {
                return false; //if closed
            }
        });
    }
}

Add a comment
Know the answer?
Add Answer to:
Java Language Create file fports with the first line as a hostname that needs to be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • This isa Java program Create file, file 1, with the following values: 3 4 35 20...

    This isa Java program Create file, file 1, with the following values: 3 4 35 20 -43 17 -10 6 7 -2 13 1 2 3 Write a program in Java to do the following: Open "file 1" and read the first two values, rand c, which are supposed to indicate the dimensions of the two-dimensional array whose values are given in the file after the values of r and c. Create a two-dimensional array of size r times c....

  • Using network sockets, write a C program called client that receives three command-line arguments in the...

    Using network sockets, write a C program called client that receives three command-line arguments in the form: client host port file and sends a request to a web server. The command-line arguments are hostRepresents the web server to connect to port Represents the port number where a request is sent. Normally an HTTP request is sent over port 80, but this format allows for custom ports file Represents the file requested from the web server Your program should create a...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • PYTHON PROGRAMMING LANGUAGE Task 5 Open a csv file for writing create 3 rows that store...

    PYTHON PROGRAMMING LANGUAGE Task 5 Open a csv file for writing create 3 rows that store username, first name, last name and age data (four columns) write the data to the csv file under the correct column Task 6 Open the file in task 5) and read its contents Output the content in a tabular format of columns and values

  • Write a java program to create a student file which includes first name, last name, and...

    Write a java program to create a student file which includes first name, last name, and GPA. Write another Java program to open the students file. Display the students list and calculate the average GPA of the class.  

  • java Part 1 Create a NetBeans project that asks for a file name. The file should...

    java Part 1 Create a NetBeans project that asks for a file name. The file should contain an unknown quantity of double numeric values with each number on its own line. There should be no empty lines. Open the file and read the numbers. Print the sum, average, and the count of the numbers. Be sure to label the outputs very clearly. Read the file values as Strings and use Double.parseDouble() to convert them. Part 2 Create a NetBeans project...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT