0

I want to write a program which controls a tool on a remote server. For that I want to create a SSH-Method to connect to the remote server and execute some commands to get some data. Sadly I don’t find a way to handle the SSH part properly.

Here is a flowchart showing what the method should be able to do: Example Flowchart

First I want to connect via SSH to the remote server. I successfully tried that with “JSch”.

After that, the method should enter the command ls -l into the shell and wait for the response. Now it should parse the output, and act differently depending on the output.

If there is a folder with the name “folder123”, the next commands should be to change into this folder (cd folder123) and enter ls -l there again. This list of content then should be returned and the ssh connection can be closed.

If the folder doesn’t exist, the program should create one (mkdir folder123) and then return a code (-1).

In my main program I will use the new data to make some decisions and then going on.

I found ways with JSch to execute one command, like ls -l and even multiple commands and get the response back as a string. Sadly, I have to pass all commands in one block for that, so I have no way to make the decision in the middle of it. When I want to implement decision making, I have to close the connection first. That would lead to a lot of connection building overhead, especially when there will be more than one decision to make and I have multiple navigating steps between them.

So is there a way to make this decision while the connections stays establish, so I can directly enter the next command after it?

Edit: I am just playing around by now. So my solution with the multiple commands is basically the answer from Mihail in this post

For this part of the task it would be more simple to use the ways DaveyDaveDave and Aaron proposed. But i have to do some more specific tasks.I thought it is the best to provide a simple example first, but here is another one which needs to use a program on the remote server:

Another Example

After the ssh connection is established (the only non-graphical way to access this server), I have to login in the tool (that is a one-liner the command line).

In the cmd, and through this in my ssh connection, i get an output after that. It shows some information about the user and ends with the word "END" and above that i have a return code. The function has to wait for the "END" and then parse the return code. Depending on it, the function should try again or enter the next command.

After the next command it has to do the same validation and when everything worked until now, the last output should be returned to the main program (in this it contains network elements and information about them).

Maas
  • 21
  • 5
  • Post your code and the issues with it. – Strelok Aug 21 '18 at 08:44
  • Is it _really_ a requirement that you use SSH for this? It feels like an overly convoluted, error-prone approach which will require a huge effort to avoid creating security vulnerabilities. If you're in an environment where you can map the target drive to a location that can be accessed from your application, you can just use Java's in-built file system functionality which will be way easier. – DaveyDaveDave Aug 21 '18 at 08:47
  • 2
    How about putting your smartness into a shell script? e.g. `[ -d folder123 ] && ls -l folder123`. – Aaron Aug 21 '18 at 08:47
  • If you just need to inspect and manipulate files on the remote system, you should consider using SFTP rather than plain SSH. SFTP has commands to list the contents of a directory and to make new directories. And Jsch includes an SFTP client. – Kenster Aug 21 '18 at 12:09
  • @Kenster Sadly it is not just file management. I tried to made it clear with the second example. I have to communicate with an application on the remote server. Bc of security restrictions the only remote way to do that is over ssh. – Maas Aug 22 '18 at 05:53

1 Answers1

0

Found an fitting answer for my problem in another Thread where I asked a more specific question.

Look here if you have similar problems:

Change System.in and read System.out programmly on the fly (Jsch)

Maas
  • 21
  • 5