I am trying to use JSCH to connect to a remote server and then from that server open a telnet like session over a tcp/ip port. Say connect to server A, and once connected issue a tcp connection to server B over another port. In my webserver logs I see a GET / logged but not GET /foo as I would expect. ANything I m missing here? (I do not need to use Port forwarding since the remote port is accessible to the system I am connected to)
package com.tekmor;
import com.jcraft.jsch.*;
import java.io.BufferedReader;
.
.
public class Siranga {
public static void main(String[] args){
Siranga t=new Siranga();
try{
t.go();
} catch(Exception ex){
ex.printStackTrace();
}
}
public void go() throws Exception{
String host="hostXXX.com";
String user="USER";
String password="PASS";
int port=22;
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
String remoteHost="hostYYY.com";
int remotePort=80;
try {
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("direct-tcpip");
((ChannelDirectTCPIP)channel).setHost(remoteHost);
((ChannelDirectTCPIP)channel).setPort(remotePort);
String cmd = "GET /foo";
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
channel.connect(10000);
byte[] bytes = cmd.getBytes();
InputStream is = new ByteArrayInputStream(cmd.getBytes("UTF-8"));
int numRead;
while ( (numRead = is.read(bytes) ) >= 0) {
out.write(bytes, 0, numRead);
System.out.println(numRead);
}
out.flush();
channel.disconnect();
session.disconnect();
System.out.println("foo");
}
catch (Exception e){
e.printStackTrace();
}
}
}