简体   繁体   English

如何从Java打开Linux终端并能够在其中执行命令?

[英]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. 我想编写一个通过ssh连接到远程计算机并安装预定义软件的程序。 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? 我遇到了一些问题:如何从Java打开终端并向其发送命令?(OutputStream不起作用)已经使用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. 您将需要一个Java库(例如JSch) ,以通过ssh直接与终端进行交互,然后利用screen实用程序在您的Java prog和用户之间共享单个终端屏幕。

From your java prog: 从您的Java编:

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. 当然,远程用户必须等到java prog初始化屏幕才能将其附加到屏幕上。

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. 您的程序必须足够聪明才能处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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