1

I want to write a program which connects to remote machines via ssh and install predefined software. Also I wanna make process of installing clear for users by make all it visible to users. I have faced some problems: how to open terminal from java and send commands to it?(OutputStream doesn't work) How to execute command in this terminal when I already ssh? I want to run local scripts on the remote machine and allow user? to interact with terminal, when script is running (for example accept licence of software and so on).

I was trying something like this but it is not working.

    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec("x-terminal-emulator -e ./script.sh");
2

1 Answer 1

1

In theory it is possible. You'll need a java library, such as JSch, to interact directly with a terminal via ssh, and then make use of the screen utility to share a single terminal screen between your java prog and a user.

From your java prog:

screen -d -m -S shared
screen -rx shared
<type your installation commands>

From the remote user:

screen -rx shared

Of course the remote user must wait until the java prog initializes the screen in order to attach to it.

Please note that all kinds of things can go wrong when you let users interact with the screen. Your program must be smart enough to handle it.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but can you explain more exactly how implement this using screen?
I added a more detailed example of using screen for a shared session. You can actually try it manually by opening 2 ssh connections to the same host and running those commands. For how to do it from Java is something you need to refer to the JSch docs. If should add that I never tried this particular scenario.
BTW, I missed some basic assumptions from my answer, such as both the prog and the user are logged in to the same SSH host as the same user ;)

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.