Discussion:
[JSch-users] Channel not connected
langoustator
2014-04-07 06:07:20 UTC
Permalink
Hi,

I'm trying to send a few commands to a bunch of cisco switch using jcsh shell channel.

Most of the times (98/100) it works flawlessly, but there are some sessions that hang (and it's not always the same switch that is being accessed). Here is the
piece of code:

JSch jsch=new JSch();
jsch.setConfig("StrictHostKeyChecking","no");
Session session=jsch.getSession(user,hostIP,22);
session.setPassword(password);
session.setServerAliveInterval(1000);
session.connect(30000);
ChannelShell channel = (ChannelShell) session.openChannel("shell");
OutputStream inputstream_for_the_channel = channel.getOutputStream();
PrintStream commander = new PrintStream(inputstream_for_the_channel, true);

FileWriter fw= new FileWriter(directory + filename+".txt");

InputStream outputstream_from_the_channel = channel.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
String line;
System.out.println(session.isConnected());
channel.connect();
System.out.println(session.isConnected());

//sends commands to the switch
commander.println("terminal length 0");
String commandstemp[] = commands.split("\n");
for(int i = 0;i>>>>>>> When it hangs it's here, the bufferedReader is not ready and the session is not connected anymore

while((!br.ready()) && (i<2)){
Thread.sleep(2000);
i=i+1;
System.out.println("not ready");
System.out.println(session.isConnected());
}
if(i==2){
channel.disconnect();
System.out.println(channel.isConnected());
}
line=br.readLine();
Pattern p = Pattern.compile("^.*>exit$");
Matcher m = p.matcher(line);
b = m.matches();
fw.write(line+"\n");
}
session.disconnect();

A successfull attempt will output:
Before channel.connect(), session is connected : true
After channel.connect(), session is connected : true

An unsuccessfull one:

Before channel.connect(), session is connected : true
After channel.connect(): session is connected : true
After checking if the bufferedreader is ready, session is connected : false


Questions:

Is there anything I'm doing seriously wrong (I'm not a dev so forgive my horrible coding)?
Is there anything I can do to get more info about what's happening when it hangs?

Thanks

Lang



___________________________________________________________
Mode, hifi, maison,… J'achète malin. Je compare les prix avec Voila.fr http://shopping.voila.fr/
___________________________________________________________
Mode, hifi, maison,… J'achète malin. Je compare les prix avec Voila.fr http://shopping.voila.fr/
Loading...