0

I am attempting to establish a VPN connection using a ProcessBuilder in Java. However, the process seems to block, and entering my password has no effect.

I am unable to include my username and password directly in the command line because they can change over time, and for security reasons, I am sometimes prompted for a second password.

Any help is welcome

Minimalist sample :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

    public class VPNConnectionHandler {
    
        public static void main(String[] args) {
            String[] command = {"sudo", "openconnect", "--background", "--protocol=nc", "--user=user", "serveradress"};
    
            try {
                ProcessBuilder processBuilder = new ProcessBuilder(command);
                Process process = processBuilder.start();
    
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
                BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                OutputStreamWriter stdOutput = new OutputStreamWriter(process.getOutputStream());
    
                String line;
                boolean passwordPrompted = false;
    
                while ((line = stdInput.readLine()) != null || (line = stdError.readLine()) != null) {
                    if (line != null) {
                        System.out.println(line);
                        if (line.toLowerCase().contains("password:")) {
                            System.out.println("password asked");
                            if (!passwordPrompted) {
                                System.out.print("password ");
                                BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
                                String password = consoleReader.readLine();
    
                                
                                stdOutput.write(password + "\n");
                                stdOutput.flush();
                                passwordPrompted = true;
                            }
                        }
                    }
                    if (line == null) {
                        break;
                    }
                }
                int exitCode = process.waitFor();
                System.out.println("Process exited with code " + exitCode);
    
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

Using G00se idea, my concern is I don't know how to redirect the output so I can read it in Java and input via the code :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

    public class VPNConnectionManager {
    
        public static void main(String[] args) {
            String[] command = {"sudo", "openconnect", "--background", "--protocol=nc", "--user=user", "server"};
    
            try {
                ProcessBuilder processBuilder = new ProcessBuilder(command).inheritIO();    
                Process process = processBuilder.start();
        
                //password request
                //scanner or what ever to I can fill in the password


                int exitCode = process.waitFor();
                System.out.println("Process exited with code " + exitCode);
    
    
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

Maybe this can help me, but no idea how to make it work here :

https://github.com/agavrilov76/ExpectIt

16
  • I would immediately change this to ProcessBuilder. Strangely it's mentioned in the title but not in the code - it will give you more control. Then, as the second step, you should read stderr in a separate thread. Are you entering a password for sudo too? If so, try to get rid of that for this command in sudoers Commented Jul 2, 2024 at 21:39
  • You are right, It's been few days I am fighting this and did not put my ProcessBuilder attempt but a simple one (I will provide an edit in my code). No sudo password needed Commented Jul 2, 2024 at 21:45
  • It's certainly essential to post the real, accurate code. I'd try it myself but I have no openconnect server to log into. I take it then that openconnect asks for the password twice? Commented Jul 2, 2024 at 21:48
  • I updated my question, here you got my best attempt, using processbuilder. Commented Jul 2, 2024 at 22:00
  • 1
    Process process = processBuilder.inheritIO().start(); // END of code What happens? Commented Jul 2, 2024 at 23:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.