ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ Java / JMS » Executing Unix commands remotely in Java program

Post new topic  Reply to topic
 Executing Unix commands remotely in Java program « View previous topic :: View next topic » 
Author Message
RadhamaniRamadoss
PostPosted: Tue Jul 02, 2013 10:11 pm    Post subject: Executing Unix commands remotely in Java program Reply with quote

Apprentice

Joined: 08 Oct 2009
Posts: 42

I want to execute unix commands remotely from Java program.
I am using below code,

String cmd = "rexec <IP address given> -l <QM name given> date";
Process FindConns = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(FindConns.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);

There are no errors reported.But I am not getting any output results.
Not sure where I am missing here...I have tried various options but ended in vain...
Please help with the correct code to execute unix commands remotely in java program..
Back to top
View user's profile Send private message
mqjeff
PostPosted: Wed Jul 03, 2013 2:34 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

There's probably additional streams that FindConns has available, not just the InputStream (which would be the one I would expect you would *write* data to, not read data from, but that's just me).
Back to top
View user's profile Send private message
RadhamaniRamadoss
PostPosted: Wed Jul 03, 2013 6:11 am    Post subject: Reply with quote

Apprentice

Joined: 08 Oct 2009
Posts: 42

Thanks Jeff for your response...
I am new to Java coding...I browsed a lot to find out an option.But could not get one.
As you have given some hint,could you please explain more so that I could try..?I could not get clear idea still on what option I could try..


Last edited by RadhamaniRamadoss on Wed Jul 03, 2013 6:32 am; edited 1 time in total
Back to top
View user's profile Send private message
mqjeff
PostPosted: Wed Jul 03, 2013 6:27 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

I'm sure the javadocs would be much more helpful than I could be.
Back to top
View user's profile Send private message
RadhamaniRamadoss
PostPosted: Wed Jul 03, 2013 6:33 am    Post subject: Reply with quote

Apprentice

Joined: 08 Oct 2009
Posts: 42

Ok..I will try on some more options and come back...
Back to top
View user's profile Send private message
RadhamaniRamadoss
PostPosted: Sun Jul 07, 2013 12:42 am    Post subject: Reply with quote

Apprentice

Joined: 08 Oct 2009
Posts: 42

I have tried various options..But still no luck...

I tried jsch.But I am getting 'Foriegn host closed connection error' while executing below code,

Session session=jsch.getSession(user, host, 1415);
session.connect();

I am not getting any clue on this ...Please explain if someone has idea on this ..
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Sun Jul 07, 2013 7:51 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20697
Location: LI,NY

It might also help if you can explain what you are trying to do.
There is more than one way to skin a cat, and maybe the way you chose is not the most optimal.
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
RadhamaniRamadoss
PostPosted: Sun Jul 07, 2013 9:28 pm    Post subject: Reply with quote

Apprentice

Joined: 08 Oct 2009
Posts: 42

Thank you so much...

The requirement to me is ,I need to perform health check of Unix servers (df -k ,netstat) for their filesystem and the TCP connectivity to other clients automatically.So,I have a java program in windows system which does lot of other stuffs also like Queue monitoring.In this java program I am implementing the below code as well to connect to Unix machines remotely from Windows and execute certain unix commands.
During this session connect phase,I am getting errors lik 'Foreign host closed connection'.For Queue monitoring I didnt face any problem.So,I beleive there is no firewall or network issues.

Here is the program,


import com.jcraft.jsch.*;
import java.io.*;

public class MonitoringJava{
public static void main(String[] arg){
try{
JSch jsch=new JSch();

String host=null;
if(arg.length>0){
host=arg[0];
}
else{
host="userid@hostname"; // username and ipaddress for machine is given
}
String user=host.substring(0, host.indexOf('@'));
host=host.substring(host.indexOf('@')+1);

Session session=jsch.getSession(user, host, <port number given>);

// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();

String command= "grep 'INFO' filepath"; // enter any command you need to execute

Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);


channel.setInputStream(null);

((ChannelExec)channel).setErrStream(System.err);

InputStream in=channel.getInputStream();

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;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.print(e.getMessage());
}
}

public static class MyUserInfo implements UserInfo{
public String getPassword(){ return passwd; }
public boolean promptYesNo(String str){
str = "Yes";
return true;}

String passwd;

public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return true; }
public boolean promptPassword(String message){
passwd="password"; // enter the password for the machine you want to connect.
return true;
}
public void showMessage(String message){

}

}
}
Back to top
View user's profile Send private message
RadhamaniRamadoss
PostPosted: Sun Jul 07, 2013 9:40 pm    Post subject: Reply with quote

Apprentice

Joined: 08 Oct 2009
Posts: 42

This is the complete error message,
com.jcraft.jsch.JSchException: connection is closed by foreign host
at com.jcraft.jsch.Session.connect(Session.java:248)
at com.jcraft.jsch.Session.connect(Session.java:162)
at MonitoringJava.main(MonitoringJava.java:24)
Back to top
View user's profile Send private message
mqjeff
PostPosted: Mon Jul 08, 2013 8:06 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

That means the remote side closed your connection.
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Mon Jul 08, 2013 9:17 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20697
Location: LI,NY

RadhamaniRamadoss wrote:
Thank you so much...

The requirement to me is ,I need to perform health check of Unix servers (df -k ,netstat) for their filesystem and the TCP connectivity to other clients automatically.So,I have a java program in windows system which does lot of other stuffs also like Queue monitoring.In this java program I am implementing the below code as well to connect to Unix machines remotely from Windows and execute certain unix commands.
During this session connect phase,I am getting errors lik 'Foreign host closed connection'.For Queue monitoring I didnt face any problem.So,I beleive there is no firewall or network issues.


BAD assumption. You are not dealing with the same ports. Firewalls can distinguish between ports and the combination host + port needs to be tested.

RadhamaniRamadoss wrote:

Here is the program,


import com.jcraft.jsch.*;
import java.io.*;

public class MonitoringJava{
public static void main(String[] arg){
try{
JSch jsch=new JSch();

String host=null;
if(arg.length>0){
host=arg[0];
}
else{
host="userid@hostname"; // username and ipaddress for machine is given
}
String user=host.substring(0, host.indexOf('@'));
host=host.substring(host.indexOf('@')+1);

Session session=jsch.getSession(user, host, <port number given>);

// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();

String command= "grep 'INFO' filepath"; // enter any command you need to execute

Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);


channel.setInputStream(null);

((ChannelExec)channel).setErrStream(System.err);

InputStream in=channel.getInputStream();

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;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.print(e.getMessage());
}
}

public static class MyUserInfo implements UserInfo{
public String getPassword(){ return passwd; }
public boolean promptYesNo(String str){
str = "Yes";
return true;}

String passwd;

public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return true; }
public boolean promptPassword(String message){
passwd="password"; // enter the password for the machine you want to connect.
return true;
}
public void showMessage(String message){

}

}
}


I don't think initializing the inputstream by setting it to null is doing what you expect it to do...
What is your getInputStream method returning? a null value?

Have fun
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ Java / JMS » Executing Unix commands remotely in Java program
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.