java channelexec_请教大神 java操作linux命令的问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

这是我在网上找的一段代码,确实可以操作一些linux命令也可以拿到输出结果;但是我在执行命令“ screen -r XXX ”命令的时候,返回结果“Must be connected to a terminal.” ,其实我是想用代码控制终端的远程连接,然后再这个远程连接中执行一些命令,但是不知道如何实现;

用的是jsch这个jar包,没有中文API太费劲了,不知可有过来人帮忙解答一下,不胜感激;

另外这俩方法该怎么用:

channelExec.setPty(true);//虚拟终端???

channelExec.setTerminalMode(terminal_mode);

public static void main(String[] args) throws Exception {

JSch jsch = new JSch(); // 创建JSch对象

String userName = "userName";// 用户名

String password = "password";// 密码

String host = "host";// 服务器地址

int port = 22;// 端口号

String cmd = "screen -r XXX";// 要运行的命令

Session session = jsch.getSession(userName, host, port); // 根据用户名,主机ip,端口获取一个Session对象

session.setPassword(password); // 设置密码

Properties config = new Properties();

config.put("StrictHostKeyChecking", "no");

session.setConfig(config); // 为Session对象设置properties

int timeout = 60000;

session.setTimeout(timeout); // 设置timeout时间

session.connect(); // 通过Session建立链接

ChannelExec channelExec = (ChannelExec) session.openChannel("exec");

channelExec.setCommand(cmd);

channelExec.setInputStream(null);

channelExec.setErrStream(System.err);

channelExec.connect();

InputStream in = channelExec.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));

String buf = null;

StringBuffer sb = new StringBuffer();

while ((buf = reader.readLine()) != null) {

sb.append(buf);

System.out.println(buf);// 打印控制台输出

}

reader.close();

channelExec.disconnect();

if (null != session) {

session.disconnect();

}

}

可以使用Java中的SSH库来连接到远程Linux服务器,并从其中读取文件。 以下是一个简单的示例,演示如何使用JSch(一个Java SSH库)从Linux服务器上读取文件: ```java import com.jcraft.jsch.*; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class ReadRemoteFile { public static void main(String[] args) { String host = "remote-host"; String user = "username"; String password = "password"; String filePath = "/path/to/file"; try { JSch jsch = new JSch(); Session session = jsch.getSession(user, host, 22); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand("cat " + filePath); channel.setInputStream(null); ((ChannelExec) channel).setErrStream(System.err); InputStream input = channel.getInputStream(); channel.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); channel.disconnect(); session.disconnect(); } catch (JSchException | java.io.IOException e) { e.printStackTrace(); } } } ``` 在此示例中,我们使用JSch库连接到远程Linux服务器,并使用“cat”命令读取指定的文件。然后,我们将获得的内容打印到控制台上。 请注意,此示例中的SSH连接未进行身份验证。在生产环境中,您可能需要使用公钥/私钥身份验证或其他更安全的方法来连接到Linux服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值