Dear,
I'm trying to give the command "dir" on a remote machine that has windows
installed f-secure ssh server, and I am not able, it is possible to do this?
Attached the code I am using
---~--~--~---------~--~----~------------~-------~--~----~
/*
* Copyright (c) 2003-2011 CEDSIF. All rights reserved.
*/
package gov.utrafe.adfundos.transfer;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
/**
* Class definition
*
* @author Xamag Productions
*/
public class SSHClient {
private static final Logger LOG = Logger.getLogger(SSHClient.class);
public static void main(String[] args) throws JSchException, IOException {
testExec(args);
// testShell(args);
}
public static void testExec(String[] args) throws JSchException,
IOException {
JSch.setLogger(new JSCHLogger());
LOG.info("Starting...");
JSch jsch = new JSch();
String username = "adfundos";
String password = "utrafe";
int port = 22;
String host = "192.168.5.194";
// String host = "192.168.5.161";
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
config.put("Protocol", "2");
config.put("Ciphers", "arcfour,arcfour128");
config.put("Cipher", "arcfour");
config.put("GSSAPIAuthentication", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("exec");
String command = "dir"+"\n";
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
channel.setOutputStream(System.out);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
int count = 0;
StringBuilder commandOutPut = new StringBuilder();
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));
commandOutPut.append(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
ee.printStackTrace();
}
++count;
System.out.println("Wainting..." + count);
}
channel.disconnect();
session.disconnect();
System.out.println(commandOutPut);
}
}
------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
JSch-users mailing list
JSch-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jsch-users