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");
Grigory Rozhkov
  • 85
  • 1
  • 12
  • 1
    http://stackoverflow.com/questions/2514439/how-to-run-ssh-commands-on-remote-system-through-java-program // refer this one. For pure java solution jsch can be used – Nitin Dandriyal May 12 '15 at 08:13
  • http://stackoverflow.com/questions/4194439/sending-commands-to-server-via-jsch-shell-channel This link has some examples as well – Nitin Dandriyal May 12 '15 at 08:24

1 Answers1

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.

ktorn
  • 1,032
  • 8
  • 14
  • Thanks but can you explain more exactly how implement this using screen? – Grigory Rozhkov May 12 '15 at 09:13
  • 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. – ktorn May 12 '15 at 09:24
  • 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 ;) – ktorn May 12 '15 at 09:47