简体   繁体   English

使用 JSch 为通过 SSH 执行的命令提供输入/子命令

[英]Providing input/subcommands to command executed over SSH with JSch

I'm trying to manage router via Java application using Jcraft Jsch library.我正在尝试使用 Jcraft Jsch 库通过 Java 应用程序管理路由器。

I'm trying to send Router Config via TFTP server.我正在尝试通过 TFTP 服务器发送路由器配置。 The problem is in my Java code because this works with PuTTY.问题出在我的 Java 代码中,因为它适用于 PuTTY。

This my Java code:这是我的 Java 代码:

int port=22;
String name ="R1";
String ip ="192.168.18.100";
String password ="root";

JSch jsch = new JSch();
Session session = jsch.getSession(name, ip, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");

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

InputStream in = channelExec.getInputStream();
channelExec.setCommand("enable");

channelExec.setCommand("copy run tftp : ");
//Setting the ip of TFTP server 
channelExec.setCommand("192.168.50.1 : ");
// Setting the name of file
channelExec.setCommand("Config.txt ");

channelExec.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
    System.out.println(line);
}
session.disconnect();

I get我得到

Line has an invalid autocommand '192.168.50.1'行有一个无效的自动命令“192.168.50.1”

The problem is how can I run those successive commands.问题是我如何运行这些连续的命令。

Calling ChannelExec.setCommand multiple times has no effect.多次调用ChannelExec.setCommand无效。

And even if it had, I'd guess that the 192.168.50.1 : and Config.txt are not commands, but inputs to the copy run tftp : command, aren't they?即使有,我猜192.168.50.1 :Config.txt不是命令,而是copy run tftp :命令的输入,不是吗?

If that's the case, you need to write them to the command input.如果是这种情况,您需要将它们写入命令输入。

Something like this:像这样的东西:

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("copy run tftp : ");
OutputStream out = channelExec.getOutputStream();
channelExec.connect();
out.write(("192.168.50.1 : \n").getBytes());
out.write(("Config.txt \n").getBytes());
out.flush();

In general, it's always better to check if the command has better "API" than feeding the commands to input.一般来说,检查命令是否具有更好的“API”总是比输入命令更好。 Commands usually have command-line arguments/switches that serve the desired purpose better.命令通常具有命令行参数/开关,可以更好地满足所需的目的。


A related question: Provide inputs to individual prompts separately with JSch .一个相关的问题:使用 JSch 分别为各个提示提供输入

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

相关问题 使用JSF为通过JSch通过SSH执行的命令提供输入/子命令 - Providing input/subcommands to command executed over SSH with JSch Using JSF 为使用 JSch 在 SSH 服务器上执行的命令提供多行输入 - Provide multiple lines of input to command executed on SSH server with JSch 使用 JSch 通过 SSH 运行命令 - Run a command over SSH with JSch 执行命令后,Jsch Java ssh客户端未断开连接 - Jsch java ssh client is not getting disconnected after command gets executed 当我使用JSCH通过SSH运行命令时,退出状态2 - ExitStatus 2 when I run a command over SSH with JSCH 未执行使用 ChannelExec 的命令 - Jsch - Command using ChannelExec not executed - Jsch 在单独的控制台/ cmd窗口中(而不是在IDE控制台中)显示用JSch执行的SSH命令的结果 - Show results of SSH command executed with JSch in separate console/cmd window, not in IDE console 使用JSch执行的“ sudo”命令需要密码,即使在交互式SSH会话中不需要密码时也是如此 - “sudo” command executed with JSch requires password, even when the password is not required in an interactive SSH session 无法使用Jsch在ssh中执行命令 - Unable to execute command in ssh with Jsch 如何使用Java中的JSch SSH会话使用需要交互式终端的命令行* nix命令? - How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM