我使用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());
}
}