我已经使用JSch创建了一个到远程windows机器的ssh会话。windows机器上安装了cygwin以接受来自远程客户端的ssh。要执行我使用以下命令打开的可执行通道的命令,
Channel channel = client_session.openChannel("exec"); 我从一个通道将一个共享目录映射到机器上。由于同一通道不能用于运行另一个命令,因此我使用同一会话的另一个通道来检索映射的驱动器。
但是我没有得到之前映射的驱动器本身(我之前在相同代码中映射的那个驱动器)。如何使这两个通道同步。我使用的代码如下
Channel channel = client_session.openChannel("exec");
((ChannelExec)channel).setCommand("net use Y: \\\\\\\\share\\\\directory /USER:WORKGROUP\\\\User password");
channel.connect();
channel = client_session.openChannel("exec");
((ChannelExec)channel).setCommand("net use");
channel.connect();我在运行后得到的输出是,
The command completed successfully.
exit-status: 0
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
Unavailable Y: \\share\directory Microsoft Windows Network
The command completed successfully.
exit-status: 0此外,我还需要在映射目录上执行多个命令,根据前面的命令输出,我需要在同一映射目录上运行更多命令。我认为如果通道是同步的,那么运行多个命令的问题就会得到解决。
public void method()
{
Channel channel = null;
try
{
channel = client_session.openChannel("exec");
((ChannelExec) channel).setCommand("net use Y: \\\\\\\\share\\\\directory /USER:WORKGROUP\\\\User password");
channel.connect();
byte[] tmp=new byte[1024];
while(true)
{
while(in.available()>0)
{
int i=in.read(tmp, 0, 1024);
if(i<0)
break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed())
{
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
}
}
catch (Exception e)
{
System.out.println(e.getStackTrace()[0].getLineNumber());
System.out.println(e.toString());
}
try
{
channel = client_session.openChannel("exec");
((ChannelExec)channel).setCommand("net use");
channel.connect();
byte[] tmp=new byte[1024];
while(true)
{
while(in.available()>0)
{
int i=in.read(tmp, 0, 1024);
if(i<0)
break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed())
{
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
}
}
catch(Exception e)
{
System.out.println(e.getStackTrace()[0].getLineNumber());
System.out.println(e.toString());
}
}发布于 2016-11-29 15:45:48
等待第一个通道关闭,然后再打开另一个通道。
https://stackoverflow.com/questions/40858078
复制相似问题