Skip to content

Commit

Permalink
connect to remote with console
Browse files Browse the repository at this point in the history
  • Loading branch information
felixlp938 committed May 10, 2024
1 parent 97a34ba commit 17e5b54
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
15 changes: 14 additions & 1 deletion src/main/java/de/felixletsplays/CredentialManager/App.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package de.felixletsplays.CredentialManager;

import de.felixletsplays.CredentialManager.Connection.Connection;
import de.felixletsplays.CredentialManager.Connection.Utils;

import java.io.IOException;

import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

Expand Down Expand Up @@ -56,7 +61,7 @@ public static void main(String[] args) {
System.out.print("> ");
command = input.nextLine();

if (command != null) {
if (command != null || !command.isBlank()) {
if (command.startsWith("?")) {
cmd.displayHelp();
} else if (command.startsWith("^")) {
Expand All @@ -71,6 +76,14 @@ public static void main(String[] args) {
cmd.view(input, command);
} else if (command.startsWith("list")) {
cmd.list(input, command);
} else {
try {
Connection connectTo = new Utils().readConnectionConfig(command);
connectTo.connect();
} catch (IOException | InterruptedException ex) {
System.out.println("This connection does not exists!");
System.err.println(ex.getMessage());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package de.felixletsplays.CredentialManager.Connection;

import de.felixletsplays.CredentialManager.App;
import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;

/**
* Class to create a connection object
*
*
* @author Felix
*/
public class Connection {

private String ID;

private String remotehost;
private String user;

private String keyPath;

private String args;

public Connection(ConnectionBuilder builder) {
this.ID = builder.getID();
this.remotehost = builder.getRemotehost();
Expand Down Expand Up @@ -65,22 +72,26 @@ public String getArgs() {
public void setArgs(String args) {
this.args = args;
}

/**
* Create a new process and connect with SSH
*
* @throws java.io.IOException
* @throws java.lang.InterruptedException
*/
public void connect() throws IOException {
public void connect() throws IOException, InterruptedException {
String keyfilearg = "";
if (!keyfilearg.isEmpty() || !keyfilearg.isBlank()) {
keyfilearg = "-i " + keyPath;
if (!getKeyPath().isEmpty() || !getKeyPath().isBlank()) {
keyfilearg = "-i" + keyPath.trim();
}

ProcessBuilder builder = new ProcessBuilder("ssh", this.user + "@" + this.remotehost, keyfilearg, args);
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
builder.inheritIO();

Process sshProcess = builder.start();
sshProcess.waitFor();
}
}

0 comments on commit 17e5b54

Please sign in to comment.