I am sending commands with JSch using a ChannelShell.
But I am getting trouble doing it against a Windows machine.
This is the code:
package example;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import com.jcraft.jsch.*;
public class JSchShell {
private static JSch jsch = null;
private static String user = null;
private static String passwd = null;
private static String host = null;
static String inputCommands = "dir\r\ncd ..\r\nexit\r\n";
public static void main(String[] args) throws Exception {
if (args.length < 3) {
throw new RuntimeException("Params needed: <host/ip> <user> <passwd>");
}
host = args[0];
user = args[1];
passwd = args[2];
jsch = new JSch();
try {
Session session = jsch.getSession(user, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no");
long startTime = System.nanoTime();
System.out.println("Before session.connect()");
session.connect();
long endTime = System.nanoTime();
long duration = (endTime - startTime);
System.out.println("After session.connect() - Took:"+ duration/1000000 + "ms");
startTime = System.nanoTime();
Channel channel = session.openChannel("shell");
InputStream in = new ByteArrayInputStream(inputCommands.getBytes(StandardCharsets.UTF_8));
channel.setInputStream(in);
channel.setOutputStream(System.out, true);
System.out.println("Before channel.connect()");
channel.connect();
do {
Thread.sleep(1);
} while(!channel.isEOF());
endTime = System.nanoTime();
duration = (endTime - startTime);
System.out.println("After commands - Took:"+ duration/1000000 + "ms");
session.disconnect();
} catch (Exception e) {
System.out.println(e);
}
}
}
It works fine if I run it against a Unix machine, but it seems not to do anything against Windows machine.
Here is the output against a Unix machine (I just ssh to my localhost, but it works on others):
[sysadmin@unixhost jsch]$ java -jar jschShell.jar localhost user passwd
Before session.connect()
After session.connect() - Took:340ms
Before channel.connect()
Last login: Tue Sep 20 16:05:30 2016 from localhost
#############################################################################
dir
cd ..
exit
[sysadmin@unixhost ~]$ dir
certificados corina corina-curl.tgz keys-for-mft tom
[sysadmin@unixhost ~]$
[sysadmin@unixhost ~]$ cd ..
[sysadmin@unixhost home]$
[sysadmin@unixhost home]$ exit
logout
After commands - Took:34ms
[sysadmin@unixhost jsch]$
Here is the output for a Windows machine:
[sysadmin@unixhost jsch]$ java -jar jschShell.jar windowshost user passwd
Before session.connect()
After session.connect() - Took:818ms
Before channel.connect()
Last login: Tue Sep 20 2016 15:58:20 +0100
After commands - Took:108ms
[sysadmin@unixhost jsch]$
This is driving me nuts... I have been testing ChannelExec, and I get it working on both machines. I have also used the approach of this answer to another JSch question, but I got a similar result.
Any hints?