简体   繁体   中英

How to open linux terminal from java and be able to execute commands in it?

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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM