我通过ssh连接到unix服务器,并尝试执行"ls“命令并获得它的输出。代码是这样的
SessionChannelClient session = client.openSessionChannel();
session.startShell();
String cmd = "ls -l";
session.executeCommand(cmd);
ChannelInputStream in = session.getInputStream();
ChannelOutputStream out = session.getOutputStream();
IOStreamConnector input = new IOStreamConnector(System.in, session.getOutputStream());
IOStreamConnector output = new IOStreamConnector(session.getInputStream(), System.out);在运行之后,我没有在日志文件中获得任何输出。我发现通道请求失败了,如下所示
1019主要信息com.sshtools.j2ssh.connection.ConnectionProtocol -通道请求成功1020主信息com.sshtools.j2ssh.session.SessionChannelClient请求命令执行1021主要信息com.sshtools.j2ssh.session.SessionChannelClient命令is ls -l 1021主信息com.sshtools.j2ssh.connection.ConnectionProtocol发送会话通道1021主信息com.sshtools.j2ssh.transport.TransportProtocolCommon的exec请求发送SSH_MSG_CHANNEL_REQUEST 1021主信息com.sshtools.j2ssh.connection.ConnectionProtocol -等待通道请求应答1032传输协议1 INFO com.sshtools.j2ssh.transport.TransportProtocolCommon -接收到的SSH_MSG_CHANNEL_EXTENDED_DATA 1033 ssh-连接1调试com.sshtools.j2ssh.transport.Service -路由选择SSH_MSG_CHANNEL_EXTENDED_DATA 1033 ssh-连接1调试com.sshtools.j2ssh.transport.Service -完成处理SSH_MSG_CHANNEL_EXTENDED_DATA 1075传输协议1 INFO com.sshtools.j2ssh.connection.ConnectionProtocol -接收SSH_MSG_CHANNEL_FAILURE 1075主信息com.sshtools.j2ssh.connection.ConnectionProtocol-通道请求失败
为什么会发生这种事?
发布于 2013-01-26 01:44:56
您可以使用startShell 或 executeCommand,但不能同时使用两者。
executeCommand只打算调用您选择的特定shell,例如/bin/bash。在大多数情况下,您只需使用startShell启动默认的shell。
这是执行您提供的命令所需的全部内容:
final String cmd = "ls -l" + '\n';
session.startShell();
session.getOutputStream().write(cmd.getBytes());或者,或者使用executeCommand:
final String cmd = "ls -l";
session.executeCommand(String.format("sh -c \"%s\"", cmd));您可以以相同的方式连接到stdout & stderr InputStreams,以查看命令的结果。此外,请注意,SessionChannelClient只能处理一个命令。您需要实例化另一个命令来发送第二个命令。
我建议你下载J2SSH文档。在里面,您可以找到j2ssh获取-started.htm,它解释了API的基本操作。
https://stackoverflow.com/questions/14488137
复制相似问题